Skip to content

Commit d45cbb3

Browse files
committed
GCP User IAM workflow modified to post Terraform plan as a comment on PRs
1 parent 24d3a7b commit d45cbb3

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

.github/workflows/beam_Infrastructure_UsersPermissions.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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

2123
name: Modify the GCP User Roles according to the infra/users.yml file
2224

@@ -28,6 +30,9 @@ on:
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
3338
concurrency:
@@ -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
3843
permissions:
3944
contents: read
45+
pull-requests: write
4046

4147
jobs:
4248
beam_UserRoles:
@@ -57,6 +63,28 @@ jobs:
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

Comments
 (0)