|
| 1 | +name: Update Go Modules (Manual) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + pr_number: |
| 7 | + description: Pull request number to update (same-repo PRs only) |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + |
| 11 | +permissions: |
| 12 | + actions: write |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + update-go-mods: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Harden Runner |
| 21 | + uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0 |
| 22 | + with: |
| 23 | + egress-policy: audit |
| 24 | + |
| 25 | + - name: Resolve pull request |
| 26 | + id: pr |
| 27 | + env: |
| 28 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + run: | |
| 30 | + set -euo pipefail |
| 31 | + pr_number="${{ inputs.pr_number }}" |
| 32 | +
|
| 33 | + pr_json="$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${pr_number}")" |
| 34 | + head_repo="$(printf '%s' "${pr_json}" | jq -r '.head.repo.full_name')" |
| 35 | + head_ref="$(printf '%s' "${pr_json}" | jq -r '.head.ref')" |
| 36 | + head_sha="$(printf '%s' "${pr_json}" | jq -r '.head.sha')" |
| 37 | +
|
| 38 | + echo "head_repo=${head_repo}" >> "$GITHUB_OUTPUT" |
| 39 | + echo "head_ref=${head_ref}" >> "$GITHUB_OUTPUT" |
| 40 | + echo "head_sha=${head_sha}" >> "$GITHUB_OUTPUT" |
| 41 | +
|
| 42 | + if [[ "${head_repo}" != "${GITHUB_REPOSITORY}" ]]; then |
| 43 | + echo "cross_repo=true" >> "$GITHUB_OUTPUT" |
| 44 | + else |
| 45 | + echo "cross_repo=false" >> "$GITHUB_OUTPUT" |
| 46 | + fi |
| 47 | +
|
| 48 | + - name: Stop (forked PRs not supported) |
| 49 | + if: steps.pr.outputs.cross_repo == 'true' |
| 50 | + env: |
| 51 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + run: | |
| 53 | + set -euo pipefail |
| 54 | + pr_number="${{ inputs.pr_number }}" |
| 55 | + head_repo="${{ steps.pr.outputs.head_repo }}" |
| 56 | + gh pr comment "${pr_number}" --repo "${GITHUB_REPOSITORY}" --body \ |
| 57 | + "This PR targets \`${GITHUB_REPOSITORY}\`, but its head branch is in \`${head_repo}\`.\n\nI can't push go.mod/go.sum/vendor updates to forked PR branches.\n\nPlease run \`go mod tidy\` (and \`go mod vendor\` if needed) locally and push the result to the PR branch." |
| 58 | + exit 1 |
| 59 | +
|
| 60 | + - name: Checkout PR branch |
| 61 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v4.2.2 |
| 62 | + with: |
| 63 | + ref: ${{ steps.pr.outputs.head_ref }} |
| 64 | + fetch-depth: 0 |
| 65 | + |
| 66 | + - name: Setup Go |
| 67 | + uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0 |
| 68 | + with: |
| 69 | + go-version-file: go.mod |
| 70 | + check-latest: true |
| 71 | + cache-dependency-path: | |
| 72 | + go.sum |
| 73 | + tests/go.sum |
| 74 | + pkg/azclient/go.sum |
| 75 | + pkg/azclient/cache/go.sum |
| 76 | + pkg/azclient/configloader/go.sum |
| 77 | + pkg/azclient/trace/go.sum |
| 78 | + pkg/azclient/client-gen/go.sum |
| 79 | + health-probe-proxy/go.sum |
| 80 | + kubetest2-aks/go.sum |
| 81 | +
|
| 82 | + - name: Run go mod tidy, vendor, and verify |
| 83 | + run: | |
| 84 | + set -euo pipefail |
| 85 | + modules=( |
| 86 | + . |
| 87 | + tests |
| 88 | + pkg/azclient |
| 89 | + pkg/azclient/cache |
| 90 | + pkg/azclient/configloader |
| 91 | + pkg/azclient/trace |
| 92 | + pkg/azclient/client-gen |
| 93 | + health-probe-proxy |
| 94 | + kubetest2-aks |
| 95 | + ) |
| 96 | + vendor_modules=( |
| 97 | + . |
| 98 | + ) |
| 99 | + for m in "${modules[@]}"; do |
| 100 | + echo ">> go mod tidy (${m})" |
| 101 | + (cd "${m}" && go mod tidy) |
| 102 | + if [[ " ${vendor_modules[*]} " == *" ${m} "* ]]; then |
| 103 | + echo ">> go mod vendor (${m})" |
| 104 | + (cd "${m}" && go mod vendor) |
| 105 | + fi |
| 106 | + echo ">> go mod verify (${m})" |
| 107 | + (cd "${m}" && go mod verify) |
| 108 | + done |
| 109 | +
|
| 110 | + - name: Detect changes |
| 111 | + id: diff |
| 112 | + run: | |
| 113 | + set -euo pipefail |
| 114 | + status_output="$(git status --porcelain)" |
| 115 | + if [ -z "${status_output}" ]; then |
| 116 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 117 | + exit 0 |
| 118 | + fi |
| 119 | + printf '%s\n' "${status_output}" |
| 120 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 121 | +
|
| 122 | + - name: Commit and push go module updates |
| 123 | + if: steps.diff.outputs.changed == 'true' |
| 124 | + run: | |
| 125 | + set -euo pipefail |
| 126 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 127 | + git config user.name "github-actions[bot]" |
| 128 | + git add -A |
| 129 | + git commit -m "chore: tidy go modules" |
| 130 | + git push origin "HEAD:${{ steps.pr.outputs.head_ref }}" |
| 131 | +
|
| 132 | + - name: Trigger CI workflows (best effort) |
| 133 | + if: steps.diff.outputs.changed == 'true' |
| 134 | + continue-on-error: true |
| 135 | + env: |
| 136 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + run: | |
| 138 | + set -euo pipefail |
| 139 | + ref="${{ steps.pr.outputs.head_ref }}" |
| 140 | + workflows=( |
| 141 | + go-mod-consistency.yaml |
| 142 | + lint.yaml |
| 143 | + trivy.yaml |
| 144 | + codespell.yaml |
| 145 | + ) |
| 146 | + for wf in "${workflows[@]}"; do |
| 147 | + echo "Dispatching ${wf} on ${ref}" |
| 148 | + gh workflow run "${wf}" --repo "${GITHUB_REPOSITORY}" --ref "${ref}" || true |
| 149 | + done |
| 150 | +
|
| 151 | + - name: Comment on pull request |
| 152 | + env: |
| 153 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 154 | + run: | |
| 155 | + set -euo pipefail |
| 156 | + pr_number="${{ inputs.pr_number }}" |
| 157 | + if [[ "${{ steps.diff.outputs.changed }}" != "true" ]]; then |
| 158 | + gh pr comment "${pr_number}" --repo "${GITHUB_REPOSITORY}" --body \ |
| 159 | + "No go.mod/go.sum/vendor updates were needed." |
| 160 | + exit 0 |
| 161 | + fi |
| 162 | +
|
| 163 | + gh pr comment "${pr_number}" --repo "${GITHUB_REPOSITORY}" --body \ |
| 164 | + "Pushed go.mod/go.sum/vendor updates to \`${{ steps.pr.outputs.head_ref }}\`.\n\nNote: commits pushed by GitHub Actions using \`GITHUB_TOKEN\` don't auto-trigger other \`push\`/ \`pull_request\` workflows. This workflow tries to dispatch common CI workflows; if checks didn't start, run them manually from the **Actions** tab." |
0 commit comments