1717
1818# This workflow modifies the GCP User Roles when the infra/users.yml file is updated.
1919# It applies the changes using Terraform to manage the IAM roles for users defined in the users.yml
20+ # If the workflow is triggered by a pull request, it will post the Terraform plan as a comment on the PR
21+ # as a code block for easy review.
2022
2123name : Modify the GCP User Roles according to the infra/users.yml file
2224
2830 - main
2931 paths :
3032 - ' infra/iam/users.yml'
33+ pull_request :
34+ paths :
35+ - ' infra/iam/users.yml'
3136
3237# This allows a subsequently queued workflow run to interrupt previous runs
3338concurrency :
@@ -37,6 +42,7 @@ concurrency:
3742# Setting explicit permissions for the action to avoid the default permissions which are `write-all` in case of pull_request_target event
3843permissions :
3944 contents : read
45+ pull-requests : write
4046
4147jobs :
4248 beam_UserRoles :
5763 - name : Terraform Plan
5864 working-directory : ./infra/iam
5965 run : terraform plan -out=tfplan
66+
67+ - name : Convert plan to plaintext
68+ if : github.event_name == 'pull_request'
69+ working-directory : ./infra/iam
70+ run : terraform show -no-color tfplan > tfplan.txt
71+
72+ - name : Create comment body
73+ if : github.event_name == 'pull_request'
74+ run : |
75+ echo "### Terraform Plan for User Roles Changes" > comment_body.txt
76+ echo '```' >> comment_body.txt
77+ cat ./infra/iam/tfplan.txt >> comment_body.txt
78+ echo '```' >> comment_body.txt
79+
80+ - name : Upload plan as a comment to PR
81+ if : github.event_name == 'pull_request'
82+ env :
83+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
84+ GH_REPO : ${{ github.repository }}
85+ run : gh pr comment ${{ github.event.pull_request.number }} --body-file comment_body.txt
86+
6087 - name : Terraform Apply
88+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
6189 working-directory : ./infra/iam
6290 run : terraform apply -auto-approve tfplan
0 commit comments