Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions .github/workflows/stash-action-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Copyright (c) The stash contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Test Stash Action

on:
pull_request:
push:
branches:
- main

permissions:
contents: read

jobs:
test-deps:
name: "check for dependencies"
runs-on: ubuntu-latest
container: quay.io/centos/centos:stream8
steps:
- uses: actions/checkout@v4

- name: Run action
continue-on-error: true
id: fail
uses: ./stash/restore
with:
path: ./stash
key: this-must-fail

- name: It did not fail
if: ${{ steps.fail.outputs.stash-hit != '' }}
run: |
echo "::error ::Dependency check should have failed!"
exit 1

test-save:
name: "stash/save on ${{ matrix.os }}"
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-13, macos-14]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Run unittests
shell: bash
env:
GH_TOKEN: "${{ github.token }}"
run: |
cd stash/restore
python3 test_get_stash.py

- name: Test Save
uses: ./stash/save
with:
path: stash/
# Create a unique key to test intra workflow artifacts.
key: test/stash-${{ matrix.os }}-${{ github.sha }}
retention-days: '1'

- name: Show rate limit
env:
GH_TOKEN: "${{ github.token }}"
run: gh api -q '.rate' rate_limit

- name: Test Overwrite
id: test
uses: ./stash/save
with:
path: stash/
# Create a unique key to test intra workflow artifacts.
key: test/stash-${{ matrix.os }}-${{ github.sha }}
retention-days: '1'

- name: Show rate limit
env:
GH_TOKEN: "${{ github.token }}"
run: gh api -q '.rate' rate_limit

- name: Check Output
shell: bash
env:
ID: ${{ steps.test.outputs.stash-id }}
URL: ${{ steps.test.outputs.stash-url }}
run: |
if [ -z "$ID" -o -z "$URL" ]; then
echo "Output empty"
exit 1
fi

- name: Check if inter-workflow stash exists
id: stash
uses: ./stash/restore
with:
key: test-stash-cross-${{ matrix.os }}
path: test-stash

- name: Show rate limit
env:
GH_TOKEN: "${{ github.token }}"
run: gh api -q '.rate' rate_limit

- name: Save cross-workflow Stash
if: ${{ steps.stash.outputs.stash-hit == 'false' }}
uses: ./stash/save
with:
path: stash/
key: test-stash-cross-${{ matrix.os }}
retention-days: '90'

test-restore:
name: "stash/restore on ${{ matrix.os }}"
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-13, macos-14]
needs: test-save
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Test intra-workflow stash
uses: ./stash/restore
with:
key: test/stash-${{ matrix.os }}-${{ github.sha }}
path: intra/stash/

- shell: bash
run: ls -laR intra

- name: Test inter-workflow stash
uses: ./stash/restore
with:
key: test-stash-cross-${{ matrix.os }}
path: inter/stash/

- shell: bash
run: ls -laR inter/
14 changes: 14 additions & 0 deletions stash/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) The stash contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__pycache__
79 changes: 79 additions & 0 deletions stash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!--
# Copyright (c) The stash contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-->

# Stash GitHub Action

`Stash` provides a solution for managing large build caches in your workflows, that doesn't require any secrets and can therefore be used in fork PRs.
It's designed as an alternative to `actions/cache` which struggles with big build caches such as `.ccache` directories due to the repository wide [size limit](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy) of 10GB and the fact that caches are [immutable](https://github.com/actions/toolkit/issues/505).
With workflows running multiple configurations across PRs and merge commits this limit is quickly reached, leading to cache evictions, causing CI times to increase.

This action is split into two distinct operations:
- `infrastructure-actions/stash/restore` for fetching a previously stored stash
- `infrastructure-actions/stash/save` for storing a new stash after a build has been completed.

## Features

- Each stash is uploaded as a workflow artifact. In contrasts to `actions/cache` there is no repository wide size limit for artifacts.
- There is no cache eviction, stashes will expire after 5 days by default.
- Artifact storage is free for public repositories and much cheaper than CI minutes (~ 1 Cent/1GB/day) for private repositories.
- No secrets required, stash can be used in fork PRs.
- Follows the same search scope as `actions/cache`: will look for the cache in the current workflow, current branch and finally the base branch of a PR.
This prevents untrusted user caches (e.g. from fork PR CI runs) from being used on the default branch (where actions have elevated permissions by default) or other repo or PR branches.

## Usage

> [!IMPORTANT]
> You have to explicitly save your stash by using `infrastructure-actions/stash/save` action,
> it will not be saved automatically by using `infrastructure-actions/stash/restore`.

To restore a stash before your build process, use the `infrastructure-actions/stash/restore` action in your workflow:


```yaml
steps:
- uses: actions/checkout@v2
- uses: infrastructure-actions/stash/restore@v1
with:
key: 'cache-key'
path: 'path/to/cache'
```

After your build completes, save the stash using the `infrastructure-actions/stash/save` action:

```yaml
steps:
- uses: infrastructure-actions/stash/save@v1
with:
key: 'cache-key'
path: 'path/to/cache'
```
Stashes will expire after 5 days by default.
You can set this from 1-90 days with the `retention-days` input.
Using the `save` action again in the same workflow run will overwrite the existing cache with the same key.
This does apply to each invocation in a matrix job as well!
If you want to keep the old cache, you can use a different key or set `overwrite` to `false`.

Comment thread
assignUser marked this conversation as resolved.
### Inputs and Outputs

Each action (restore and save) has specific inputs tailored to its functionality,
they are specifically modeled after `actions/cache` and `actions/upload-artifact` to provide a drop in replacement.
Please refer to the action metadata (`action.yml`) for a comprehensive list of inputs, including descriptions and default values.

Additionally the `restore` action has an output `stash-hit` which is set to `true` if the cache was restored successfully,
`false` if no cache was restored and '' if the action failed (an error will be thrown unless `continue-on-error` is set).
A technical limitation of composite actions like `Stash` is that all outputs are **strings**.
Therefore an explicit comparison has to be used when using the output:
`if: ${{ steps.restore-stash.outputs.stash-hit == 'true' }}`
Loading