From 7f828cd86dd83e7f2bc0d47b5e318a7a0e9ee4a4 Mon Sep 17 00:00:00 2001 From: junderw Date: Tue, 31 Oct 2023 22:41:10 -0700 Subject: [PATCH] Use Composite Action to reduce duplication --- .github/actions/ci-rust-setup/action.yml | 50 ++++++++++++++++++++++++ .github/workflows/ci.yml | 43 ++++++-------------- rust-toolchain | 1 + 3 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 .github/actions/ci-rust-setup/action.yml create mode 100644 rust-toolchain diff --git a/.github/actions/ci-rust-setup/action.yml b/.github/actions/ci-rust-setup/action.yml new file mode 100644 index 00000000..03b548bb --- /dev/null +++ b/.github/actions/ci-rust-setup/action.yml @@ -0,0 +1,50 @@ +name: CI Rust Setup +description: 'Sets up the environment for Rust jobs during CI workflow' + +inputs: + cache-name: + description: 'Name of cache artifacts (same name is same cache key) empty to disable cache' + required: false + targets: + description: 'A comma separated list of extra targets you want to install' + required: false + components: + description: 'A comma separated list of extra components you want to install' + required: false + toolchain: + description: 'The toolchain to use. If not specified, the rust-toolchain file will be used' + required: false + +runs: + using: composite + steps: + - name: Get toolchain from input OR rust-toolchain file + id: gettoolchain + shell: bash + run: |- + RUST_TOOLCHAIN="${{ inputs.toolchain }}" + if [ ! -f rust-toolchain ] && [ -z "${RUST_TOOLCHAIN}" ]; then + echo "***ERROR*** NEED toolchain INPUT OR rust-toolchain FILE IN ROOT OF REPOSITORY" >&2 + exit 1 + fi + if [ -z "${RUST_TOOLCHAIN}" ]; then + RUST_TOOLCHAIN="$(cat rust-toolchain)" + fi + echo "toolchain=\"${RUST_TOOLCHAIN}\"" >> $GITHUB_OUTPUT + - name: Install ${{ steps.gettoolchain.outputs.toolchain }} Rust toolchain + id: toolchain + # Commit date is Sep 19, 2023 + uses: dtolnay/rust-toolchain@439cf607258077187679211f12aa6f19af4a0af7 + with: + toolchain: ${{ steps.gettoolchain.outputs.toolchain }} + targets: ${{ inputs.targets }} + components: ${{ inputs.components }} + - name: Cache dependencies + uses: actions/cache@v3 + if: inputs.cache-name != '' + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ inputs.cache-name }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6b318ff2..c9ef33c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,16 +13,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - id: toolchain - uses: dtolnay/rust-toolchain@1.70 - - name: Cache dependencies - uses: actions/cache@v3 + - name: Setup Rust + uses: './.github/actions/ci-rust-setup' with: - path: | - ~/.cargo/registry - ~/.cargo/git - target - key: ${{ runner.os }}-cargo-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }} + cache-name: dev - run: cargo check --all-features fmt: @@ -30,8 +24,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - id: toolchain - uses: dtolnay/rust-toolchain@1.70 + - name: Setup Rust + uses: './.github/actions/ci-rust-setup' with: components: rustfmt - run: cargo fmt --all -- --check @@ -41,16 +35,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - id: toolchain - uses: dtolnay/rust-toolchain@1.70 - - name: Cache dependencies - uses: actions/cache@v3 - with: # test cache key is different (adding test cfg is a re-compile) - path: | - ~/.cargo/registry - ~/.cargo/git - target - key: ${{ runner.os }}-cargo-test-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }} + - name: Setup Rust + uses: './.github/actions/ci-rust-setup' + with: + cache-name: test - run: cargo test --lib --all-features clippy: @@ -67,17 +55,10 @@ jobs: ] steps: - uses: actions/checkout@v3 - - id: toolchain - uses: dtolnay/rust-toolchain@1.70 + - name: Setup Rust + uses: './.github/actions/ci-rust-setup' with: + cache-name: dev components: clippy - - name: Cache dependencies - uses: actions/cache@v3 - with: - path: | - ~/.cargo/registry - ~/.cargo/git - target - key: ${{ runner.os }}-cargo-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }} - name: Clippy with Features = ${{ matrix.features }} run: cargo clippy ${{ matrix.features }} -- -D warnings diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 00000000..bfe79d0b --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +1.70