|
| 1 | +name: Update Provider Lock Files |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - "infra/shared/versions.tf" |
| 7 | + |
| 8 | +jobs: |
| 9 | + update-locks: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout code |
| 17 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 18 | + with: |
| 19 | + # Use the pull request head ref to ensure we're on the PR branch |
| 20 | + ref: ${{ github.head_ref }} |
| 21 | + # Use PAT for Dependabot PRs, regular token for others |
| 22 | + token: ${{ github.actor == 'dependabot[bot]' && secrets.DEPENDABOT_PAT || secrets.GITHUB_TOKEN }} |
| 23 | + |
| 24 | + - name: Set up Go |
| 25 | + uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 |
| 26 | + |
| 27 | + - name: Install tfupdate |
| 28 | + run: | |
| 29 | + go install github.com/minamijoyo/tfupdate@latest |
| 30 | +
|
| 31 | + - name: Install hcl2json |
| 32 | + run: | |
| 33 | + go install github.com/tmccombs/hcl2json@latest |
| 34 | +
|
| 35 | + - name: Run lock update script |
| 36 | + run: | |
| 37 | + ./infra/scripts/upgrade_tf_version.sh --lock-only |
| 38 | +
|
| 39 | + - name: Check for changed files |
| 40 | + id: git-check |
| 41 | + run: | |
| 42 | + # Check if there are any changes |
| 43 | + if git diff --quiet && git diff --cached --quiet; then |
| 44 | + echo "changed=false" >> "${GITHUB_OUTPUT}" |
| 45 | + echo "No changes detected" |
| 46 | + else |
| 47 | + echo "changed=true" >> "${GITHUB_OUTPUT}" |
| 48 | + echo "Changes detected:" |
| 49 | + git diff --name-only |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Handle Dependabot PR - commit lock files |
| 53 | + if: steps.git-check.outputs.changed == 'true' && github.actor == 'dependabot[bot]' |
| 54 | + env: |
| 55 | + PUSH_REF: ${{ github.head_ref }} |
| 56 | + run: | |
| 57 | + git config --local user.email "action@github.com" |
| 58 | + git config --local user.name "GitHub Action" |
| 59 | +
|
| 60 | + # Add only .terraform.lock.hcl files |
| 61 | + git add '**/.terraform.lock.hcl' |
| 62 | +
|
| 63 | + # Check if there are staged changes |
| 64 | + if git diff --cached --quiet; then |
| 65 | + echo "No .terraform.lock.hcl files to commit" |
| 66 | + else |
| 67 | + git commit -m "Update provider lock files" |
| 68 | + # Push using the PAT token configured in checkout |
| 69 | + git push origin HEAD:"${PUSH_REF}" |
| 70 | + fi |
| 71 | +
|
| 72 | + - name: Check for uncommitted changes (Dependabot PR) |
| 73 | + if: github.actor == 'dependabot[bot]' |
| 74 | + run: | |
| 75 | + # After committing lock files, check if there are still any changes |
| 76 | + if ! git diff --quiet || ! git diff --cached --quiet; then |
| 77 | + echo "Error: There are still uncommitted changes after processing lock files:" |
| 78 | + git status --porcelain |
| 79 | + echo "" |
| 80 | + echo "Changed files:" |
| 81 | + git diff --name-only |
| 82 | + if git diff --cached --quiet; then |
| 83 | + echo "No staged changes" |
| 84 | + else |
| 85 | + echo "Staged changes:" |
| 86 | + git diff --cached --name-only |
| 87 | + fi |
| 88 | + echo "" |
| 89 | + echo "This suggests there are changes beyond just .terraform.lock.hcl files that need attention." |
| 90 | + exit 1 |
| 91 | + else |
| 92 | + echo "All changes have been properly handled" |
| 93 | + fi |
| 94 | +
|
| 95 | + - name: Check for missing lock updates (Non-Dependabot PR) |
| 96 | + if: steps.git-check.outputs.changed == 'true' && github.actor != 'dependabot[bot]' |
| 97 | + env: |
| 98 | + COMMENT_MARKER: "<!-- provider-locks: missing updates -->" |
| 99 | + GH_TOKEN: ${{ github.token }} |
| 100 | + run: | |
| 101 | + # shellcheck disable=SC2296 |
| 102 | + # shellcheck disable=SC2016 |
| 103 | + echo "Error: Provider lock files are out of date!" |
| 104 | + echo "" |
| 105 | + echo "You have modified infra/shared/versions.tf but the corresponding .terraform.lock.hcl files" |
| 106 | + echo "have not been updated. Please run the following command locally and commit the changes:" |
| 107 | + echo "" |
| 108 | + echo " ./infra/scripts/upgrade_tf_version.sh --lock-only" |
| 109 | + echo "" |
| 110 | + echo "Changed files detected:" |
| 111 | + git status --porcelain |
| 112 | + echo "" |
| 113 | + git diff --name-only |
| 114 | +
|
| 115 | + # Leave a comment on the PR |
| 116 | + cat <<EOF > "${{runner.temp}}/pr-comment.md" |
| 117 | + > [!CAUTION] |
| 118 | + > **Provider lock files are out of date!** |
| 119 | + > |
| 120 | + > You have modified \`infra/shared/versions.tf\` but the corresponding \`.terraform.lock.hcl\` files |
| 121 | + > have not been updated. Please run the following command locally and commit the changes: |
| 122 | + > |
| 123 | + > \`\`\`bash |
| 124 | + > ./infra/scripts/upgrade_tf_version.sh --lock-only |
| 125 | + > \`\`\` |
| 126 | +
|
| 127 | + ${COMMENT_MARKER} |
| 128 | + EOF |
| 129 | +
|
| 130 | + # Remove any existing comments from this workflow |
| 131 | + old_comment_ids=$(gh api "repos/{owner}/{repo}/issues/${{github.event.pull_request.number}}/comments" --jq 'map(select((.user.login == "github-actions[bot]") and (.body | endswith($ENV.COMMENT_MARKER + "\n")))) | .[].id') |
| 132 | + for comment_id in $old_comment_ids; do |
| 133 | + gh api -X DELETE "repos/{owner}/{repo}/issues/comments/${comment_id}" |
| 134 | + done |
| 135 | +
|
| 136 | + gh pr comment "${{github.event.pull_request.html_url}}" --body-file "${{runner.temp}}/pr-comment.md" |
| 137 | +
|
| 138 | + exit 1 |
| 139 | +
|
| 140 | + - name: Comment on Dependabot PR success |
| 141 | + if: steps.git-check.outputs.changed == 'true' && github.actor == 'dependabot[bot]' |
| 142 | + env: |
| 143 | + COMMENT_MARKER: "<!-- provider-locks: dependabot updated -->" |
| 144 | + GH_TOKEN: ${{ github.token }} |
| 145 | + run: | |
| 146 | + # shellcheck disable=SC2296 |
| 147 | + # shellcheck disable=SC2016 |
| 148 | + # Leave a comment on the PR |
| 149 | + cat <<EOF > "${{runner.temp}}/pr-comment.md" |
| 150 | + > [!NOTE] |
| 151 | + > **Provider lock files have been automatically updated** |
| 152 | + > |
| 153 | + > This Dependabot PR modified \`infra/shared/versions.tf\`, so the corresponding |
| 154 | + > \`.terraform.lock.hcl\` files have been automatically updated and committed. |
| 155 | + > |
| 156 | + > The changes are ready for review and merge. |
| 157 | +
|
| 158 | + ${COMMENT_MARKER} |
| 159 | + EOF |
| 160 | +
|
| 161 | + # Remove any existing comments from this workflow |
| 162 | + old_comment_ids=$(gh api "repos/{owner}/{repo}/issues/${{github.event.pull_request.number}}/comments" --jq 'map(select((.user.login == "github-actions[bot]") and (.body | endswith($ENV.COMMENT_MARKER + "\n")))) | .[].id') |
| 163 | + for comment_id in $old_comment_ids; do |
| 164 | + gh api -X DELETE "repos/{owner}/{repo}/issues/comments/${comment_id}" |
| 165 | + done |
| 166 | +
|
| 167 | + gh pr comment "${{github.event.pull_request.html_url}}" --body-file "${{runner.temp}}/pr-comment.md" |
| 168 | +
|
| 169 | + - name: Remove stale comments when no changes needed |
| 170 | + if: steps.git-check.outputs.changed == 'false' |
| 171 | + env: |
| 172 | + COMMENT_MARKER_MISSING: "<!-- provider-locks: missing updates -->" |
| 173 | + COMMENT_MARKER_DEPENDABOT: "<!-- provider-locks: dependabot updated -->" |
| 174 | + GH_TOKEN: ${{ github.token }} |
| 175 | + run: | |
| 176 | + # shellcheck disable=SC2296 |
| 177 | + # shellcheck disable=SC2016 |
| 178 | + # Remove any existing comments from this workflow since no changes are needed |
| 179 | + old_comment_ids=$(gh api "repos/{owner}/{repo}/issues/${{github.event.pull_request.number}}/comments" --jq 'map(select((.user.login == "github-actions[bot]") and ((.body | endswith($ENV.COMMENT_MARKER_MISSING + "\n")) or (.body | endswith($ENV.COMMENT_MARKER_DEPENDABOT + "\n"))))) | .[].id') |
| 180 | + for comment_id in $old_comment_ids; do |
| 181 | + echo "Removing stale comment: $comment_id" |
| 182 | + gh api -X DELETE "repos/{owner}/{repo}/issues/comments/${comment_id}" |
| 183 | + done |
| 184 | +
|
| 185 | + echo "No provider lock updates needed - removed any stale comments" |
0 commit comments