From e2648edd9a9a5520846db086c262b3b2c0c99205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eike=20Ha=C3=9F?= Date: Thu, 16 Sep 2021 10:31:46 +0200 Subject: [PATCH] Add composite action to CI (#392) --- .github/actions/rust/rust-setup/action.yml | 72 +++++++ .../sccache/setup-sccache-macos/action.yml | 16 ++ .../sccache/setup-sccache-ubuntu/action.yml | 27 +++ .../sccache/setup-sccache-windows/action.yml | 17 ++ .../stop-sccache-ubuntu-macos/action.yml | 8 + .../sccache/stop-sccache-windows/action.yml | 8 + .../get-current-date-ubuntu-macos/action.yml | 8 + .../utils/get-current-date-windows/action.yml | 8 + .github/workflows/build-and-test.yml | 204 ++++-------------- 9 files changed, 208 insertions(+), 160 deletions(-) create mode 100644 .github/actions/rust/rust-setup/action.yml create mode 100644 .github/actions/rust/sccache/setup-sccache-macos/action.yml create mode 100644 .github/actions/rust/sccache/setup-sccache-ubuntu/action.yml create mode 100644 .github/actions/rust/sccache/setup-sccache-windows/action.yml create mode 100644 .github/actions/rust/sccache/stop-sccache-ubuntu-macos/action.yml create mode 100644 .github/actions/rust/sccache/stop-sccache-windows/action.yml create mode 100644 .github/actions/utils/get-current-date-ubuntu-macos/action.yml create mode 100644 .github/actions/utils/get-current-date-windows/action.yml diff --git a/.github/actions/rust/rust-setup/action.yml b/.github/actions/rust/rust-setup/action.yml new file mode 100644 index 0000000000..a7f9bcffdf --- /dev/null +++ b/.github/actions/rust/rust-setup/action.yml @@ -0,0 +1,72 @@ +name: 'rust-setup' +description: 'Prepares a rust environment and relevant caches.' +inputs: + sccache-path: + description: 'Path of the sccache.' + required: true + os: + description: 'OS of the runner, used for cache key construction.' + required: true + job: + description: 'Name of the job, used for cache key construction.' + required: true + current-date: + description: 'Current date, used for cache key construction.' + required: true + target-cache-path: + description: 'Path of the target cache.' + required: false + default: target +runs: + using: "composite" + steps: + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + + - name: Cache cargo + uses: actions/cache@v2.1.4 + with: + # https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + # Add date to the cache to keep it up to date + key: ${{ inputs.os }}-cargo-${{ inputs.job }}-${{ hashFiles('**/Cargo.toml') }}-${{ inputs.current-date }} + # Restore from outdated cache for speed + restore-keys: | + ${{ inputs.os }}-cargo-${{ inputs.job }}-${{ hashFiles('**/Cargo.toml') }}- + ${{ inputs.os }}-cargo-${{ inputs.job }}- + ${{ inputs.os }}-cargo- + + # Generate Cargo.lock files for build, sccache cache keys. + # Allows dependencies updated on crates.io between runs to trigger storing an updated cache, + # which hashing Cargo.toml files alone does not. + - name: Cargo update + uses: actions-rs/cargo@v1 + with: + command: update + + - name: Cache build target + uses: actions/cache@v2.1.4 + with: + path: ${{ inputs.target-cache-path }} + # Add date to the cache to keep it up to date + key: ${{ inputs.os }}-target-${{ inputs.job }}-${{ hashFiles('**/Cargo.lock') }} + # Restore from outdated cache for speed + restore-keys: | + ${{ inputs.os }}-target-${{ inputs.job }}- + ${{ inputs.os }}-target- + + - name: Cache sccache + uses: actions/cache@v2.1.6 + with: + path: ${{ inputs.sccache-path }} + key: ${{ inputs.os }}-sccache-${{ inputs.job }}-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ inputs.os }}-sccache-${{ inputs.job }}- + ${{ inputs.os }}-sccache- \ No newline at end of file diff --git a/.github/actions/rust/sccache/setup-sccache-macos/action.yml b/.github/actions/rust/sccache/setup-sccache-macos/action.yml new file mode 100644 index 0000000000..d67ba271a6 --- /dev/null +++ b/.github/actions/rust/sccache/setup-sccache-macos/action.yml @@ -0,0 +1,16 @@ +name: 'setup-sccache-macos' +description: 'Setup sccache for macos.' +runs: + using: "composite" + steps: + - name: Install sccache (macos-latest) + shell: sh + run: | + brew update --preinstall + brew install sccache + + - name: Start sccache + shell: sh + run: | + sccache --start-server + sccache -s \ No newline at end of file diff --git a/.github/actions/rust/sccache/setup-sccache-ubuntu/action.yml b/.github/actions/rust/sccache/setup-sccache-ubuntu/action.yml new file mode 100644 index 0000000000..f89f6dddbb --- /dev/null +++ b/.github/actions/rust/sccache/setup-sccache-ubuntu/action.yml @@ -0,0 +1,27 @@ +name: 'setup-sccache-ubuntu' +description: 'Setup sccache for ubuntu.' +runs: + using: "composite" + steps: + - name: Install sccache (macos-latest) + shell: sh + run: | + SCCACHE_DOWNLOAD_LINK=https://github.com/mozilla/sccache/releases/download + SCCACHE_VERSION=v0.2.15 + SCCACHE_PREFIX="sccache-$SCCACHE_VERSION-x86_64-unknown-linux-musl" + SCCACHE_TAR="${SCCACHE_PREFIX}.tar.gz" + DOWNLOAD_LINK="${SCCACHE_DOWNLOAD_LINK}/${SCCACHE_VERSION}/${SCCACHE_TAR}" + curl -L "${DOWNLOAD_LINK}" --output ${SCCACHE_TAR} + echo "$(curl -L ${DOWNLOAD_LINK}.sha256) ${SCCACHE_TAR}" | shasum -a 256 --check --status + tar xzf ${SCCACHE_TAR} + BIN_DIR="$HOME/.local/bin" + mkdir -p ${BIN_DIR} + mv -f ${SCCACHE_PREFIX}/sccache ${BIN_DIR}/sccache + chmod a+x "${BIN_DIR}/sccache" + echo ${BIN_DIR} >> $GITHUB_PATH + + - name: Start sccache + shell: sh + run: | + sccache --start-server + sccache -s \ No newline at end of file diff --git a/.github/actions/rust/sccache/setup-sccache-windows/action.yml b/.github/actions/rust/sccache/setup-sccache-windows/action.yml new file mode 100644 index 0000000000..9c0a237635 --- /dev/null +++ b/.github/actions/rust/sccache/setup-sccache-windows/action.yml @@ -0,0 +1,17 @@ +name: 'setup-sccache-windows' +description: 'Setup sccache for windows.' +runs: + using: "composite" + steps: + - name: Install sccache (windows-latest) + shell: pwsh + run: | + Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh') + scoop install sccache + echo "${HOME}/scoop/apps/sccache/current" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + + - name: Start sccache + shell: pwsh + run: | + sccache --start-server + sccache -s \ No newline at end of file diff --git a/.github/actions/rust/sccache/stop-sccache-ubuntu-macos/action.yml b/.github/actions/rust/sccache/stop-sccache-ubuntu-macos/action.yml new file mode 100644 index 0000000000..416d0f5a25 --- /dev/null +++ b/.github/actions/rust/sccache/stop-sccache-ubuntu-macos/action.yml @@ -0,0 +1,8 @@ +name: 'stop-sccache-ubuntu-macos' +description: 'Stop sccache on ubuntu and macos.' +runs: + using: "composite" + steps: + - name: Stop sccache server + shell: sh + run: sccache --stop-server || true \ No newline at end of file diff --git a/.github/actions/rust/sccache/stop-sccache-windows/action.yml b/.github/actions/rust/sccache/stop-sccache-windows/action.yml new file mode 100644 index 0000000000..9a720e3103 --- /dev/null +++ b/.github/actions/rust/sccache/stop-sccache-windows/action.yml @@ -0,0 +1,8 @@ +name: 'stop-sccache-windows' +description: 'Stop sccache on windows.' +runs: + using: "composite" + steps: + - name: Stop sccache server + shell: pwsh + run: sccache --stop-server || true \ No newline at end of file diff --git a/.github/actions/utils/get-current-date-ubuntu-macos/action.yml b/.github/actions/utils/get-current-date-ubuntu-macos/action.yml new file mode 100644 index 0000000000..1cd76c005e --- /dev/null +++ b/.github/actions/utils/get-current-date-ubuntu-macos/action.yml @@ -0,0 +1,8 @@ +name: 'get-current-date-ubuntu-macos' +description: 'Script for getting the current date on ubuntu and macos.' +runs: + using: "composite" + steps: + - name: Get current date + shell: sh + run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV \ No newline at end of file diff --git a/.github/actions/utils/get-current-date-windows/action.yml b/.github/actions/utils/get-current-date-windows/action.yml new file mode 100644 index 0000000000..373e7d2c0b --- /dev/null +++ b/.github/actions/utils/get-current-date-windows/action.yml @@ -0,0 +1,8 @@ +name: 'get-current-date-windows' +description: 'Script for getting the current date on windows.' +runs: + using: "composite" + steps: + - name: Get current date + shell: pwsh + run: echo "CURRENT_DATE=$(Get-Date -Format "yyyy-MM-dd")" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append \ No newline at end of file diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index ab62858b38..48fe814d74 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -16,8 +16,6 @@ on: env: RUST_BACKTRACE: full - SCCACHE_DOWNLOAD_LINK: https://github.com/mozilla/sccache/releases/download - SCCACHE_VERSION: v0.2.15 SCCACHE_CACHE_SIZE: 2G SCCACHE_IDLE_TIMEOUT: 0 # SCCACHE_RECACHE: 1 # uncomment to clear sccache cache, then re-comment @@ -43,99 +41,34 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Install stable toolchain - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - - - name: Get current date - if: matrix.os == 'macos-latest' || matrix.os == 'ubuntu-latest' - run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV - - - name: Get current date + - name: Get current date (Windows) + uses: './.github/actions/utils/get-current-date-windows' if: matrix.os == 'windows-latest' - run: echo "CURRENT_DATE=$(Get-Date -Format "yyyy-MM-dd")" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - - name: Cache cargo - uses: actions/cache@v2.1.4 - with: - # https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - # Add date to the cache to keep it up to date - key: ${{ matrix.os }}-cargo-${{ github.job }}-${{ hashFiles('**/Cargo.toml') }}-${{ env.CURRENT_DATE }} - # Restore from outdated cache for speed - restore-keys: | - ${{ matrix.os }}-cargo-${{ github.job }}-${{ hashFiles('**/Cargo.toml') }}- - ${{ matrix.os }}-cargo-${{ github.job }}- - ${{ matrix.os }}-cargo- - - # Generate Cargo.lock files for build, sccache cache keys. - # Allows dependencies updated on crates.io between runs to trigger storing an updated cache, - # which hashing Cargo.toml files alone does not. - - name: Cargo update - uses: actions-rs/cargo@v1 - with: - command: update - - - name: Cache build target - uses: actions/cache@v2.1.4 - with: - path: target - # Add date to the cache to keep it up to date - key: ${{ matrix.os }}-target-${{ github.job }}-${{ hashFiles('**/Cargo.lock') }} - # Restore from outdated cache for speed - restore-keys: | - ${{ matrix.os }}-target-${{ github.job }}- - ${{ matrix.os }}-target- + - name: Get current date (Ubuntu/MacOS) + uses: './.github/actions/utils/get-current-date-ubuntu-macos' + if: matrix.os == 'macos-latest' || matrix.os == 'ubuntu-latest' - - name: Cache sccache - uses: actions/cache@v2.1.6 - continue-on-error: false + - name: Setup Rust and cache + uses: './.github/actions/rust/rust-setup' with: - path: ${{ matrix.sccache-path }} - key: ${{ runner.os }}-sccache-${{ github.job }}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-sccache-${{ github.job }}- - ${{ runner.os }}-sccache- + os: ${{ runner.os }} + job: ${{ github.job }} + sccache-path: ${{ matrix.sccache-path }} + current-date: ${{ env.CURRENT_DATE }} - - name: Install sccache (ubuntu-latest) + - name: Setup sccache (Ubuntu) + uses: './.github/actions/rust/sccache/setup-sccache-ubuntu' if: matrix.os == 'ubuntu-latest' - run: | - SCCACHE_PREFIX="sccache-$SCCACHE_VERSION-x86_64-unknown-linux-musl" - SCCACHE_TAR="${SCCACHE_PREFIX}.tar.gz" - DOWNLOAD_LINK="${SCCACHE_DOWNLOAD_LINK}/${SCCACHE_VERSION}/${SCCACHE_TAR}" - curl -L "${DOWNLOAD_LINK}" --output ${SCCACHE_TAR} - echo "$(curl -L ${DOWNLOAD_LINK}.sha256) ${SCCACHE_TAR}" | shasum -a 256 --check --status - tar xzf ${SCCACHE_TAR} - BIN_DIR="$HOME/.local/bin" - mkdir -p ${BIN_DIR} - mv -f ${SCCACHE_PREFIX}/sccache ${BIN_DIR}/sccache - chmod a+x "${BIN_DIR}/sccache" - echo ${BIN_DIR} >> $GITHUB_PATH - - - name: Install sccache (macos-latest) - if: matrix.os == 'macos-latest' - run: | - brew update --preinstall - brew install sccache - - - name: Install sccache (windows-latest) + + - name: Setup sccache (Windows) + uses: './.github/actions/rust/sccache/setup-sccache-windows' if: matrix.os == 'windows-latest' - run: | - Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh') - scoop install sccache - echo "${HOME}/scoop/apps/sccache/current" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append - - - name: Start sccache - run: | - sccache --start-server - sccache -s + - name: Setup sccache (MacOS) + uses: './.github/actions/rust/sccache/setup-sccache-macos' + if: matrix.os == 'macos-latest' + - name: Build uses: actions-rs/cargo@v1 with: @@ -149,8 +82,13 @@ jobs: command: test args: --all --release - - name: Stop sccache server - run: sccache --stop-server || true + - name: Stop sccache (Windows) + uses: './.github/actions/rust/sccache/stop-sccache-windows' + if: matrix.os == 'windows-latest' + + - name: Stop sccache (Ubuntu/MacOS) + uses: './.github/actions/rust/sccache/stop-sccache-ubuntu-macos' + if: matrix.os == 'macos-latest' || matrix.os == 'ubuntu-latest' build-and-test-libjose: runs-on: ubuntu-latest @@ -199,83 +137,28 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Install stable toolchain - uses: actions-rs/toolchain@v1 + - name: Get current date (Ubuntu/MacOS) + uses: './.github/actions/utils/get-current-date-ubuntu-macos' + if: matrix.os == 'macos-latest' || matrix.os == 'ubuntu-latest' + + - uses: './.github/actions/rust/rust-setup' + name: rust-setup with: - toolchain: stable - override: true + os: ${{ runner.os }} + job: ${{ github.job }} + sccache-path: ${{ matrix.sccache-path }} + current-date: ${{ env.CURRENT_DATE }} + target-cache-path: bindings/wasm/target - name: Install WASM toolchain uses: actions-rs/toolchain@v1 with: toolchain: stable target: wasm32-unknown-unknown - - - name: Cache cargo - uses: actions/cache@v2.1.4 - with: - # https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - # Add date to the cache to keep it up to date - key: ${{ matrix.os }}-cargo-${{ github.job }}-${{ hashFiles('**/Cargo.toml') }}-${{ env.CURRENT_DATE }} - # Restore from outdated cache for speed - restore-keys: | - ${{ matrix.os }}-cargo-${{ github.job }}-${{ hashFiles('**/Cargo.toml') }}- - ${{ matrix.os }}-cargo-${{ github.job }}- - ${{ matrix.os }}-cargo- - - # Generate Cargo.lock files for build, sccache cache keys. - # Allows dependencies updated on crates.io between runs to trigger storing an updated cache, - # which hashing Cargo.toml files alone does not. - - name: Cargo update - uses: actions-rs/cargo@v1 - with: - command: update - - - name: Cache wasm target - uses: actions/cache@v2.1.4 - with: - path: bindings/wasm/target - # Add date to the cache to keep it up to date - key: ${{ matrix.os }}-wasm-target-${{ github.job }}-${{ hashFiles('**/Cargo.lock') }} - # Restore from outdated cache for speed - restore-keys: | - ${{ matrix.os }}-wasm-target-${{ github.job }}- - ${{ matrix.os }}-wasm-target- - - - name: Cache sccache - uses: actions/cache@v2.1.6 - continue-on-error: false - with: - path: ${{ matrix.sccache-path }} - key: ${{ runner.os }}-sccache-${{ github.job }}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-sccache-${{ github.job }}- - ${{ runner.os }}-sccache- - - - name: Install sccache (ubuntu-latest) + + - name: Setup sccache (Ubuntu) + uses: './.github/actions/rust/sccache/setup-sccache-ubuntu' if: matrix.os == 'ubuntu-latest' - run: | - SCCACHE_PREFIX="sccache-$SCCACHE_VERSION-x86_64-unknown-linux-musl" - SCCACHE_TAR="${SCCACHE_PREFIX}.tar.gz" - DOWNLOAD_LINK="${SCCACHE_DOWNLOAD_LINK}/${SCCACHE_VERSION}/${SCCACHE_TAR}" - curl -L "${DOWNLOAD_LINK}" --output ${SCCACHE_TAR} - echo "$(curl -L ${DOWNLOAD_LINK}.sha256) ${SCCACHE_TAR}" | shasum -a 256 --check --status - tar xzf ${SCCACHE_TAR} - BIN_DIR="$HOME/.local/bin" - mkdir -p ${BIN_DIR} - mv -f ${SCCACHE_PREFIX}/sccache ${BIN_DIR}/sccache - chmod a+x "${BIN_DIR}/sccache" - echo ${BIN_DIR} >> $GITHUB_PATH - - - name: Start sccache - run: | - sccache --start-server - sccache -s - name: Set up Node.js uses: actions/setup-node@v1 @@ -307,5 +190,6 @@ jobs: run: wasm-pack test --node working-directory: bindings/wasm - - name: Stop sccache server - run: sccache --stop-server || true + - name: Stop sccache (Ubuntu/MacOS) + uses: './.github/actions/rust/sccache/stop-sccache-ubuntu-macos' + if: matrix.os == 'macos-latest' || matrix.os == 'ubuntu-latest'