|
1 | 1 | name: 'Terraform'
|
2 | 2 |
|
| 3 | +# Controls when the workflow will run |
3 | 4 | on:
|
| 5 | + workflow_dispatch: |
| 6 | + # Triggers the workflow on push (only for the "main" branch) or pull request events |
4 | 7 | push:
|
5 | 8 | branches:
|
6 | 9 | - main
|
7 | 10 | pull_request:
|
8 | 11 |
|
9 |
| -# env: |
10 |
| -# TF_VER: 1.2.8 |
| 12 | +# if a new commit is pushed to the main branch while a previous run is still in progress, the previous run will be cancelled and the new one will start |
| 13 | +concurrency: |
| 14 | + group: ci-${{ github.ref }} |
| 15 | + cancel-in-progress: true |
11 | 16 |
|
12 | 17 | jobs:
|
13 |
| - terraform: |
14 |
| - name: 'Terraform' |
15 |
| - runs-on: ubuntu-latest |
| 18 | + tffmt: |
16 | 19 | permissions:
|
| 20 | + contents: write |
17 | 21 | pull-requests: write
|
| 22 | + runs-on: ubuntu-latest |
18 | 23 |
|
19 | 24 | steps:
|
20 |
| - - name: Checkout |
21 |
| - uses: actions/checkout@v4 |
| 25 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
22 | 28 |
|
23 |
| - - name: Setup Terraform |
24 |
| - uses: hashicorp/setup-terraform@v3 |
25 |
| - # with: |
26 |
| - # terraform_version: $TF_VER |
| 29 | + - name: terraform fmt |
| 30 | + uses: dflook/terraform-fmt@v1 |
| 31 | + with: |
| 32 | + path: . |
27 | 33 |
|
28 |
| - - name: Terraform Format |
29 |
| - id: fmt |
30 |
| - run: terraform fmt -check -recursive |
31 |
| - continue-on-error: true |
| 34 | + - name: Create Pull Request |
| 35 | + uses: peter-evans/create-pull-request@v5 |
| 36 | + with: |
| 37 | + commit-message: terraform fmt |
| 38 | + title: Reformat terraform files |
| 39 | + body: Update Terraform files to canonical format using `terraform fmt` |
| 40 | + branch: automated-terraform-fmt |
| 41 | + base: ${{ github.head_ref }} |
| 42 | + |
| 43 | + tflint: |
| 44 | + runs-on: ubuntu-latest |
32 | 45 |
|
33 |
| -# - name: Terraform Init |
34 |
| -# id: init |
35 |
| -# run: terraform init |
| 46 | + steps: |
| 47 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 48 | + - name: Checkout source code |
| 49 | + uses: actions/checkout@v4 |
36 | 50 |
|
37 |
| - - uses: actions/github-script@v7 |
38 |
| - if: github.event_name == 'pull_request' |
| 51 | + - name: Cache plugin dir |
| 52 | + uses: actions/cache@v4 |
39 | 53 | with:
|
40 |
| - github-token: ${{ secrets.GITHUB_TOKEN }} |
41 |
| - script: | |
42 |
| - // 1. Retrieve existing bot comments for the PR |
43 |
| - const { data: comments } = await github.rest.issues.listComments({ |
44 |
| - owner: context.repo.owner, |
45 |
| - repo: context.repo.repo, |
46 |
| - issue_number: context.issue.number, |
47 |
| - }) |
48 |
| - const botComment = comments.find(comment => { |
49 |
| - return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style') |
50 |
| - }) |
51 |
| - |
52 |
| - // 2. Prepare format of the comment |
53 |
| - const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` |
| 54 | + path: ~/.tflint.d/plugins |
| 55 | + key: tflint-${{ hashFiles('.tflint.hcl') }} |
| 56 | + |
| 57 | + - name: Setup TFLint |
| 58 | + uses: terraform-linters/setup-tflint@v4 |
| 59 | + |
| 60 | + - name: Show version |
| 61 | + run: tflint --version |
54 | 62 |
|
55 |
| - *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`; |
| 63 | + - name: Init TFLint |
| 64 | + env: |
| 65 | + # https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/plugins.md#avoiding-rate-limiting |
| 66 | + GITHUB_TOKEN: ${{ github.token }} |
| 67 | + run: tflint --recursive --init |
56 | 68 |
|
57 |
| - // 3. If we have a comment, update it, otherwise create a new one |
58 |
| - if (botComment) { |
59 |
| - github.rest.issues.updateComment({ |
60 |
| - owner: context.repo.owner, |
61 |
| - repo: context.repo.repo, |
62 |
| - comment_id: botComment.id, |
63 |
| - body: output |
64 |
| - }) |
65 |
| - } else { |
66 |
| - github.rest.issues.createComment({ |
67 |
| - issue_number: context.issue.number, |
68 |
| - owner: context.repo.owner, |
69 |
| - repo: context.repo.repo, |
70 |
| - body: output |
71 |
| - }) |
72 |
| - } |
| 69 | + - name: Run TFLint |
| 70 | + run: tflint --recursive --call-module-type=all |
| 71 | + |
| 72 | + tfdocs: |
| 73 | + permissions: |
| 74 | + contents: write |
| 75 | + runs-on: ubuntu-latest |
| 76 | + |
| 77 | + steps: |
| 78 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 79 | + - name: Checkout source code |
| 80 | + uses: actions/checkout@v4 |
| 81 | + with: |
| 82 | + ref: ${{ github.event.pull_request.head.ref }} |
| 83 | + |
| 84 | + - name: Render terraform docs inside the README.md and push changes back to PR branch |
| 85 | + uses: terraform-docs/gh-actions@v1.3.0 |
| 86 | + with: |
| 87 | + working-dir: . |
| 88 | + output-file: README.md |
| 89 | + output-method: inject |
| 90 | + git-push: "true" |
0 commit comments