|
| 1 | +name: Terraform |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + terraform-version: |
| 7 | + required: false |
| 8 | + type: string |
| 9 | + default: "latest" |
| 10 | + comitter-name: |
| 11 | + required: false |
| 12 | + type: string |
| 13 | + default: "staffbase-actions[bot]" |
| 14 | + comitter-email: |
| 15 | + required: false |
| 16 | + type: string |
| 17 | + default: "staffbase-actions[bot]@users.noreply.github.com" |
| 18 | + secrets: |
| 19 | + app-id: |
| 20 | + required: true |
| 21 | + private-key: |
| 22 | + required: true |
| 23 | + |
| 24 | +jobs: |
| 25 | + format: |
| 26 | + name: Format |
| 27 | + runs-on: ubuntu-24.04 |
| 28 | + steps: |
| 29 | + - name: Get App Token |
| 30 | + uses: actions/create-github-app-token@v1.11.0 |
| 31 | + id: get_token |
| 32 | + with: |
| 33 | + app-id: ${{ secrets.app-id }} |
| 34 | + private-key: ${{ secrets.private-key }} |
| 35 | + |
| 36 | + - name: Checkout |
| 37 | + uses: actions/checkout@v4.2.2 |
| 38 | + with: |
| 39 | + token: ${{ steps.get_token.outputs.token }} |
| 40 | + |
| 41 | + - name: Setup Terraform |
| 42 | + uses: hashicorp/setup-terraform@v3.1.2 |
| 43 | + with: |
| 44 | + terraform_version: ${{ inputs.terraform-version }} |
| 45 | + |
| 46 | + - name: Terraform Format |
| 47 | + id: format |
| 48 | + run: terraform fmt -recursive -diff |
| 49 | + |
| 50 | + - name: Find Format Comment |
| 51 | + if: ${{ steps.format.outputs.stdout != '' }} |
| 52 | + uses: peter-evans/find-comment@v3 |
| 53 | + id: comment |
| 54 | + with: |
| 55 | + issue-number: ${{ github.event.number }} |
| 56 | + comment-author: "github-actions[bot]" |
| 57 | + body-includes: "Terraform `format` " |
| 58 | + |
| 59 | + - name: Comment Hint when Terraform Format failed |
| 60 | + if: ${{ steps.format.outputs.stdout != '' }} |
| 61 | + uses: peter-evans/create-or-update-comment@v4 |
| 62 | + with: |
| 63 | + comment-id: ${{ steps.comment.outputs.comment-id }} |
| 64 | + issue-number: ${{ github.event.number }} |
| 65 | + edit-mode: replace |
| 66 | + body: | |
| 67 | + ### Terraform `format` applied changes |
| 68 | + We found some linting issues in your files. We automatically fixed them for you. See the diff below for details. |
| 69 | +
|
| 70 | + <details><summary>Show Details</summary> |
| 71 | +
|
| 72 | + ```diff |
| 73 | + ${{ steps.format.outputs.stdout }} |
| 74 | + ``` |
| 75 | +
|
| 76 | + </details> |
| 77 | +
|
| 78 | + Hint: You can install Terraform Support in your editor to avoid such issues in the future: |
| 79 | + - [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform) |
| 80 | + - [Jetbrains](https://plugins.jetbrains.com/plugin/7808-terraform-and-hcl) |
| 81 | +
|
| 82 | + - name: Commit changes |
| 83 | + if: ${{ steps.format.outputs.stdout != '' }} |
| 84 | + run: | |
| 85 | + git config --global user.name "${{ inputs.comitter-name }}" |
| 86 | + git config --global user.email "${{ inputs.comitter-email }}" |
| 87 | + git add . |
| 88 | + git commit -m "🚨 Format Terraform files" |
| 89 | + git push |
| 90 | +
|
| 91 | + - name: Fail if format failed |
| 92 | + if: ${{ steps.format.outputs.stdout != '' }} |
| 93 | + run: exit 1 |
0 commit comments