Skip to content

Commit b61985c

Browse files
BUILD-9956 add backend input option
1 parent f34b8e8 commit b61985c

File tree

3 files changed

+82
-40
lines changed

3 files changed

+82
-40
lines changed

.github/workflows/test-action.yml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ on:
77
workflow_dispatch:
88

99
jobs:
10-
build:
10+
test-github-cache:
1111
runs-on: github-ubuntu-latest-s
1212
permissions:
1313
id-token: write
1414
contents: read
15-
1615
steps:
1716
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1817
- uses: jdx/mise-action@c37c93293d6b742fc901e1406b8f764f6fb19dac # v2.4.4
@@ -22,12 +21,10 @@ jobs:
2221
id: cache-python
2322
uses: ./
2423
with:
25-
path: |
26-
~/.cache/pip
24+
path: ~/.cache/pip
2725
key: python-${{ runner.os }}-pytest-requests
2826
restore-keys: python-${{ runner.os }}-
29-
environment: dev
30-
27+
backend: github
3128
- name: Check cache hit result
3229
run: |
3330
echo "Cache hit: ${{ steps.cache-python.outputs.cache-hit }}"
@@ -43,12 +40,46 @@ jobs:
4340
- name: Run tests
4441
run: python -m pytest --version
4542

46-
cache-with-fallback:
43+
test-s3-cache:
4744
runs-on: github-ubuntu-latest-s
4845
permissions:
4946
id-token: write
5047
contents: read
48+
steps:
49+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
50+
- uses: jdx/mise-action@c37c93293d6b742fc901e1406b8f764f6fb19dac # v2.4.4
51+
with:
52+
version: 2025.7.12
53+
- name: Cache Python dependencies
54+
id: cache-python
55+
uses: ./
56+
with:
57+
path: ~/.cache/pip
58+
key: python-${{ runner.os }}-pytest-requests
59+
restore-keys: python-${{ runner.os }}-
60+
environment: dev
61+
backend: s3
5162

63+
- name: Check cache hit result
64+
run: |
65+
echo "Cache hit: ${{ steps.cache-python.outputs.cache-hit }}"
66+
if [ "${{ steps.cache-python.outputs.cache-hit }}" == "true" ]; then
67+
echo "✅ Cache was found and restored"
68+
else
69+
echo "❌ Cache was not found, will need to rebuild"
70+
fi
71+
- name: Install dependencies
72+
run: |
73+
python -m pip install --upgrade pip
74+
pip install pytest requests
75+
- name: Run tests
76+
run: python -m pytest --version
77+
78+
test-s3-cache-with-fallback:
79+
runs-on: github-ubuntu-latest-s
80+
permissions:
81+
id-token: write
82+
contents: read
5283
steps:
5384
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
5485
- uses: jdx/mise-action@c37c93293d6b742fc901e1406b8f764f6fb19dac # v2.4.4
@@ -68,6 +99,7 @@ jobs:
6899
fail-on-cache-miss: false
69100
fallback-branch: refs/heads/branch-2
70101
environment: dev
102+
backend: s3
71103
- name: Check Go cache hit result
72104
run: |
73105
echo "Go cache hit: ${{ steps.cache-go.outputs.cache-hit }}"

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ Adaptive cache action that automatically chooses the appropriate caching backend
2626
2727
## Inputs
2828
29-
| Input | Description | Required | Default |
30-
|------------------------|---------------------------------------------------------------------------------------------------------------------------------|----------|---------|
31-
| `path` | Files, directories, and wildcard patterns to cache | Yes | |
32-
| `key` | Explicit key for restoring and saving cache | Yes | |
33-
| `restore-keys` | Ordered list of prefix-matched keys for fallback | No | |
34-
| `fallback-branch` | Optional maintenance branch for fallback restore keys (pattern: `branch-*`). If not set, the repository default branch is used. | No | |
35-
| `environment` | Environment to use (dev or prod, S3 cache only) | No | `prod` |
36-
| `upload-chunk-size` | Chunk size for large file uploads (bytes) | No | |
37-
| `enableCrossOsArchive` | Enable cross-OS cache compatibility | No | `false` |
38-
| `fail-on-cache-miss` | Fail workflow if cache entry not found | No | `false` |
39-
| `lookup-only` | Only check cache existence without downloading | No | `false` |
29+
| Input | Description | Required | Default |
30+
|------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------|
31+
| `path` | Files, directories, and wildcard patterns to cache | Yes | |
32+
| `key` | Explicit key for restoring and saving cache | Yes | |
33+
| `restore-keys` | Ordered list of prefix-matched keys for fallback | No | |
34+
| `fallback-branch` | Optional maintenance branch for fallback restore keys (pattern: `branch-*`, S3 backend only). If not set, the repository default branch is used. | No | |
35+
| `environment` | Environment to use (dev or prod, S3 backend only) | No | `prod` |
36+
| `upload-chunk-size` | Chunk size for large file uploads (bytes) | No | |
37+
| `enableCrossOsArchive` | Enable cross-OS cache compatibility | No | `false` |
38+
| `fail-on-cache-miss` | Fail workflow if cache entry not found | No | `false` |
39+
| `lookup-only` | Only check cache existence without downloading | No | `false` |
40+
| `backend` | Force specific backend: `github` or `s3` | No | |
4041

4142
## Outputs
4243

action.yml

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ inputs:
2323
description: Check if a cache entry exists for the given input(s) (key, restore-keys) without downloading the cache
2424
default: false
2525
environment:
26-
description: Environment to use (dev or prod)
26+
description: Environment to use ('dev' or 'prod', 's3' backend only).
2727
default: prod
2828
fallback-branch:
29-
description: Optional maintenance branch for fallback restore keys (pattern branch-*). If not set, the repository default branch is used.
29+
description: Optional maintenance branch for fallback restore keys (pattern 'branch-*', 's3' backend only). If not set, the repository
30+
default branch is used.
31+
backend:
32+
description: Force cache backend ('github' or 's3'). If not set, automatically determined based on repository visibility.
3033

3134
outputs:
3235
cache-hit:
@@ -36,34 +39,39 @@ outputs:
3639
runs:
3740
using: composite
3841
steps:
39-
- name: Determine repository visibility
40-
id: repo-visibility
42+
- name: Determine cache backend
43+
id: cache-backend
4144
shell: bash
4245
env:
4346
GITHUB_TOKEN: ${{ github.token }}
4447
REPO_VISIBILITY: ${{ github.event.repository.visibility }}
48+
FORCED_BACKEND: ${{ inputs.backend }}
4549
run: |
46-
# If visibility is not available in the event, try to get it from the API
47-
if [[ -z "$REPO_VISIBILITY" || "$REPO_VISIBILITY" = "null" ]]; then
48-
REPO_VISIBILITY=$(curl -s -H "Authorization: token ${{ github.token }}" \
49-
"https://api.github.com/repos/${{ github.repository }}" | \
50-
jq -r '.visibility // "private"')
51-
fi
52-
echo "Repository visibility: $REPO_VISIBILITY"
53-
54-
if [[ "$REPO_VISIBILITY" == "public" ]]; then
55-
CACHE_BACKEND="github"
56-
echo "Using GitHub cache for public repository"
50+
if [[ "$FORCED_BACKEND" == "github" || "$FORCED_BACKEND" == "s3" ]]; then
51+
CACHE_BACKEND="$FORCED_BACKEND"
52+
echo "Using forced backend: $CACHE_BACKEND"
5753
else
58-
CACHE_BACKEND="s3"
59-
echo "Using S3 cache for private/internal repository"
54+
# If visibility is not available in the event, try to get it from the API
55+
if [[ -z "$REPO_VISIBILITY" || "$REPO_VISIBILITY" = "null" ]]; then
56+
REPO_VISIBILITY=$(curl -s -H "Authorization: token ${{ github.token }}" \
57+
"https://api.github.com/repos/${{ github.repository }}" | \
58+
jq -r '.visibility // "private"')
59+
fi
60+
echo "Repository visibility: $REPO_VISIBILITY"
61+
62+
if [[ "$REPO_VISIBILITY" == "public" ]]; then
63+
CACHE_BACKEND="github"
64+
echo "Using GitHub cache for public repository"
65+
else
66+
CACHE_BACKEND="s3"
67+
echo "Using S3 cache for private/internal repository"
68+
fi
6069
fi
6170

6271
echo "cache-backend=$CACHE_BACKEND" >> "$GITHUB_OUTPUT"
63-
echo "repo-visibility=$REPO_VISIBILITY" >> "$GITHUB_OUTPUT"
6472

6573
- name: Cache with GitHub Actions (public repos)
66-
if: steps.repo-visibility.outputs.cache-backend == 'github'
74+
if: steps.cache-backend.outputs.cache-backend == 'github'
6775
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
6876
id: github-cache
6977
with:
@@ -77,7 +85,7 @@ runs:
7785

7886
# Cache with S3 (private/internal repos)
7987
- name: Authenticate to AWS
80-
if: steps.repo-visibility.outputs.cache-backend == 's3'
88+
if: steps.cache-backend.outputs.cache-backend == 's3'
8189
id: aws-auth
8290
shell: bash
8391
env:
@@ -136,7 +144,7 @@ runs:
136144
} >> "$GITHUB_OUTPUT"
137145
138146
- name: Prepare cache keys
139-
if: steps.repo-visibility.outputs.cache-backend == 's3'
147+
if: steps.cache-backend.outputs.cache-backend == 's3'
140148
shell: bash
141149
id: prepare-keys
142150
env:
@@ -148,13 +156,14 @@ runs:
148156
run: $GITHUB_ACTION_PATH/scripts/prepare-keys.sh
149157

150158
- uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708 # v5.1.1
159+
if: steps.cache-backend.outputs.cache-backend == 's3'
151160
with:
152161
aws-region: eu-central-1
153162
aws-access-key-id: ${{ steps.aws-auth.outputs.AWS_ACCESS_KEY_ID }}
154163
aws-secret-access-key: ${{ steps.aws-auth.outputs.AWS_SECRET_ACCESS_KEY }}
155164
aws-session-token: ${{ steps.aws-auth.outputs.AWS_SESSION_TOKEN }}
156165
- name: Cache on S3
157-
if: steps.repo-visibility.outputs.cache-backend == 's3'
166+
if: steps.cache-backend.outputs.cache-backend == 's3'
158167
uses: runs-on/cache@50350ad4242587b6c8c2baa2e740b1bc11285ff4 # v4.3.0
159168
id: s3-cache
160169
env:

0 commit comments

Comments
 (0)