Skip to content

Commit 6bb520c

Browse files
committed
ci: Updated pull request workflow to run plan on changed config.
1 parent 7a5d22c commit 6bb520c

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

.github/workflows/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585
- name: Download plan file
8686
uses: actions/download-artifact@v4
8787
with:
88-
name: tfplan
88+
name: ${{ inputs.config }}-tfplan
8989
path: ./tofu/config/${{ inputs.config }}
9090
- name: Initialize OpenTofu
9191
working-directory: ./tofu/config/${{ inputs.config }}

.github/workflows/plan.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ permissions:
4848

4949
jobs:
5050
plan:
51-
name: Plan changes to ${{ inputs.environment || 'development' }}
51+
name: Plan changes to ${{ inputs.config }} in ${{ inputs.environment || 'development' }}
5252
runs-on: ubuntu-latest
5353
environment: ${{ inputs.environment || 'development' }}
5454
env:
@@ -99,7 +99,7 @@ jobs:
9999
- name: Upload plan file
100100
uses: actions/upload-artifact@v4
101101
with:
102-
name: tfplan
102+
name: ${{ inputs.config }}-tfplan
103103
path: |
104104
./tofu/config/${{ inputs.config }}/plan.txt
105105
./tofu/config/${{ inputs.config }}/tfplan

.github/workflows/pull-request.yaml

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,61 @@ on:
44
pull_request:
55

66
jobs:
7+
configs:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout source code
11+
uses: actions/checkout@v4
12+
- name: Find all configs
13+
id: find
14+
uses: bendrucker/find-terraform-modules@v1
15+
with:
16+
working-directory: tofu/config
17+
- name: Show all matching configs
18+
shell: bash
19+
run: |
20+
mods=(${{ join(fromJSON(steps.find.outputs.modules), ' ') }})
21+
printf "%s\n" "${mods[@]}"
22+
- name: Find all changed files
23+
id: diff
24+
uses: technote-space/get-diff-action@v6
25+
with:
26+
FORMAT: json
27+
- name: Show changed files
28+
run: |
29+
echo "${{ steps.diff.outputs.diff }}"
30+
- name: Get the modified configs
31+
id: modified
32+
uses: actions/github-script@v7
33+
with:
34+
script: |
35+
const configs = ${{ steps.find.outputs.modules }}
36+
const diff = ${{ steps.diff.outputs.diff }}
37+
const modifiedConfigs = configs.filter(
38+
(config) => {
39+
return !!diff.find(file => new RegExp(`^${config}/.+`).test(file))
40+
}
41+
).map(config => config.replace(/^tofu\/config\//, ''))
42+
43+
core.setOutput('configs', modifiedConfigs)
44+
- name: Show modified configs
45+
run: |
46+
echo "${{ steps.modified.outputs.configs }}"
47+
outputs:
48+
configs: ${{ steps.modified.outputs.configs }}
49+
750
plan:
851
uses: ./.github/workflows/plan.yaml
52+
needs: configs
953
permissions:
1054
contents: read
1155
id-token: write
56+
strategy:
57+
matrix:
58+
config: ${{ fromJson(needs.configs.outputs.configs) }}
1259
with:
1360
environment: development
61+
config: ${{ matrix.config }}
1462
secrets:
1563
AWS_REGION: ${{ secrets.AWS_REGION }}
1664
AWS_ROLE_ARN: ${{ secrets.AWS_ROLE_ARN }}
@@ -23,17 +71,20 @@ jobs:
2371

2472
comment:
2573
runs-on: ubuntu-latest
26-
needs: plan
74+
needs:
75+
- configs
76+
- plan
2777
permissions:
2878
contents: read
2979
pull-requests: write
30-
env:
31-
CONFIG_PATH: ./tofu/config/development
80+
strategy:
81+
matrix:
82+
config: ${{ fromJson(needs.configs.outputs.configs) }}
3283
steps:
3384
- name: Download plan file
3485
uses: actions/download-artifact@v4
3586
with:
36-
name: tfplan
87+
name: ${{ matrix.config }}-tfplan
3788
- uses: actions/github-script@v7
3889
with:
3990
github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -45,15 +96,15 @@ jobs:
4596
issue_number: context.issue.number,
4697
})
4798
const botComment = comments.find(comment => {
48-
return comment.user.type === 'Bot' && comment.body.includes('## Plan output')
99+
return comment.user.type === 'Bot' && comment.body.includes('## Plan output for ${{ matrix.config }} config')
49100
})
50101
51102
// Read the contents of the plan.
52103
const fs = require('fs');
53104
const plan = fs.readFileSync('./plan.txt', 'utf8');
54105
55106
// Prepare the format of the comment.
56-
const output = `## Plan output\n\n\`\`\`\n${plan}\n\`\`\``
107+
const output = `## Plan output for ${{ matrix.config }} config\n\n\`\`\`\n${plan}\n\`\`\``
57108
58109
// If we have a comment, update it. Otherwise, create a new one.
59110
if (botComment) {

0 commit comments

Comments
 (0)