|
| 1 | +--- |
| 2 | +name: Sync go.mod with Dockerfile |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + paths: |
| 7 | + - Dockerfile |
| 8 | + |
| 9 | +permissions: {} |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} |
| 13 | + cancel-in-progress: ${{ github.event_name == 'pull_request' }} |
| 14 | + |
| 15 | +jobs: |
| 16 | + sync: |
| 17 | + name: Sync go.mod |
| 18 | + runs-on: ubuntu-latest |
| 19 | + permissions: |
| 20 | + contents: write # Push the go.mod sync commit back to dependabot branches when they diverge |
| 21 | + steps: |
| 22 | + - uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1 |
| 23 | + with: |
| 24 | + egress-policy: audit |
| 25 | + |
| 26 | + # Check out the PR head branch (not the merge ref) so any sync commit lands on it. |
| 27 | + # persist-credentials is required so the subsequent git push uses GITHUB_TOKEN. |
| 28 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 29 | + with: |
| 30 | + ref: ${{ github.head_ref }} |
| 31 | + persist-credentials: true |
| 32 | + |
| 33 | + - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 |
| 34 | + with: |
| 35 | + go-version-file: go.mod |
| 36 | + |
| 37 | + - name: Verify or sync go directive against Dockerfile |
| 38 | + env: |
| 39 | + IS_DEPENDABOT: ${{ github.actor == 'dependabot[bot]' && startsWith(github.head_ref, 'dependabot/docker/golang') }} |
| 40 | + run: | |
| 41 | + set -euo pipefail |
| 42 | +
|
| 43 | + DOCKERFILE_GO=$(grep -oE 'golang:[0-9]+\.[0-9]+\.[0-9]+' Dockerfile | head -1 | cut -d: -f2) |
| 44 | + if [ -z "$DOCKERFILE_GO" ]; then |
| 45 | + echo "::error::Could not extract Go version from Dockerfile" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | +
|
| 49 | + GOMOD_GO=$(go mod edit -json | jq -r '.Go') |
| 50 | +
|
| 51 | + if [ "$DOCKERFILE_GO" = "$GOMOD_GO" ]; then |
| 52 | + echo "go.mod and Dockerfile both at Go $GOMOD_GO" |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | +
|
| 56 | + if [ "$IS_DEPENDABOT" != "true" ]; then |
| 57 | + echo "::error file=go.mod::Dockerfile is on Go $DOCKERFILE_GO but go.mod is on $GOMOD_GO. Run: go mod edit -go=$DOCKERFILE_GO" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +
|
| 61 | + echo "Bumping go.mod from $GOMOD_GO to $DOCKERFILE_GO" |
| 62 | + go mod edit -go="$DOCKERFILE_GO" |
| 63 | +
|
| 64 | + git config user.name 'github-actions[bot]' |
| 65 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 66 | + git add go.mod |
| 67 | + git commit -m "chore(deps): sync go.mod to Go $DOCKERFILE_GO" |
| 68 | + git push |
0 commit comments