Skip to content

Commit 5fe0dc4

Browse files
ci(caching): make caching more effective (#3215)
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.
1 parent f80c714 commit 5fe0dc4

File tree

2 files changed

+93
-14
lines changed

2 files changed

+93
-14
lines changed

.github/workflows/cache-factory.yml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# This workflow _produces_ caches which are used to speed up pull request builds.
2+
# The caches are split by Rust version (stable vs MSRV per crate) because those caches cannot share any artifacts.
3+
4+
name: Cache factory
5+
6+
on:
7+
push:
8+
branches:
9+
- master # Caches are only created on master branch.
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
gather_msrv_versions:
17+
runs-on: ubuntu-latest
18+
outputs:
19+
versions: ${{ steps.find-rust-versions.outputs.versions }}
20+
steps:
21+
- uses: actions/checkout@v3
22+
23+
- id: find-rust-versions
24+
run: |
25+
RUST_VERSIONS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | map(.rust_version) | unique | del(..|nulls)')
26+
echo "versions=${RUST_VERSIONS}" >> $GITHUB_OUTPUT
27+
28+
make_msrv_cache:
29+
runs-on: ubuntu-latest
30+
needs: gather_msrv_versions
31+
strategy:
32+
fail-fast: false
33+
matrix:
34+
rust: ${{ fromJSON(needs.gather_msrv_versions.outputs.versions) }}
35+
steps:
36+
- name: Install Protoc
37+
uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2
38+
39+
- uses: actions/checkout@v3
40+
41+
- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
42+
with:
43+
toolchain: ${{ matrix.rust }}
44+
45+
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
46+
with:
47+
shared-key: msrv-cache
48+
49+
- name: Compile all crates which have MSRV ${{ matrix.rust }}
50+
run: |
51+
cargo metadata --format-version=1 --no-deps | \
52+
jq -r '.packages[] | select(.rust_version == "${{ matrix.rust }}") | "+\(.rust_version) build --all-features --package \(.name)"' |
53+
xargs --verbose -L 1 cargo
54+
55+
make_stable_rust_cache:
56+
runs-on: ubuntu-latest
57+
steps:
58+
- name: Install Protoc
59+
uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # v1.1.2
60+
61+
- uses: actions/checkout@v3
62+
63+
- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
64+
with:
65+
toolchain: stable
66+
67+
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
68+
with:
69+
shared-key: stable-cache
70+
71+
- name: Compile workspace with stable Rust
72+
run: cargo test --all-features --all-targets --workspace --no-run
73+
74+
- name: Render docs
75+
run: cargo doc --all-features --workspace
76+
77+
- name: Install tools
78+
run: cargo install cargo-semver-checks --locked

.github/workflows/ci.yml

+15-14
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,33 @@ jobs:
3838
with:
3939
profile: minimal
4040
toolchain: ${{ steps.parse-msrv.outputs.version }}
41+
override: true
4142

42-
- name: Update to latest stable Rust
43-
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
43+
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
44+
with:
45+
shared-key: msrv-cache
46+
save-if: false
47+
48+
- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
49+
run: cargo +${{ steps.parse-msrv.outputs.version }} build --package ${{ matrix.crate }} --all-features
50+
51+
- uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1.0.7
4452
with:
4553
profile: minimal
4654
toolchain: stable
4755
override: true
4856

49-
# By default, this action already includes the active Rust toolchain in the cache key.
50-
# 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.
51-
# cargo separates build artifacts by Rust compiler version, meaning we can compile with different versions but cache all artifacts.
5257
- uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # v2.2.0
5358
with:
54-
key: ${{ matrix.crate }}-msrv-${{ steps.parse-msrv.outputs.version }}
55-
56-
- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
57-
run: cargo +${{ steps.parse-msrv.outputs.version }} check --package ${{ matrix.crate }} --all-features
58-
59-
- name: Check if we compile without any features activated
60-
run: cargo check --package ${{ matrix.crate }} --no-default-features
59+
shared-key: stable-cache
60+
save-if: false
6161

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

65+
- name: Check if we compile without any features activated
66+
run: cargo build --package ${{ matrix.crate }} --no-default-features
67+
6568
- name: Check if crate has been released
6669
id: check-released
6770
run: |
@@ -138,8 +141,6 @@ jobs:
138141
override: true
139142

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

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

0 commit comments

Comments
 (0)