From 0083ced1eed38cf45b9d95a7ad4ab8dab7efee1f Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 20 Jun 2024 18:16:56 +0200 Subject: [PATCH] Refactor GitHub setup-go action (#227) --- .github/actions/setup-go/action.yml | 90 +++++++++++++------------- .github/workflows/dependency-check.yml | 12 ++-- .github/workflows/e2e.yml | 18 +++--- .github/workflows/golang.yml | 9 +-- .github/workflows/rust.yml | 2 +- .github/workflows/soroban-rpc.yml | 23 ++----- 6 files changed, 70 insertions(+), 84 deletions(-) diff --git a/.github/actions/setup-go/action.yml b/.github/actions/setup-go/action.yml index 1bfd4a41..2d157acc 100644 --- a/.github/actions/setup-go/action.yml +++ b/.github/actions/setup-go/action.yml @@ -1,55 +1,55 @@ name: 'Setup the Go environment' description: 'Installs go and restores/saves the build/module cache' -inputs: - go-version: - required: true runs: using: "composite" steps: - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: ${{ inputs.go-version }} - stable: ${{ !(contains(inputs.go-version, 'beta') || contains(inputs.go-version, 'rc')) }} + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.22' + # unfortunately we cannot use the provided caching because it uses the + # same cache for all workflows/jobs, leading to undesired cache clashes, + # causing uncached test runs etc ... + # You can see the cache key at https://github.com/actions/setup-go/blob/4e0b6c77c6448caafaff5eed51516cad78e7639a/src/cache-restore.ts#L34 + # There is a ticket to add prefixes for the cache, which should solve it https://github.com/actions/setup-go/issues/358 + cache: false - # Restore original modification time of files based on the date of the most - # recent commit that modified them as mtimes affect the Go test cache. - - name: Restore modification time of checkout files - shell: bash - run: | - # Set a base, fixed modification time of all directories. - # git-restore-mtime doesn't set the mtime of all directories. - # (see https://github.com/MestreLion/git-tools/issues/47 for details) - touch -m -t '201509301646' $(find . -type d -not -path '.git/*') - # Restore original modification time from git. git clone sets the - # modification time to the current time, but Go tests that access fixtures - # get invalidated if their modification times change. - sudo apt-get install -y git-restore-mtime - git restore-mtime + # Restore original modification time of files based on the date of the most + # recent commit that modified them as mtimes affect the Go test cache. + # See https://github.com/golang/go/issues/58571 for details + - name: Restore modification time of checkout files + uses: chetan/git-restore-mtime-action@075f9bc9d159805603419d50f794bd9f33252ebe - # The PREFIX must uniquely identify the specific instance of a job executing. - - shell: bash - run: echo 'PREFIX=${{ github.workflow }}-${{ github.job }}-${{ runner.os }}-${{ inputs.go-version }}-matrix(${{ join(matrix.*,'|') }})' >> $GITHUB_ENV - # Cache the Go Modules downloaded during the job. - - uses: actions/cache@v2 - with: - path: ~/go/pkg/mod - key: ${{ env.PREFIX }}-go-mod-${{ hashFiles('**/go.sum') }} - restore-keys: ${{ env.PREFIX }}-go-mod- + # KEY_PREFIX must uniquely identify the specific instance of a job executing. + - shell: bash + run: | + echo 'KEY_PREFIX=${{ github.workflow }}-${{ github.job }}-${{ runner.os }}-${{ inputs.go-version }}-matrix(${{ join(matrix.*,'|') }})' >> $GITHUB_OUTPUT + echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT + echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT + if [ ${{ runner.os }} != 'Windows' ]; then echo SUDO='sudo' >> $GITHUB_OUTPUT; fi + id: variables - # Cache any build and test artifacts during the job, which will speed up - # rebuilds and cause test runs to skip tests that have no reason to rerun. - - uses: actions/cache@v2 - with: - path: ~/.cache/go-build - key: ${{ env.PREFIX }}-go-build-${{ github.ref }}-${{ hashFiles('**', '!.git') }} - restore-keys: | - ${{ env.PREFIX }}-go-build-${{ github.ref }}- - ${{ env.PREFIX }}-go-build- + # Cache the Go Modules downloaded during the job. + - uses: actions/cache@v4 + with: + path: ${{ steps.variables.outputs.GOMODCACHE }} + key: ${{ steps.variables.outputs.KEY_PREFIX }}-go-mod-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ steps.variables.outputs.KEY_PREFIX }}-go-mod- + + # Cache any build and test artifacts during the job, which will speed up + # rebuilds and cause test runs to skip tests that have no reason to rerun. + - uses: actions/cache@v4 + with: + path: ${{ steps.variables.outputs.GOCACHE }} + key: ${{ steps.variables.outputs.KEY_PREFIX }}-go-build-${{ github.ref }}-${{ hashFiles('**', '!.git') }} + restore-keys: | + ${{ steps.variables.outputs.KEY_PREFIX }}-go-build-${{ github.ref }}- + ${{ steps.variables.outputs.KEY_PREFIX }}-go-build- + + # Reset the cache for master/protected branches, to ensure they build and run the tests from zero + # and that the module cache is cleaned (otherwise it accumulates orphan dependencies over time). + - if: github.ref_protected + shell: bash + run: ${{ steps.variables.outputs.SUDO }} rm -rf ${{ steps.variables.outputs.GOMODCACHE }} ${{ steps.variables.outputs.GOCACHE }} - # Reset the cache for master/protected branches, to ensure they build and run the tests from zero - # and that the module cache is cleaned (otherwise it accumulates orphan dependencies over time). - - if: github.ref_protected - shell: bash - run: sudo rm -rf ~/.cache/go-build ~/go/pkg/mod diff --git a/.github/workflows/dependency-check.yml b/.github/workflows/dependency-check.yml index 79a14aa7..dc865935 100644 --- a/.github/workflows/dependency-check.yml +++ b/.github/workflows/dependency-check.yml @@ -2,7 +2,7 @@ name: Dependency sanity checker on: push: - branches: [main, release/**] + branches: [ main, release/** ] pull_request: defaults: @@ -13,14 +13,14 @@ jobs: dependency-sanity-checker: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: rustup update - - uses: actions/setup-go@v3 - with: - go-version: 1.22 + - uses: stellar/actions/rust-cache@main + - uses: ./.github/actions/setup-go - run: scripts/check-dependencies.bash + validate-rust-git-rev-deps: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: stellar/actions/rust-check-git-rev-deps@main diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index ce1677ad..f14121e9 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -2,7 +2,7 @@ name: Soroban Tools e2e on: push: - branches: [main, release/**] + branches: [ main, release/** ] pull_request: jobs: @@ -10,7 +10,7 @@ jobs: name: System tests strategy: matrix: - scenario-filter: ["^TestDappDevelop$/^.*$"] + scenario-filter: [ "^TestDappDevelop$/^.*$" ] runs-on: ubuntu-latest-4-cores env: # the gh tag of system-test repo version to run @@ -28,7 +28,7 @@ jobs: # or set SYSTEM_TEST_CORE_GIT_REF to empty, and set SYSTEM_TEST_CORE_IMAGE # to pull a pre-compiled image from dockerhub instead SYSTEM_TEST_CORE_IMAGE: stellar/stellar-core:20 - SYSTEM_TEST_CORE_IMAGE_BIN_PATH: /usr/bin/stellar-core + SYSTEM_TEST_CORE_IMAGE_BIN_PATH: /usr/bin/stellar-core # sets the version of rust toolchain that will be pre-installed in the # test runtime environment, tests invoke rustc/cargo @@ -42,8 +42,8 @@ jobs: # option #2, set the version of stellar-sdk used as a ref to a gh repo if # a value is set on SYSTEM_TEST_JS_STELLAR_SDK_GH_REPO, it takes # precedence over any SYSTEM_TEST_JS_STELLAR_SDK_NPM_VERSION - SYSTEM_TEST_JS_STELLAR_SDK_GH_REPO: - SYSTEM_TEST_JS_STELLAR_SDK_GH_REF: + SYSTEM_TEST_JS_STELLAR_SDK_GH_REPO: + SYSTEM_TEST_JS_STELLAR_SDK_GH_REF: # the version of rs-stellar-xdr to use for quickstart SYSTEM_TEST_RS_XDR_GIT_REF: v20.0.2 @@ -59,18 +59,18 @@ jobs: SYSTEM_TEST_SOROBAN_EXAMPLES_GIT_HASH: "v20.0.0" SYSTEM_TEST_SOROBAN_EXAMPLES_GIT_REPO: "https://github.com/stellar/soroban-examples.git" steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 name: checkout system-test with: repository: stellar/system-test ref: ${{ env.SYSTEM_TEST_GIT_REF }} path: system-test - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 name: checkout soroban-tools with: repository: stellar/soroban-tools path: soroban-tools - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 name: checkout soroban-rpc with: repository: stellar/soroban-rpc @@ -81,7 +81,7 @@ jobs: run: | rm -rf $GITHUB_WORKSPACE/system-test/js-stellar-sdk; - if: ${{ env.SYSTEM_TEST_JS_STELLAR_SDK_GH_REPO != ''}} - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: repository: ${{ env.SYSTEM_TEST_JS_STELLAR_SDK_GH_REPO }} ref: ${{ env.SYSTEM_TEST_JS_STELLAR_SDK_GH_REF }} diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml index a9726b9e..21db63bb 100644 --- a/.github/workflows/golang.yml +++ b/.github/workflows/golang.yml @@ -8,7 +8,7 @@ permissions: contents: read # Optional: allow read access to pull request. Use with `only-new-issues` option. pull-requests: read - # Optional: allow write access to checks to allow the action to annotate code in the PR. + # Optional: allow the action to annotate code in the PR. checks: write jobs: @@ -16,14 +16,11 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # version v3.0.2 + uses: actions/checkout@v4 with: fetch-depth: 0 # required for new-from-rev option in .golangci.yml - - name: Setup GO - uses: actions/setup-go@268d8c0ca0432bb2cf416faae41297df9d262d7f # version v3.3.0 - with: - go-version: '>=1.22.1' + - uses: ./.github/actions/setup-go - uses: stellar/actions/rust-cache@main - name: Build libpreflight diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6f9dd3fa..6b1cb36a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,7 +13,7 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: rustup update - uses: stellar/actions/rust-cache@main - run: make rust-check diff --git a/.github/workflows/soroban-rpc.yml b/.github/workflows/soroban-rpc.yml index e9122e82..7edfe2e3 100644 --- a/.github/workflows/soroban-rpc.yml +++ b/.github/workflows/soroban-rpc.yml @@ -15,10 +15,9 @@ jobs: strategy: matrix: os: [ ubuntu-20.04, ubuntu-22.04 ] - go: [ 1.22 ] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: # For pull requests, build and test the PR head not a merge of the PR with the destination. ref: ${{ github.event.pull_request.head.sha || github.ref }} @@ -26,12 +25,10 @@ jobs: # Otherwise, the Go test cache will fail (due to the modification time of fixtures changing). fetch-depth: "0" - uses: ./.github/actions/setup-go - with: - go-version: ${{ matrix.go }} - run: rustup update - uses: stellar/actions/rust-cache@main - run: make build-libpreflight - - run: go test -race -cover -timeout 25m -v ./cmd/soroban-rpc/... + - run: go test -race -timeout 25m ./cmd/soroban-rpc/... build: name: Build @@ -55,13 +52,8 @@ jobs: go_arch: amd64 runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 - # we cannot use our own ./.github/actions/setup-go action - # because it uses apt-get and some OSs (e.g. windows) don't have it - - uses: actions/setup-go@v3 - with: - go-version: 1.22 - + - uses: actions/checkout@v4 + - uses: ./.github/actions/setup-go # On windows, make sure we have the same compiler (linker) used by rust. # This is important since the symbols names won't match otherwise. - if: matrix.os == 'windows-latest' @@ -102,7 +94,6 @@ jobs: strategy: matrix: os: [ ubuntu-20.04, ubuntu-22.04 ] - go: [ 1.22 ] protocol-version: [ 20, 21 ] runs-on: ${{ matrix.os }} env: @@ -114,7 +105,7 @@ jobs: PROTOCOL_21_CORE_DEBIAN_PKG_VERSION: 21.0.1-1897.dfd3dbff1.focal PROTOCOL_21_CORE_DOCKER_IMG: stellar/unsafe-stellar-core:21.0.1-1897.dfd3dbff1.focal steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: # For pull requests, build and test the PR head not a merge of the PR with the destination. ref: ${{ github.event.pull_request.head.sha || github.ref }} @@ -123,8 +114,6 @@ jobs: fetch-depth: "0" - uses: ./.github/actions/setup-go - with: - go-version: ${{ matrix.go }} - name: Pull and set Stellar Core image shell: bash @@ -179,4 +168,4 @@ jobs: - name: Run Soroban RPC Integration Tests run: | - go test -race -timeout 20m ./cmd/soroban-rpc/internal/integrationtest/... + go test -race -timeout 20m ./cmd/soroban-rpc/internal/integrationtest/...