-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
211 lines (188 loc) · 8.99 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
name: Cargo Cache
description: Cache cargo build files and the registry
inputs:
cache-group:
description: |
The group of the cache, defaulting to a unique identifier for the workflow job.
If you want two jobs to share the same cache, give them the same group name.
required: false
cargo-home:
description: |
The location of the Cargo home directory.
If you specify the `CARGO_HOME` env variable for your commands, you need to set it here too.
This must *not* end with the trailing slash of the directory.
required: false
default: ~/.cargo
cargo-target-dir:
description: |
The location where Cargo places all generated artifacts, relative to the current working
directory.
If you specify the `CARGO_TARGET_DIR` environmental variable or `--target-dir` for your
commands, you need to set it here as well. This must *not* end with the trailing slash of the
directory.
required: false
default: target
manifest-path:
description: |
The path to `Cargo.toml`.
This is used to determine where `Cargo.lock` and is, which is used in the cache key.
required: false
default: Cargo.toml
save-always:
description: |
This input has been deprecated and will be removed in v3.0.0. It has no effect.
This input used to specify the `save-always` input for `actions/cache`, but has been
deprecated due to its unintended behavior. If you still require this input, you will need
to manually use `actions/cache`. For more information, please see
<https://github.com/actions/cache/tree/v4/save#always-save-cache>.
required: false
save-if:
description: |
Determines if the cache should be saved, or only loaded.
Setting this to `false` will prevent new caches from being created.
required: false
default: "true"
sweep-cache:
description: |
Automatically delete files in the target folder that are not used between when this action is
called and the end of the job.
This can prevent the size of caches snowballing. Since old caches are used to create new
caches, unused files can slowly pile up over time, causing larger caches are longer runtimes.
required: false
default: "false"
cache-cargo-sweep:
description: |
This input has been deprecated and will be removed in v3.0.0. It has no effect.
required: false
outputs:
cache-hit:
description: |
A string value that indicates if an exact match was found for the key.
This is passed through from the `actions/cache` action.
value: ${{ steps.cache.outputs.cache-hit }}
runs:
using: composite
steps:
# We use Cargo throughout the rest of this action for things like generating the lockfile
# and tracking the version in the cache key. If it's not installed, exit early.
- name: Check that Cargo is installed
shell: bash
run: |
cargo --version
# Check that the exit code of the previous command is 0, signaling a success.
if [[ $? -eq 0 ]]; then
# We are assuming that `rustc` is installed if Cargo is.
echo 'Cargo is installed!'
else
echo 'Cargo is not installed!' >&2
echo ' help: Try using the `dtolnay/rust-toolchain` action before this one.' >&2
exit 1
fi
# The `Cargo.lock` file contains the exact crates used as dependencies.
# It is important to use for the caching key, because it might become outdated otherwise.
# If only the `Cargo.toml` file was used, patch releases of crates could cause unnecessary re-compiles.
# Therefore, if the `Cargo.lock` file does not yet exist (e.g. for libraries), we need to create it first.
# If the `Cargo.lock` file has been committed to the repo we reuse it to reproduce the same scenario.
- name: Create Cargo.lock file
shell: bash
env:
MANIFEST_PATH: ${{ inputs.manifest-path }}
run: |
# Locate the root `Cargo.toml` from the a given `Cargo.toml` manifest, then use `jq` to
# extract the result from JSON.
ROOT_MANIFEST=$(cargo locate-project --manifest-path "${MANIFEST_PATH}" --workspace | jq -r '.root')
# Find the `Cargo.lock` that lives next to the root `Cargo.toml`.
ROOT_LOCKFILE=$(dirname "${ROOT_MANIFEST}")/Cargo.lock
# Check if `Cargo.lock` exists.
if [ -f "${ROOT_LOCKFILE}" ]; then
echo 'Reusing existing `Cargo.lock` file.'
else
echo "Cargo.lock file does not exist, creating it..."
# Create `Cargo.lock` file
cargo update --manifest-path "${ROOT_MANIFEST}"
fi
# The dependencies are recompiled if a new Rust version is used
# This steps determines the rustc and cargo version to include it in the cache key
- name: Get cargo version
id: rust-version
shell: bash
run: |
# Print version once for debugging
cargo --version
rustc --version
# Extract the commit hash and date of the rust version
# E.g. "cargo 1.72.0-nightly (0c14026aa 2023-06-14)" becomes "(0c14026aa 2023-06-14)"
cargo=$(cargo --version | grep -oE "\(([a-z0-9]+) [0-9-]+\)")
rustc=$(rustc --version | grep -oE "\(([a-z0-9]+) [0-9-]+\)")
# Write the extracted version to a GitHub output variable
# See <https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions>
echo "rust-version=$(echo $cargo)-$(echo $rustc)" >> "${GITHUB_OUTPUT}"
# If the workflow file's contents change, the new version's jobs should be considered distinct.
- name: Get workflow path
shell: bash
run: |
workflow_ref='${{ github.workflow_ref }}'
# Strip git ref suffix
workflow_abs_path="${workflow_ref%%@*}"
# Strip repository prefix
echo "workflow_path=${workflow_abs_path#'${{ github.repository }}/'}" >> "${GITHUB_ENV}"
- name: Determine cache group
id: cache-group
shell: bash
run: |
if [ -n '${{ inputs.cache-group }}' ]; then
# Use the `cache-group` input if it was provided.
echo 'cache-group=${{ inputs.cache-group }}' >> "${GITHUB_OUTPUT}"
else
# If not, fall back to a unique identifier for the workflow job.
echo 'cache-group=${{ hashFiles(env.workflow_path) }}-${{ github.job }}-${{ strategy.job-index }}' >> "${GITHUB_OUTPUT}"
fi
- name: Log that `save-always` is deprecated
if: ${{ inputs.save-always }}
shell: bash
run: |
echo '::warning title=`save-always` is deprecated::`save-always` does not work as intended and will be removed in v3.0.0.'
# If the cache should also be saved, we use the cache action
# See <https://github.com/actions/cache>.
- name: Restore and save cache
id: cache
uses: actions/cache@v4
if: ${{ inputs.save-if == 'true' }}
with:
path: |
${{ inputs.cargo-home }}/bin/
${{ inputs.cargo-home }}/registry/index/
${{ inputs.cargo-home }}/registry/cache/
${{ inputs.cargo-home }}/git/db/
${{ inputs.cargo-target-dir }}/
key: ${{ runner.os }}-${{ steps.rust-version.outputs.rust-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-${{ steps.rust-version.outputs.rust-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-
${{ runner.os }}-${{ steps.rust-version.outputs.rust-version }}-${{ steps.cache-group.outputs.cache-group }}-
# Otherwise, we only restore the cache
# See <https://github.com/actions/cache/tree/main/restore>.
- name: Restore cache
id: cache-restore
uses: actions/cache/restore@v4
if: ${{ inputs.save-if != 'true' }}
with:
path: |
${{ inputs.cargo-home }}/bin/
${{ inputs.cargo-home }}/registry/index/
${{ inputs.cargo-home }}/registry/cache/
${{ inputs.cargo-home }}/git/db/
${{ inputs.cargo-target-dir }}/
key: ${{ runner.os }}-${{ steps.rust-version.outputs.rust-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-${{ steps.rust-version.outputs.rust-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-
${{ runner.os }}-${{ steps.rust-version.outputs.rust-version }}-${{ steps.cache-group.outputs.cache-group }}-
- name: Log that `cache-cargo-sweep` is deprecated
if: ${{ inputs.cache-cargo-sweep }}
shell: bash
run: |
echo '::warning title=`cache-cargo-sweep` is deprecated::With updates to `BD103/cargo-sweep`, it no longer has any effect. It will be removed in v3.0.0.'
- name: Sweep cache
uses: BD103/cargo-sweep@v2
if: ${{ inputs.sweep-cache == 'true' }}
with:
manifest-path: ${{ inputs.manifest-path }}