|
| 1 | +name: Adaptive Cache Action |
| 2 | +description: Automatically chooses GitHub Actions cache for public repos, S3 for private/internal repos |
| 3 | +author: SonarSource |
| 4 | + |
| 5 | +inputs: |
| 6 | + path: |
| 7 | + description: A list of files, directories, and wildcard patterns to cache and restore |
| 8 | + required: true |
| 9 | + key: |
| 10 | + description: An explicit key for restoring and saving the cache |
| 11 | + required: true |
| 12 | + restore-keys: |
| 13 | + description: An ordered list of prefix-matched keys to use for restoring stale cache if no cache hit occurred for key |
| 14 | + upload-chunk-size: |
| 15 | + description: The chunk size used to split up large files during upload, in bytes |
| 16 | + enableCrossOsArchive: |
| 17 | + description: An optional boolean when enabled, allows windows runners to save or restore caches that can be restored or saved respectively on other platforms |
| 18 | + default: false |
| 19 | + fail-on-cache-miss: |
| 20 | + description: Fail the workflow if cache entry is not found |
| 21 | + default: false |
| 22 | + lookup-only: |
| 23 | + description: Check if a cache entry exists for the given input(s) (key, restore-keys) without downloading the cache |
| 24 | + default: false |
| 25 | + |
| 26 | +outputs: |
| 27 | + cache-hit: |
| 28 | + description: A boolean value to indicate an exact match was found for the primary key |
| 29 | + value: ${{ steps.github-cache.outputs.cache-hit || steps.s3-cache.outputs.cache-hit }} |
| 30 | + |
| 31 | +runs: |
| 32 | + using: composite |
| 33 | + steps: |
| 34 | + - name: Determine repository visibility |
| 35 | + id: repo-visibility |
| 36 | + shell: bash |
| 37 | + env: |
| 38 | + GITHUB_TOKEN: ${{ github.token }} |
| 39 | + run: | |
| 40 | + REPO_VISIBILITY="${{ github.event.repository.visibility }}" |
| 41 | +
|
| 42 | + # If visibility is not available in the event, try to get it from the API |
| 43 | + if [ -z "$REPO_VISIBILITY" ] || [ "$REPO_VISIBILITY" = "null" ]; then |
| 44 | + REPO_VISIBILITY=$(curl -s -H "Authorization: token ${{ github.token }}" \ |
| 45 | + "https://api.github.com/repos/${{ github.repository }}" | \ |
| 46 | + jq -r '.visibility // "private"') |
| 47 | + fi |
| 48 | +
|
| 49 | + echo "Repository visibility: $REPO_VISIBILITY" |
| 50 | +
|
| 51 | + if [ "$REPO_VISIBILITY" = "public" ]; then |
| 52 | + CACHE_BACKEND="github" |
| 53 | + echo "Using GitHub cache for public repository" |
| 54 | + else |
| 55 | + CACHE_BACKEND="s3" |
| 56 | + echo "Using S3 cache for private/internal repository" |
| 57 | + fi |
| 58 | +
|
| 59 | + echo "cache-backend=$CACHE_BACKEND" >> $GITHUB_OUTPUT |
| 60 | + echo "repo-visibility=$REPO_VISIBILITY" >> $GITHUB_OUTPUT |
| 61 | +
|
| 62 | + - name: Cache with GitHub Actions (public repos) |
| 63 | + if: steps.repo-visibility.outputs.cache-backend == 'github' |
| 64 | + uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 |
| 65 | + id: github-cache |
| 66 | + with: |
| 67 | + path: ${{ inputs.path }} |
| 68 | + key: ${{ inputs.key }} |
| 69 | + restore-keys: ${{ inputs.restore-keys }} |
| 70 | + upload-chunk-size: ${{ inputs.upload-chunk-size }} |
| 71 | + enableCrossOsArchive: ${{ inputs.enableCrossOsArchive }} |
| 72 | + fail-on-cache-miss: ${{ inputs.fail-on-cache-miss }} |
| 73 | + lookup-only: ${{ inputs.lookup-only }} |
| 74 | + |
| 75 | + - name: Cache with S3 (private/internal repos) |
| 76 | + if: steps.repo-visibility.outputs.cache-backend == 's3' |
| 77 | + uses: SonarSource/gh-action_cache@master |
| 78 | + id: s3-cache |
| 79 | + with: |
| 80 | + path: ${{ inputs.path }} |
| 81 | + key: ${{ inputs.key }} |
| 82 | + restore-keys: ${{ inputs.restore-keys }} |
| 83 | + upload-chunk-size: ${{ inputs.upload-chunk-size }} |
| 84 | + enableCrossOsArchive: ${{ inputs.enableCrossOsArchive }} |
| 85 | + fail-on-cache-miss: ${{ inputs.fail-on-cache-miss }} |
| 86 | + lookup-only: ${{ inputs.lookup-only }} |
0 commit comments