|
| 1 | +name: Update GitHub Actions runner release |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "23 7 * * 1" |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + actions: write |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: update-actions-runner |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +env: |
| 18 | + BASE_BRANCH: ${{ github.event.repository.default_branch }} |
| 19 | + UPDATE_BRANCH: automation/update-actions-runner |
| 20 | + |
| 21 | +jobs: |
| 22 | + update: |
| 23 | + name: Check for a stable runner release |
| 24 | + runs-on: ubuntu-latest |
| 25 | + timeout-minutes: 20 |
| 26 | + steps: |
| 27 | + - name: Check out the default branch |
| 28 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 |
| 29 | + with: |
| 30 | + ref: ${{ github.event.repository.default_branch }} |
| 31 | + fetch-depth: 0 |
| 32 | + persist-credentials: false |
| 33 | + |
| 34 | + - name: Test updater |
| 35 | + run: python3 scripts/test_update_actions_runner.py |
| 36 | + |
| 37 | + - name: Update runner pins |
| 38 | + id: update |
| 39 | + run: python3 scripts/update_actions_runner.py --github-output "$GITHUB_OUTPUT" |
| 40 | + |
| 41 | + - name: Validate generated changes |
| 42 | + if: steps.update.outputs.changed == 'true' |
| 43 | + run: | |
| 44 | + set -Eeuo pipefail |
| 45 | + git diff --check |
| 46 | + changed_file_list=$(git diff --name-only) |
| 47 | + mapfile -t changed_files <<<"$changed_file_list" |
| 48 | + expected_files=(deploy/compose.yaml runner/Dockerfile) |
| 49 | + if [[ "${changed_files[*]}" != "${expected_files[*]}" ]]; then |
| 50 | + printf 'updater changed unexpected files: %s\n' "${changed_files[*]}" >&2 |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + python3 scripts/test_update_actions_runner.py |
| 54 | +
|
| 55 | + - name: Commit and push deterministic update branch |
| 56 | + if: steps.update.outputs.changed == 'true' |
| 57 | + id: push |
| 58 | + env: |
| 59 | + GH_TOKEN: ${{ github.token }} |
| 60 | + VERSION: ${{ steps.update.outputs.version }} |
| 61 | + run: | |
| 62 | + set -Eeuo pipefail |
| 63 | + gh auth setup-git |
| 64 | + git config user.name 'github-actions[bot]' |
| 65 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 66 | + git add -- deploy/compose.yaml runner/Dockerfile |
| 67 | + git diff --cached --check |
| 68 | + git commit -m "chore(runner): update GitHub Actions runner to ${VERSION}" |
| 69 | +
|
| 70 | + remote_sha= |
| 71 | + if git fetch --no-tags origin "refs/heads/${UPDATE_BRANCH}:refs/remotes/origin/${UPDATE_BRANCH}"; then |
| 72 | + remote_sha=$(git rev-parse "refs/remotes/origin/${UPDATE_BRANCH}") |
| 73 | + fi |
| 74 | + if [[ -n "$remote_sha" ]] && git diff --quiet "$remote_sha" HEAD; then |
| 75 | + printf 'pushed=false\n' >>"$GITHUB_OUTPUT" |
| 76 | + exit 0 |
| 77 | + fi |
| 78 | + git push \ |
| 79 | + --force-with-lease="refs/heads/${UPDATE_BRANCH}:${remote_sha}" \ |
| 80 | + origin "HEAD:refs/heads/${UPDATE_BRANCH}" |
| 81 | + printf 'pushed=true\n' >>"$GITHUB_OUTPUT" |
| 82 | +
|
| 83 | + - name: Create or update pull request |
| 84 | + if: steps.update.outputs.changed == 'true' |
| 85 | + id: pull_request |
| 86 | + env: |
| 87 | + GH_TOKEN: ${{ github.token }} |
| 88 | + VERSION: ${{ steps.update.outputs.version }} |
| 89 | + run: | |
| 90 | + set -Eeuo pipefail |
| 91 | + body_file=$(mktemp) |
| 92 | + trap 'rm -f "$body_file"' EXIT |
| 93 | + printf '%s\n' \ |
| 94 | + "Updates all GitHub Actions runner pins to v${VERSION}." \ |
| 95 | + '' \ |
| 96 | + 'The updater selected the latest stable actions/runner release, verified both official Linux archives, and calculated their SHA-256 checksums.' \ |
| 97 | + '' \ |
| 98 | + 'Generated by .github/workflows/update-actions-runner.yml.' >"$body_file" |
| 99 | +
|
| 100 | + pull_request_numbers=$( |
| 101 | + gh pr list \ |
| 102 | + --repo "$GITHUB_REPOSITORY" \ |
| 103 | + --state open \ |
| 104 | + --base "$BASE_BRANCH" \ |
| 105 | + --head "$UPDATE_BRANCH" \ |
| 106 | + --json number \ |
| 107 | + --jq '.[].number' |
| 108 | + ) |
| 109 | + pull_requests=() |
| 110 | + if [[ -n "$pull_request_numbers" ]]; then |
| 111 | + mapfile -t pull_requests <<<"$pull_request_numbers" |
| 112 | + fi |
| 113 | + if (( ${#pull_requests[@]} > 1 )); then |
| 114 | + echo "more than one open updater pull request exists" >&2 |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | + if (( ${#pull_requests[@]} == 0 )); then |
| 118 | + gh pr create \ |
| 119 | + --repo "$GITHUB_REPOSITORY" \ |
| 120 | + --base "$BASE_BRANCH" \ |
| 121 | + --head "$UPDATE_BRANCH" \ |
| 122 | + --title "chore(runner): update GitHub Actions runner to ${VERSION}" \ |
| 123 | + --body-file "$body_file" |
| 124 | + printf 'created=true\n' >>"$GITHUB_OUTPUT" |
| 125 | + else |
| 126 | + gh pr edit "${pull_requests[0]}" \ |
| 127 | + --repo "$GITHUB_REPOSITORY" \ |
| 128 | + --title "chore(runner): update GitHub Actions runner to ${VERSION}" \ |
| 129 | + --body-file "$body_file" |
| 130 | + printf 'created=false\n' >>"$GITHUB_OUTPUT" |
| 131 | + fi |
| 132 | +
|
| 133 | + - name: Dispatch validation for the update head |
| 134 | + if: >- |
| 135 | + steps.update.outputs.changed == 'true' && |
| 136 | + (steps.push.outputs.pushed == 'true' || steps.pull_request.outputs.created == 'true') |
| 137 | + env: |
| 138 | + GH_TOKEN: ${{ github.token }} |
| 139 | + run: gh workflow run validate.yml --repo "$GITHUB_REPOSITORY" --ref "$UPDATE_BRANCH" |
0 commit comments