Skip to content

Commit dd61c91

Browse files
committed
Add optional "clean" parameter to input of stash/restore action
Sometimes you want to restore stash (for example "~/.cache/" when it already exists - in which case `gh download` will fail with error extracting FILE: file exists While you can delete the directory before restore action is called, it's a bit more convenient to add optional "clean" input - when set to "true" and target directory exists, it will delete it and subsequently re-create the directory.
1 parent 4ab8682 commit dd61c91

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

stash/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ Each action (restore and save) has specific inputs tailored to its functionality
7272
they are specifically modeled after `actions/cache` and `actions/upload-artifact` to provide a drop in replacement.
7373
Please refer to the action metadata (`action.yml`) for a comprehensive list of inputs, including descriptions and default values.
7474

75-
Additionally the `restore` action has an output `stash-hit` which is set to `true` if the cache was restored successfully,
75+
The `restore` action has an optional "overwrite" input which defaults to `false` - when set to "true", it
76+
will delete the existing contents of the target directory before restoring the stash.
77+
78+
Additionally, the `restore` action has an output `stash-hit` which is set to `true` if the cache was restored successfully,
7679
`false` if no cache was restored and '' if the action failed (an error will be thrown unless `continue-on-error` is set).
7780
A technical limitation of composite actions like `Stash` is that all outputs are **strings**.
78-
Therefore an explicit comparison has to be used when using the output:
81+
Therefore, an explicit comparison has to be used when using the output:
7982
`if: ${{ steps.restore-stash.outputs.stash-hit == 'true' }}`

stash/restore/action.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ inputs:
2828
To reduce amount of api calls (1000/h/repo limit) the name of the
2929
current branch will be appended to the key. Key and branchname will be normalized.
3030
required: true
31-
3231
path:
3332
description: 'The directory the stash will be restored in.'
3433
default: ${{ github.workspace }}
3534

3635
token:
3736
description: 'GITHUB_TOKEN to use to authenticate against the artifacts api.'
3837
default: ${{ github.token }}
38+
39+
clean:
40+
description: 'Whether the stash_directory should be removed before downloading the stash.'
41+
default: 'false'
42+
3943
outputs:
4044
stash-hit:
4145
description: >
@@ -139,6 +143,12 @@ runs:
139143
run: |
140144
# Catch errors in the download with || to avoid the whole workflow failing
141145
# when the download times out
146+
if [[ "${{ inputs.clean }}" == "true" ]]; then
147+
if [[ -d "$STASH_DIR" ]]; then
148+
echo "Removing existing stash directory: $STASH_DIR"
149+
rm -rf "$STASH_DIR"
150+
fi
151+
fi
142152
gh run download "$STASH_RUN_ID" \
143153
--name "$STASH_NAME" \
144154
--dir "$STASH_DIR" \

0 commit comments

Comments
 (0)