Skip to content

Commit 5dda29f

Browse files
committed
feat: add PR plan comments and environment protection
- Restructure workflows into separate test/plan/apply jobs - Add PR comment integration for plan output - Add permissions block for PR write access - Add environment protection for apply job - Rename workflows to OpenTofu
1 parent e576ae8 commit 5dda29f

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

.github/workflows/apply.yml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
---
2-
name: Apply - Terraform via ARC
2+
name: Apply - OpenTofu via ARC
3+
34
on:
45
push:
56
branches:
67
- main
78
workflow_dispatch:
89

10+
permissions:
11+
contents: read
12+
pull-requests: write
13+
914
env:
1015
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
1116

1217
jobs:
13-
deploy:
18+
test:
19+
name: Pre-commit Tests
20+
runs-on: arc-dind
21+
container: image-registry.openshift-image-registry.svc:5000/public-registry/terraform-runner:latest
22+
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v4
26+
27+
- name: Run Git as root
28+
run: git config --global --add safe.directory '*'
29+
30+
- name: Install SSH key
31+
uses: shimataro/ssh-key-action@v2
32+
with:
33+
key: ${{ secrets.SSH_PRIVATE_KEY }}
34+
known_hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
35+
36+
- name: Copy SSH area
37+
run: cp -r /root/.ssh /github/home/
38+
39+
- name: Run tests
40+
run: make test
41+
42+
apply:
43+
name: OpenTofu Apply
1444
runs-on: arc-dind
1545
container: image-registry.openshift-image-registry.svc:5000/public-registry/terraform-runner:latest
46+
needs: [test]
47+
environment: production
1648

1749
steps:
18-
- uses: actions/checkout@v4
50+
- name: Checkout
51+
uses: actions/checkout@v4
1952

2053
- name: Run Git as root
2154
run: git config --global --add safe.directory '*'
@@ -29,5 +62,5 @@ jobs:
2962
- name: Copy SSH area
3063
run: cp -r /root/.ssh /github/home/
3164

32-
- name: Terraform apply
65+
- name: OpenTofu Apply
3366
run: make apply

.github/workflows/plan.yml

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
---
2-
name: Plan - Terraform via ARC
2+
name: Plan - OpenTofu via ARC
33

44
on:
55
pull_request:
66
types: [opened, reopened, synchronize]
77
workflow_dispatch:
88

9+
permissions:
10+
contents: read
11+
pull-requests: write
12+
913
env:
1014
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
1115

1216
jobs:
13-
plan:
17+
test:
18+
name: Pre-commit Tests
1419
runs-on: arc-dind
1520
container: image-registry.openshift-image-registry.svc:5000/public-registry/terraform-runner:latest
1621

1722
steps:
18-
- uses: actions/checkout@v4
23+
- name: Checkout
24+
uses: actions/checkout@v4
1925

2026
- name: Run Git as root
2127
run: git config --global --add safe.directory '*'
@@ -28,8 +34,62 @@ jobs:
2834

2935
- name: Copy SSH area
3036
run: cp -r /root/.ssh /github/home/
37+
3138
- name: Run tests
3239
run: make test
3340

34-
- name: Terraform plan
35-
run: make plan
41+
plan:
42+
name: OpenTofu Plan
43+
runs-on: arc-dind
44+
container: image-registry.openshift-image-registry.svc:5000/public-registry/terraform-runner:latest
45+
needs: [test]
46+
47+
steps:
48+
- name: Checkout
49+
uses: actions/checkout@v4
50+
51+
- name: Run Git as root
52+
run: git config --global --add safe.directory '*'
53+
54+
- name: Install SSH key
55+
uses: shimataro/ssh-key-action@v2
56+
with:
57+
key: ${{ secrets.SSH_PRIVATE_KEY }}
58+
known_hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
59+
60+
- name: Copy SSH area
61+
run: cp -r /root/.ssh /github/home/
62+
63+
- name: OpenTofu Plan
64+
id: plan
65+
run: |
66+
make plan || true
67+
68+
sed -n '/OpenTofu will perform the following actions:/,$p' plan-output.txt > plan-filtered.txt
69+
70+
if [ ! -s plan-filtered.txt ]; then
71+
grep -A 2 "No changes" plan-output.txt > plan-filtered.txt || echo "No plan output found" > plan-filtered.txt
72+
fi
73+
74+
tail -n 1000 plan-filtered.txt > plan-filtered-truncated.txt
75+
mv plan-filtered-truncated.txt plan-filtered.txt
76+
77+
- name: Comment PR with Plan
78+
uses: actions/github-script@v7
79+
with:
80+
github-token: ${{ secrets.GITHUB_TOKEN }}
81+
script: |
82+
const fs = require('fs');
83+
const planOutput = fs.readFileSync('plan-filtered.txt', 'utf8');
84+
85+
const output = `#### OpenTofu Plan
86+
\`\`\`
87+
${planOutput}
88+
\`\`\`
89+
`;
90+
github.rest.issues.createComment({
91+
issue_number: context.issue.number,
92+
owner: context.repo.owner,
93+
repo: context.repo.repo,
94+
body: output
95+
});

0 commit comments

Comments
 (0)