From 95f26b340b991f4a4693182e9d7c2e12c1144783 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 1 Apr 2022 17:31:12 -0700 Subject: [PATCH] build: "Quick Checks" no longer uses goenv goenv was making things more complicated than needed since it's really designed to help with interactive use in a shell more than automated use like this. Instead, we'll follow the same strategy that our build.yml was doing of just reading the .go-version file directly and then using the official actions/setup-go action to do the actual installation. The key advantage here is that Go ends up installed in a way where just running "go" will do the right thing, and we no longer need to fuss with shims and version-based path prefixes. Rather than duplicating the logic from build.yml, instead it's factored out into a separate composite action which both build.yml and checks.yml will now share, in case we want to change the Go version selection methodology in the future. --- .github/actions/go-setup/action.yml | 41 --------------------------- .github/actions/go-version/action.yml | 23 +++++++++++++++ .github/workflows/build.yml | 8 ++---- .github/workflows/checks.yml | 34 ++++++++++++---------- 4 files changed, 44 insertions(+), 62 deletions(-) delete mode 100644 .github/actions/go-setup/action.yml create mode 100644 .github/actions/go-version/action.yml diff --git a/.github/actions/go-setup/action.yml b/.github/actions/go-setup/action.yml deleted file mode 100644 index 45e8857706..0000000000 --- a/.github/actions/go-setup/action.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: 'Set Up Go Toolchain' -description: 'Installs the Go toolchain specified in .go-version.' -outputs: - go-version: - description: "Go toolchain version" - value: ${{ steps.go.outputs.go-version }} - gopath: - description: "GOPATH location" - value: ${{ steps.go.outputs.gopath }} - goenv: - description: "Path to the 'goenv' executable" - value: ${{ steps.go.outputs.goenv }} - goenv-bin-dir: - description: "Path to the directory containing the 'goenv' executable" - value: ${{ steps.go.outputs.goenv-bin-dir }} - go: - description: "Path to the 'go' executable" - value: ${{ steps.go.outputs.go }} -runs: - using: "composite" - steps: - # We use goenv to make sure we're always using the same Go version we'd - # use for releases, as recorded in the .go-version file. - - name: "Install Go" - id: go - shell: bash - run: | - git clone https://github.com/syndbg/goenv.git ~/.goenv - # "install" makes sure we have the .go-version-selected toolchain - # available in goenv's repository of toolchains. - # (this creates ~/.goenv/versions/VERSION/... to serve as GOROOT) - ~/.goenv/bin/goenv install - # "rehash" generates the wrapper scripts that goenv uses to - # select the correct executables for the selected toolchain. - # (this is what creates the ~/.goenv/shims/go we refer to below) - ~/.goenv/bin/goenv rehash - echo "::set-output name=go-version::$(~/.goenv/bin/goenv version-name)" - echo "::set-output name=gopath::$HOME/go/$(~/.goenv/bin/goenv version-name)" - echo "::set-output name=goenv::$HOME/.goenv/bin/goenv" - echo "::set-output name=goenv-bin-dir::$HOME/.goenv/bin" - echo "::set-output name=go::$HOME/.goenv/shims/go" diff --git a/.github/actions/go-version/action.yml b/.github/actions/go-version/action.yml new file mode 100644 index 0000000000..9c12343b93 --- /dev/null +++ b/.github/actions/go-version/action.yml @@ -0,0 +1,23 @@ +name: 'Determine Go Toolchain Version' +description: 'Uses the .go-version file to determine which Go toolchain to use for any Go-related actions downstream.' +outputs: + version: + description: "Go toolchain version" + value: ${{ steps.go.outputs.version }} +runs: + using: "composite" + steps: + # We use goenv to make sure we're always using the same Go version we'd + # use for releases, as recorded in the .go-version file. + - name: "Determine Go version" + id: go + shell: bash + # We use .go-version as our source of truth for current Go + # version, because "goenv" can react to it automatically. + # However, we don't actually use goenv for our automated + # steps in GitHub Actions, because it's primarily for + # interactive use in shells and makes things unnecessarily + # complex for automation. + run: | + echo "Building with Go $(cat .go-version)" + echo "::set-output name=version::$(cat .go-version)" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index be0985c26a..56e02ca8b9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -68,17 +68,13 @@ jobs: name: "Determine Go toolchain version" runs-on: ubuntu-latest outputs: - go-version: ${{ steps.get-go-version.outputs.go-version }} + go-version: ${{ steps.get-go-version.outputs.version }} steps: - uses: actions/checkout@v2 - name: Determine Go version id: get-go-version - # We use .go-version as our source of truth for current Go - # version, because "goenv" can react to it automatically. - run: | - echo "Building with Go $(cat .go-version)" - echo "::set-output name=go-version::$(cat .go-version)" + uses: ./.github/actions/go-version generate-metadata-file: name: "Generate release metadata" diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 47c4b90f71..4e3d7ea401 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -33,23 +33,28 @@ jobs: - name: "Fetch source code" uses: actions/checkout@v2 - - name: "Install Go" + - name: Determine Go version id: go - uses: ./.github/actions/go-setup + uses: ./.github/actions/go-version + + - name: Install Go toolchain + uses: actions/setup-go@v2 + with: + go-version: ${{ steps.go.outputs.version }} # NOTE: This cache is shared with the e2e-tests job, so the corresponding # step in that job must always be identical to this one. - name: Cache Go modules uses: actions/cache@v3 with: - path: "${{steps.go.outputs.gopath}}/pkg" + path: "~/go/pkg" key: go-mod-${{ hashFiles('go.sum') }} restore-keys: | go-mod- - name: "go.mod and go.sum consistency check" run: | - "${{steps.go.outputs.go}}" mod tidy + go mod tidy if [[ -n "$(git status --porcelain)" ]]; then echo "go.mod/go.sum are not up-to-date. Run 'go mod tidy' and then commit the updated files." exit 1 @@ -57,7 +62,7 @@ jobs: - name: "Unit tests" run: | - "${{steps.go.outputs.go}}" test ./... + go test ./... # We'll also deal with some general consistency/lint/style checks # here just because they are relatively fast and so not worth running @@ -65,12 +70,6 @@ jobs: # out the source code in another worker. - name: "Code consistency checks" run: | - # We do need to fully init goenv in this one case because we're - # going to run Go indirectly via make and some shell scripts, so - # we need PATH set up correctly. - export PATH="${{steps.go.outputs.goenv-bin-dir}}":$PATH - eval "$(goenv init -)" - go version make fmtcheck generate staticcheck exhaustive protobuf if [[ -n "$(git status --porcelain)" ]]; then echo "Generated files are inconsistent. Run 'make generate' and 'make protobuf' locally and then commit the updated files." @@ -90,20 +89,25 @@ jobs: - name: "Fetch source code" uses: actions/checkout@v2 - - name: "Install Go" + - name: Determine Go version id: go - uses: ./.github/actions/go-setup + uses: ./.github/actions/go-version + + - name: Install Go toolchain + uses: actions/setup-go@v2 + with: + go-version: ${{ steps.go.outputs.version }} # NOTE: This cache is shared with the e2e-tests job, so the corresponding # step in that job must always be identical to this one. - name: Cache Go modules uses: actions/cache@v3 with: - path: "${{steps.go.outputs.gopath}}/pkg" + path: "~/go/pkg" key: go-mod-${{ hashFiles('go.sum') }} restore-keys: | go-mod- - name: "End-to-end tests" run: | - TF_ACC=1 "${{steps.go.outputs.go}}" test -v ./internal/command/e2etest + TF_ACC=1 go test -v ./internal/command/e2etest