|
| 1 | +name: Setup Go cache |
| 2 | +description: Restore Go module and build caches for generated provider workflows |
| 3 | + |
| 4 | +inputs: |
| 5 | + cache-key: |
| 6 | + description: Identifies the workload that writes the build cache |
| 7 | + required: true |
| 8 | + target-os: |
| 9 | + description: GOOS compiled by this workload; defaults to the host GOOS |
| 10 | + required: false |
| 11 | + default: "" |
| 12 | + target-arch: |
| 13 | + description: GOARCH compiled by this workload; defaults to the host GOARCH |
| 14 | + required: false |
| 15 | + default: "" |
| 16 | + prime-modules: |
| 17 | + description: Download dependencies for the checked-out Go modules before saving the module cache |
| 18 | + required: false |
| 19 | + default: "false" |
| 20 | + save: |
| 21 | + description: Save caches when the job succeeds |
| 22 | + required: false |
| 23 | + default: "true" |
| 24 | + |
| 25 | +runs: |
| 26 | + using: composite |
| 27 | + steps: |
| 28 | + - name: Get Go cache configuration |
| 29 | + id: cache-config |
| 30 | + shell: bash |
| 31 | + env: |
| 32 | + TARGET_OS: ${{ inputs.target-os }} |
| 33 | + TARGET_ARCH: ${{ inputs.target-arch }} |
| 34 | + run: | |
| 35 | + set -euo pipefail |
| 36 | + echo "build-cache=$(go env GOCACHE)" >> "$GITHUB_OUTPUT" |
| 37 | + echo "module-cache=$(go env GOMODCACHE)" >> "$GITHUB_OUTPUT" |
| 38 | + echo "go-version=$(go env GOVERSION)" >> "$GITHUB_OUTPUT" |
| 39 | + echo "target-os=${TARGET_OS:-$(go env GOOS)}" >> "$GITHUB_OUTPUT" |
| 40 | + echo "target-arch=${TARGET_ARCH:-$(go env GOARCH)}" >> "$GITHUB_OUTPUT" |
| 41 | +
|
| 42 | + - name: Prepare Go module cache restore |
| 43 | + shell: bash |
| 44 | + env: |
| 45 | + MODULE_CACHE: ${{ steps.cache-config.outputs.module-cache }} |
| 46 | + run: | |
| 47 | + set -euo pipefail |
| 48 | + if [[ -z "$MODULE_CACHE" || "$MODULE_CACHE" == "/" ]]; then |
| 49 | + echo "Refusing to remove unsafe Go module cache path: '$MODULE_CACHE'" >&2 |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + rm -rf -- "$MODULE_CACHE" |
| 53 | +
|
| 54 | + - name: Restore and save Go module cache |
| 55 | + if: inputs.save == 'true' |
| 56 | + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6 |
| 57 | + with: |
| 58 | + path: ${{ steps.cache-config.outputs.module-cache }} |
| 59 | + key: go-mod-v2-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('provider/*.sum', 'upstream/*.sum', 'sdk/go/*.sum', 'sdk/*.sum', '*.sum') }} |
| 60 | + restore-keys: | |
| 61 | + go-mod-v2-${{ runner.os }}-${{ runner.arch }}- |
| 62 | +
|
| 63 | + - name: Restore Go module cache |
| 64 | + if: inputs.save != 'true' |
| 65 | + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6 |
| 66 | + with: |
| 67 | + path: ${{ steps.cache-config.outputs.module-cache }} |
| 68 | + key: go-mod-v2-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('provider/*.sum', 'upstream/*.sum', 'sdk/go/*.sum', 'sdk/*.sum', '*.sum') }} |
| 69 | + restore-keys: | |
| 70 | + go-mod-v2-${{ runner.os }}-${{ runner.arch }}- |
| 71 | +
|
| 72 | + - name: Prime Go module cache |
| 73 | + if: inputs.prime-modules == 'true' |
| 74 | + shell: bash |
| 75 | + run: | |
| 76 | + set -euo pipefail |
| 77 | + shopt -s nullglob |
| 78 | + declare -A modules=() |
| 79 | + for dependency_file in provider/*.sum upstream/*.sum sdk/go/*.sum sdk/*.sum *.sum; do |
| 80 | + module_dir=$(dirname "$dependency_file") |
| 81 | + if [[ -f "$module_dir/go.mod" ]]; then |
| 82 | + modules["$module_dir"]=1 |
| 83 | + fi |
| 84 | + done |
| 85 | + for module_dir in "${!modules[@]}"; do |
| 86 | + (cd "$module_dir" && go mod download) |
| 87 | + done |
| 88 | +
|
| 89 | + - name: Restore and save Go build cache |
| 90 | + if: inputs.save == 'true' |
| 91 | + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6 |
| 92 | + with: |
| 93 | + path: ${{ steps.cache-config.outputs.build-cache }} |
| 94 | + key: go-build-v2-${{ steps.cache-config.outputs.target-os }}-${{ steps.cache-config.outputs.target-arch }}-${{ steps.cache-config.outputs.go-version }}-${{ inputs.cache-key }}-${{ hashFiles('**/*.go', '**/go.sum') }} |
| 95 | + restore-keys: | |
| 96 | + go-build-v2-${{ steps.cache-config.outputs.target-os }}-${{ steps.cache-config.outputs.target-arch }}-${{ steps.cache-config.outputs.go-version }}-${{ inputs.cache-key }}- |
| 97 | + go-build-v2-${{ steps.cache-config.outputs.target-os }}-${{ steps.cache-config.outputs.target-arch }}-${{ steps.cache-config.outputs.go-version }}- |
| 98 | +
|
| 99 | + - name: Restore Go build cache |
| 100 | + if: inputs.save != 'true' |
| 101 | + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6 |
| 102 | + with: |
| 103 | + path: ${{ steps.cache-config.outputs.build-cache }} |
| 104 | + key: go-build-v2-${{ steps.cache-config.outputs.target-os }}-${{ steps.cache-config.outputs.target-arch }}-${{ steps.cache-config.outputs.go-version }}-${{ inputs.cache-key }}-${{ hashFiles('**/*.go', '**/go.sum') }} |
| 105 | + restore-keys: | |
| 106 | + go-build-v2-${{ steps.cache-config.outputs.target-os }}-${{ steps.cache-config.outputs.target-arch }}-${{ steps.cache-config.outputs.go-version }}-${{ inputs.cache-key }}- |
| 107 | + go-build-v2-${{ steps.cache-config.outputs.target-os }}-${{ steps.cache-config.outputs.target-arch }}-${{ steps.cache-config.outputs.go-version }}- |
0 commit comments