1+ name : Terraform Plan Custom Action
2+
3+ description : Performs a Terraform plan and returns the output
4+
5+ inputs :
6+ tfDir :
7+ description : The directory where the Terraform templates are located
8+ required : false
9+ default : " "
10+ tfArgs :
11+ description : Extra arguments to pass to terraform
12+ required : false
13+ default : " "
14+ tfWorkspace :
15+ description : The Terraform workspace to select
16+ required : false
17+ default : " default"
18+ gitUser :
19+ description : The Git user used to clone repositories
20+ required : true
21+ gitToken :
22+ description : The Git user token used to clone repositories
23+ required : true
24+ commentPrNumber :
25+ description : Pull Request ID to add the TF Plan comment
26+ required : false
27+ commentTitle :
28+ description : Main title for TF Plan comment on Pull Request
29+ required : false
30+ default : Terraform Plan Results
31+ commentSubTitle :
32+ description : Sub title for TF Plan comment on Pull Request
33+ required : false
34+ default : Terraform Plan
35+
36+ outputs :
37+ tfplan :
38+ description : The Terraform plan output
39+ value : ${{ steps.plan.outputs.plan_output }}
40+ message :
41+ description : Friendly message that shows if there are changes to review
42+ value : ${{ steps.plan.outputs.plan_msg }}
43+ result :
44+ description : The return code of the plan. 0 = no changes, 1 = error, 2 = changes
45+ value : ${{ steps.plan.outputs.plan_result }}
46+
47+ runs :
48+ using : " composite"
49+ steps :
50+ - name : Terraform formatting
51+ shell : bash
52+ working-directory : ${{inputs.tfDir}}
53+ run : |
54+ terraform fmt -check -recursive -diff
55+
56+ - name : Setup Git
57+ shell : bash
58+ env :
59+ GIT_USER : ${{inputs.gitUser}}
60+ GIT_TOKEN : ${{inputs.gitToken}}
61+ run : |
62+ git config --global credential.helper store
63+ echo "https://$GIT_USER:GIT_TOKEN@github.com" >> ~/.git-credentials
64+
65+ - name : Terraform Init
66+ shell : bash
67+ working-directory : ${{inputs.tfDir}}
68+ run : |
69+ terraform init -input=false
70+ terraform workspace select ${{ inputs.tfWorkspace }} || terraform workspace new ${{ inputs.tfWorkspace }}
71+
72+ - name : Terraform Plan
73+ shell : bash
74+ id : plan
75+ working-directory : ${{ inputs.tfDir }}
76+ run : |
77+ PLAN_OUTPUT="$(terraform plan -no-color -detailed-exitcode -out=tfplan ${{ inputs.tfArgs }})" || PLAN_RESULT=$?
78+ echo "plan_result=$PLAN_RESULT" >> $GITHUB_OUTPUT
79+
80+ if [ "${#PLAN_OUTPUT}" -gt 131071 ]; then
81+ PLAN_OUTPUT="The plan output was too big to comment. Please view the actions output for the plan results.";
82+ fi
83+
84+ # echo "plan_output=$PLAN_OUTPUT" >> $GITHUB_OUTPUT
85+
86+ EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
87+ echo "plan_output<<$EOF" >> $GITHUB_OUTPUT
88+ echo "$PLAN_OUTPUT" >> $GITHUB_OUTPUT
89+ echo "$EOF" >> $GITHUB_OUTPUT
90+
91+
92+ case $PLAN_RESULT in
93+ 1)
94+ exit 1
95+ ;;
96+ 2)
97+ MSG="Changes to review! :warning:"
98+ ;;
99+ *)
100+ MSG="No changes :white_check_mark:"
101+ ;;
102+ esac
103+ echo "plan_msg=$MSG" >> $GITHUB_OUTPUT
104+
105+ terraform show -json tfplan > tfplan.json
106+ terraform show tfplan
107+
108+ - name : Add TF Plan comment to PR
109+ if : ${{ inputs.commentPrNumber != '' && steps.plan.outcome == 'success'}}
110+ shell : bash
111+ working-directory : ${{ inputs.tfDir }}
112+ run : |
113+ echo "## ${{ inputs.commentTitle }} 📃" > comment.txt
114+ echo "### ${{ inputs.commentSubTitle }}" >> comment.txt
115+ echo "${{ steps.plan.outputs.plan_msg }}" >> comment.txt
116+ echo "<details><summary>Show Plan</summary>" >> comment.txt
117+ echo "" >> comment.txt
118+ echo "\`\`\`terraform" >> comment.txt
119+ echo "${{ steps.plan.outputs.plan_output }}" >> comment.txt
120+ echo "\`\`\`" >> comment.txt
121+ echo "</details>" >> comment.txt
122+
123+ PR_PAYLOAD="$(echo '{}' | jq --arg body "$(cat comment.txt)" '.body = $body')"
124+
125+ curl -sS \
126+ -X POST \
127+ -H "Accept: application/vnd.github+json" \
128+ -H "Authorization: Bearer ${{ github.token }}"\
129+ -H "X-GitHub-Api-Version: 2022-11-28" \
130+ -L "https://api.github.com/repos/${{ github.repository }}/issues/${{ inputs.commentPrNumber }}/comments" \
131+ --data "$PR_PAYLOAD"
132+
133+ - name : Cleanup
134+ shell : bash
135+ run : rm ~/.git-credentials
136+
137+ branding :
138+ icon : ' award'
139+ color : ' green'
0 commit comments