Skip to content

Commit

Permalink
ci(caching): make caching more effective (#3215)
Browse files Browse the repository at this point in the history
Currently, we create a new cache for each workflow run for each crate. That ends up blowing the maximum allowed cache size of 10GB and GitHub deletes the least-recently used cache again. Effectively, this means we don't have any caching.

This patch introduces a cache factory workflow that only runs on master and always _saves_ a new cache. The CI workflow run for pull-requests on the other hand only restore these caches but don't save them.
  • Loading branch information
thomaseizinger authored Dec 13, 2022
1 parent f80c714 commit 5fe0dc4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
78 changes: 78 additions & 0 deletions .github/workflows/cache-factory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# This workflow _produces_ caches which are used to speed up pull request builds.
# The caches are split by Rust version (stable vs MSRV per crate) because those caches cannot share any artifacts.

name: Cache factory

on:
push:
branches:
- master # Caches are only created on master branch.

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
gather_msrv_versions:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.find-rust-versions.outputs.versions }}
steps:
- uses: actions/checkout@v3

- id: find-rust-versions
run: |
RUST_VERSIONS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | map(.rust_version) | unique | del(..|nulls)')
echo "versions=${RUST_VERSIONS}" >> $GITHUB_OUTPUT
make_msrv_cache:
runs-on: ubuntu-latest
needs: gather_msrv_versions
strategy:
fail-fast: false
matrix:
rust: ${{ fromJSON(needs.gather_msrv_versions.outputs.versions) }}
steps:
- name: Install Protoc
uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2

- uses: actions/checkout@v3

- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
with:
toolchain: ${{ matrix.rust }}

- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
shared-key: msrv-cache

- name: Compile all crates which have MSRV ${{ matrix.rust }}
run: |
cargo metadata --format-version=1 --no-deps | \
jq -r '.packages[] | select(.rust_version == "${{ matrix.rust }}") | "+\(.rust_version) build --all-features --package \(.name)"' |
xargs --verbose -L 1 cargo
make_stable_rust_cache:
runs-on: ubuntu-latest
steps:
- name: Install Protoc
uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2

- uses: actions/checkout@v3

- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
with:
toolchain: stable

- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
shared-key: stable-cache

- name: Compile workspace with stable Rust
run: cargo test --all-features --all-targets --workspace --no-run

- name: Render docs
run: cargo doc --all-features --workspace

- name: Install tools
run: cargo install cargo-semver-checks --locked
29 changes: 15 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,33 @@ jobs:
with:
profile: minimal
toolchain: ${{ steps.parse-msrv.outputs.version }}
override: true

- name: Update to latest stable Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
shared-key: msrv-cache
save-if: false

- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
run: cargo +${{ steps.parse-msrv.outputs.version }} build --package ${{ matrix.crate }} --all-features

- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
with:
profile: minimal
toolchain: stable
override: true

# By default, this action already includes the active Rust toolchain in the cache key.
# We also install a separate toolchain for the MSRV check so all we need to do is add that to the key to make sure it invalidates when we update the MSRV.
# cargo separates build artifacts by Rust compiler version, meaning we can compile with different versions but cache all artifacts.
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
key: ${{ matrix.crate }}-msrv-${{ steps.parse-msrv.outputs.version }}

- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
run: cargo +${{ steps.parse-msrv.outputs.version }} check --package ${{ matrix.crate }} --all-features

- name: Check if we compile without any features activated
run: cargo check --package ${{ matrix.crate }} --no-default-features
shared-key: stable-cache
save-if: false

- name: Run all tests
run: cargo test --package ${{ matrix.crate }} --all-features

- name: Check if we compile without any features activated
run: cargo build --package ${{ matrix.crate }} --no-default-features

- name: Check if crate has been released
id: check-released
run: |
Expand Down Expand Up @@ -138,8 +141,6 @@ jobs:
override: true

- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
with:
key: ${{ matrix.runtime }}

- run: cargo check --package libp2p --features="${{ matrix.features }}"

Expand Down

0 comments on commit 5fe0dc4

Please sign in to comment.