11name : Cargo Cache
22description : |
3- Rust Cargo cache used only to build the test archive.
4- All crates should be cached based on runner OS and Cargo.lock hash.
5- This speeds up test archive generation but this cache should not be used for executing the tests.
6- See: https://github.com/actions/cache/blob/main/examples.md#rust---cargo
3+ Cargo crate cache management.
74
85inputs :
96 action :
10- description : " Type of operation (restore,save )"
7+ description : " Type of operation (check, restore,create )"
118 required : false
12- default : " "
13- cache-key :
14- description : " The cache key to use. Should be based on runner OS, Cargo.lock hash, and Cargo.toml hash."
15- required : true
9+ default : " check"
1610
1711outputs :
18- cache-hit :
19- description : " Cache Hit"
20- value : ${{ steps.check-cache.outputs.cache-hit }}
12+ cache-key :
13+ description : " Cache Key"
14+ value : ${{ steps.generate-keys.outputs.cargo-cache-key }}
15+ cache-matched-key :
16+ description : " Key of the cache that was checked or restored, it could either be the primary key on cache-hit or a partial/complete match of one of the restore keys."
17+ value : ${{ steps.check-cache.outputs.cache-matched-key || steps.restore-cache.outputs.cache-matched-key }}
2118
2219runs :
2320 using : " composite"
2421 steps :
25-
22+ # Generate cache keys
23+ - name : Generate Cache Keys
24+ id : generate-keys
25+ uses : ./.github/actions/cache/generate-keys
26+
2627 # Check if cache data exists
2728 - name : Check Cache
28- uses : actions/cache/restore@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
29+ if : |
30+ inputs.action == 'check'
31+ uses : actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
2932 id : check-cache
3033 with :
3134 lookup-only : true
@@ -35,34 +38,107 @@ runs:
3538 ~/.cargo/registry/cache/
3639 ~/.cargo/git/db/
3740 target/
38- key : ${{ inputs.cache-key }}
41+ key : ${{ steps.generate-keys.outputs.cargo-cache-key }}
42+ # Allows cache to "fallback" to `master` generated cache, using a prefix. The most recently created cache will be selected.
43+ restore-keys : ${{ steps.generate-keys.outputs.cargo-restore-key }}
3944
4045 # Restore cache data
4146 - name : Restore Cache
4247 if : |
43- inputs.action == 'restore' &&
44- steps.check- cache.outputs.cache-hit == 'true'
45- uses : actions/cache/ restore@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
48+ inputs.action == 'restore'
49+ uses : actions/ cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
50+ id : restore-cache
4651 with :
4752 path : |
4853 ~/.cargo/bin/
4954 ~/.cargo/registry/index/
5055 ~/.cargo/registry/cache/
5156 ~/.cargo/git/db/
5257 target/
53- key : ${{ inputs.cache-key }}
58+ key : ${{ steps.generate-keys.outputs.cargo-cache-key }}
59+ # Allows cache to "fallback" to base branch or `master`
60+ restore-keys : ${{ steps.generate-keys.outputs.cargo-restore-key }}
61+ fail-on-cache-miss : true
5462
63+ # Checkout the code
64+ - name : Checkout the latest code
65+ if : |
66+ inputs.action == 'create'
67+ uses : actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
68+ with :
69+ ref : ${{ github.ref }}
70+
71+ # Setup rust toolchain
72+ - name : Setup Rust Toolchain
73+ if : |
74+ inputs.action == 'create'
75+ uses : ./.github/actions/setup-rust-toolchain
76+ with :
77+ components : llvm-tools-preview
78+
79+ # Install nextest
80+ - name : Install nextest
81+ if : |
82+ inputs.action == 'create'
83+ uses : ./.github/actions/install-tool
84+ with :
85+ tools : nextest
86+
87+ # Install mold linker and clang (faster than default)
88+ - name : Install mold Linker (and clang)
89+ if : |
90+ inputs.action == 'create'
91+ shell : bash
92+ run : |
93+ sudo apt-get update
94+ sudo apt-get install -y mold clang
95+
96+ # Build the cargo crates
97+ - name : Build Cargo Crates
98+ if : |
99+ inputs.action == 'create'
100+ shell : bash
101+ env :
102+ RUSTFLAGS : " -C instrument-coverage -C linker=clang -C link-arg=-fuse-ld=mold -A warnings"
103+ CARGO_INCREMENTAL : 0
104+ # This command should be kept in sync with the same command in artifacts/nextest-archive action
105+ run : |
106+ cargo nextest archive \
107+ --workspace \
108+ --tests \
109+ --locked \
110+ --features monitoring_prom \
111+ --archive-file "not_used.tar.zst"
112+
113+ # Dynamic cleanup of local workspace crates right after building. We never want them to exist in the produced cache.
114+ # These are things like stacks-signer, stacks-inspect, etc. built locally from our codebase.
115+ # We must always compile them on-the-fly from current code, as they aren't bumped in version when they are changed.
116+ - name : Clean Local Workspace Crates
117+ if : |
118+ inputs.action == 'create'
119+ shell : bash
120+ run : |
121+ if [ -f Cargo.toml ]; then
122+ echo "Identifying local workspace crates to exclude from restored cache..."
123+ cargo metadata --format-version 1 --no-deps | jq -r '.packages[].name' | while read -r crate; do
124+ echo "Wiping build artifacts for local crate: $crate"
125+ cargo clean -p "$crate"
126+ done
127+ else
128+ echo "ERROR: No Cargo.toml found in the current working directory."
129+ exit 1
130+ fi
131+
55132 # Save cache data
56133 - name : Save Cache
57134 if : |
58- inputs.action == 'save' &&
59- steps.check-cache.outputs.cache-hit != 'true'
60- uses : actions/cache/save@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
135+ inputs.action == 'create'
136+ uses : actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
61137 with :
62138 path : |
63139 ~/.cargo/bin/
64140 ~/.cargo/registry/index/
65141 ~/.cargo/registry/cache/
66142 ~/.cargo/git/db/
67143 target/
68- key : ${{ inputs. cache-key }}
144+ key : ${{ steps.generate-keys.outputs.cargo- cache-key }}
0 commit comments