generated from deploymenttheory/Template
-
Notifications
You must be signed in to change notification settings - Fork 9
187 lines (165 loc) · 6.46 KB
/
01-terraform-plan-sandbox.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# ref: https://sentenz.github.io/convention/convention/conventional-commits/
name: "01 - terraform speculative plan: sandbox"
on:
workflow_dispatch:
push:
branches:
- 'feat-*'
- 'fix-*'
- 'docs-*'
- 'style-*'
- 'refactor-*'
- 'test-*'
- 'chore-*'
- 'build-*'
- 'ci-*'
- 'perf-*'
paths:
- '**/*.tf'
env:
TF_CLOUD_ORGANIZATION: "deploymenttheory"
TF_API_TOKEN: "${{ secrets.TF_API_TOKEN }}"
TF_WORKSPACE: "terraform-jamfpro-sandbox"
CONFIG_DIRECTORY: "workload/terraform/jamfpro"
permissions:
contents: write
id-token: write
pull-requests: write
jobs:
branch-check:
if: github.event_name == 'workflow_dispatch' # only need to check branch for manual triggers
name: "branch check"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4.1.7
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Extract branch name
id: extract_branch
run: echo "branch=${{ github.ref_name }}" >> $GITHUB_OUTPUT
- name: Validate Branch Name
run: |
branch="${{ steps.extract_branch.outputs.branch }}"
if [[ "$branch" =~ ^(feat|fix|docs|style|refactor|test|chore|build|ci|perf)- ]]; then
echo "Success: Branch name '$branch' is valid."
exit 0
else
echo "Error: Branch name must start with 'feat-', 'fix-', 'docs-', 'style-', 'refactor-', 'test-', 'chore-', 'build-', 'ci-', or 'perf-'"
exit 1
fi
terraform-plan:
uses: ./.github/workflows/terraform-plan.yml
with:
tf_cloud_organization: "deploymenttheory"
tf_workspace: "terraform-jamfpro-sandbox"
target_environment: "sandbox"
debug: true
config_directory: "workload/terraform/jamfpro"
release_version: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref_name }}
secrets:
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }}
update-pr:
needs: terraform-plan
runs-on: ubuntu-latest
steps:
- name: Create or Update PR
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const getHeadBranch = () => {
if (context.eventName === 'pull_request') {
return context.payload.pull_request.head.ref;
} else if (context.eventName === 'push') {
return context.ref.replace('refs/heads/', '');
} else {
return context.ref.replace('refs/heads/', '');
}
};
const headBranch = getHeadBranch();
const baseBranch = 'sandbox';
console.log(`Head branch: ${headBranch}`);
console.log(`Base branch: ${baseBranch}`);
// Check if PR already exists
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.repo.owner}:${headBranch}`,
base: baseBranch,
state: 'open'
});
let pr;
if (prs.length === 0) {
// Create new PR
try {
const { data: newPr } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `🔄 Terraform changes from ${headBranch}`,
head: headBranch,
base: baseBranch,
body: '🚀 This PR contains Terraform changes.'
});
pr = newPr;
} catch (error) {
console.error('Error creating PR:', error.message);
return;
}
} else {
pr = prs[0];
}
function getStatusSummary(status, planStatus, add, change, destroy) {
if (status !== 'Success') return `❌ Plan ${status}`;
switch (planStatus) {
case 'planned':
if (destroy > 0) return '🚨 Destructive changes detected';
if (change > 0) return '⚠️ Resource modifications planned';
if (add > 0) return '✨ New resources to be added';
return '✅ No changes required';
case 'pending':
return '⏳ Plan pending';
case 'running':
return '🔄 Plan in progress';
case 'errored':
return '🚫 Plan encountered an error';
case 'canceled':
return '🛑 Plan was canceled';
case 'cost_estimated':
return '💰 Cost estimation completed';
case 'policy_checked':
return '📋 Policy check completed';
case 'planned_and_finished':
return '🏁 Plan completed and finished';
default:
return `ℹ️ Plan status: ${planStatus}`;
}
}
const statusSummary = getStatusSummary(
'${{ needs.terraform-plan.outputs.status }}',
'${{ needs.terraform-plan.outputs.plan_status }}',
${{ needs.terraform-plan.outputs.add }},
${{ needs.terraform-plan.outputs.change }},
${{ needs.terraform-plan.outputs.destroy }}
);
// Add Terraform plan results to PR
const planOutput = `### 🔍 Terraform Plan Results
${statusSummary}
\`\`\`diff
+ Plan: ${{ needs.terraform-plan.outputs.add }} to add
~ ${{ needs.terraform-plan.outputs.change }} to change
- ${{ needs.terraform-plan.outputs.destroy }} to destroy
\`\`\`
📊 [View full plan details](${{ needs.terraform-plan.outputs.run_link }})
Additional Information:
- Operation Result: ${{ needs.terraform-plan.outputs.status }}
- Plan ID: ${{ needs.terraform-plan.outputs.plan_id }}
---
⚠️ Please review these changes carefully before merging.
`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: planOutput
});