11name : Deploy IAM Resources to AWS with Terraform
22
33on :
4- pull_request :
4+ pull-request :
55 branches :
6- - main
6+ - main # or any other branch you want to trigger the deployment
77
88jobs :
99 terraform :
1010 name : Terraform
1111 runs-on : ubuntu-latest
12-
13- defaults :
14- run :
15- working-directory : terraform
16- permissions :
17- pull-requests : write
12+ env :
13+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
1814
1915 steps :
20- - uses : actions/checkout@v4
21- - uses : hashicorp/setup-terraform@v3
22-
23- - name : Terraform fmt
24- id : fmt
25- run : terraform fmt -check
26- continue-on-error : true
27-
28- - name : Terraform Init
29- id : init
30- run : terraform init
31- env :
32- AWS_ACCESS_KEY_ID : ${{ secrets.AWS_ACCESS_KEY_ID }}
33- AWS_SECRET_ACCESS_KEY : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
34-
35- - name : Terraform Validate
36- id : validate
37- run : terraform validate -no-color
38-
39- - name : Terraform Plan
40- id : plan
41- run : terraform plan -no-color
42- continue-on-error : true
43-
44- - uses : actions/github-script@v6
45- if : github.event_name == 'pull_request'
46- env :
47- PLAN : " terraform\n ${{ steps.plan.outputs.stdout }}"
48- with :
49- github-token : ${{ secrets.GITHUB_TOKEN }}
50- script : |
51- // 1. Retrieve existing bot comments for the PR
52- const { data: comments } = await github.rest.issues.listComments({
53- owner: context.repo.owner,
54- repo: context.repo.repo,
55- issue_number: context.issue.number,
56- })
57- const botComment = comments.find(comment => {
58- return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style')
59- })
60-
61- // 2. Prepare format of the comment
62- const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
63- # ### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
64- # ### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
65- <details><summary>Validation Output</summary>
66-
67- \`\`\`\n
68- ${{ steps.validate.outputs.stdout }}
69- \`\`\`
70-
71- </details>
72-
73- # ### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
74-
75- <details><summary>Show Plan</summary>
76-
77- \`\`\`\n
78- ${process.env.PLAN}
79- \`\`\`
80-
81- </details>
82-
83- *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`;
84-
85- // 3. If we have a comment, update it, otherwise create a new one
86- if (botComment) {
87- github.rest.issues.updateComment({
88- owner : context.repo.owner,
89- repo : context.repo.repo,
90- comment_id : botComment.id,
91- body : output
92- })
93- } else {
94- github.rest.issues.createComment({
95- issue_number : context.issue.number,
96- owner : context.repo.owner,
97- repo : context.repo.repo,
98- body : output
99- })
100- }
16+ - name : Checkout code
17+ uses : actions/checkout@v3
18+
19+ - name : Set up Terraform
20+ uses : hashicorp/setup-terraform@v3
21+ with :
22+ terraform_version : 1.7.3 # Specify the Terraform version
23+
24+ - name : Configure AWS Credentials
25+ uses : aws-actions/configure-aws-credentials@v4
26+ with :
27+ aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
28+ aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
29+ aws-region : us-west-2 # Change to your AWS region
30+
31+ - name : Terraform Init
32+ run : terraform init
33+
34+ - name : Terraform Plan
35+ uses : dflook/terraform-plan@v1
36+ with :
37+ path : terraform
0 commit comments