-
Notifications
You must be signed in to change notification settings - Fork 8
183 lines (170 loc) · 5.79 KB
/
jenkins-deployment.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
name: Jenkins Deployment Test
env:
TF_VAR_fully_qualified_domain_name: ${{ secrets.CI_FULLY_QUALIFIED_DOMAIN_NAME }}
STATE_BUCKET_NAME: ${{ secrets.TF_REMOTE_STATE_BUCKET_NAME }}
# Triggers on any changes to modules/jenkins
on:
pull_request: # change to pull_request before publish
paths:
- 'modules/jenkins/**'
# - '.github/workflows/**'
workflow_dispatch:
permissions:
id-token: write
contents: read
issues: write
jobs:
# Plan: Generates a tf plan of the deployment and posts it as a comment in the triggering PR
plan:
runs-on: ubuntu-latest
environment: aws-ci
defaults:
run:
working-directory: modules/jenkins/examples/complete
steps:
# Retrieve necessary AWS permissions
- name: configure aws credentials
uses: aws-actions/[email protected]
with:
role-to-assume: ${{ secrets.AWS_CI_ROLE_ARN }}
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: ${{ vars.AWS_REGION }}
# Checkout Repository
- name: Checkout Git Repository
uses: actions/[email protected]
with:
ref: ${{ github.ref }}
# Install Terraform
- name: Install Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.6.3
# Inject remote state block
# This is required to enable remote state
- name: Inject Remote State
run: |
cat > backend.tf << EOF
terraform {
backend "s3" {
}
}
# Initialize S3 remote state
# The triggering commit hash is used as the key of the remote state
- name: Terraform init
id: init
run: |
terraform init -backend-config="bucket=${STATE_BUCKET_NAME}" -backend-config="key=${{ github.sha }}" -backend-config="region=${{ vars.AWS_REGION }}"
# Generate tf plan
- name: Terraform plan
id: plan
run: |
terraform plan -no-color
# Post the tf plan as a comment in the triggering PR
- name: Update Pull Request
uses: actions/github-script@v7
with:
github-token: ${{ secrets.USER_TOKEN }}
script: |
const output = #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${{ steps.plan.outputs.stdout }}
\`\`\`
</details>
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
# Deploy: After manual approval, deploys the solution to the designated AWS account
deploy:
needs: [ plan ]
environment: aws-ci
runs-on: ubuntu-latest
defaults:
run:
working-directory: modules/jenkins/examples/complete
steps:
# Checkout Repository
- name: Checkout Git Repository
uses: actions/[email protected]
with:
ref: ${{ github.ref }}
# Retrieve necessary AWS permissions
- name: configure aws credentials
uses: aws-actions/[email protected]
with:
role-to-assume: ${{ secrets.AWS_CI_ROLE_ARN }}
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: ${{ vars.AWS_REGION }}
# Install Terraform
- name: Install Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.6.3
# Inject remote state block
# This is required to enable remote state
- name: Inject Remote State
run: |
cat > backend.tf << EOF
terraform {
backend "s3" {
}
}
# Initialize S3 remote state
# The triggering commit hash is used as the key of the remote state
- name: Terraform init
id: init
run: |
terraform init -backend-config="bucket=${STATE_BUCKET_NAME}" -backend-config="key=${{ github.sha }}" -backend-config="region=${{ vars.AWS_REGION }}"
# Deploys the solution
- name: Terraform apply
run: |
terraform apply -auto-approve
# Destroy: After manual approval, destroy the solution in the designated AWS account
destroy:
needs: [ deploy ]
runs-on: ubuntu-latest
environment: aws-ci
defaults:
run:
working-directory: modules/jenkins/examples/complete
steps:
# Checkout Repository
- name: Checkout Git Repository
uses: actions/[email protected]
with:
ref: ${{ github.ref }}
# Retrieve necessary AWS permissions
- name: configure aws credentials
uses: aws-actions/[email protected]
with:
role-to-assume: ${{ secrets.AWS_CI_ROLE_ARN }}
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: ${{ vars.AWS_REGION }}
# Install Terraform
- name: Install Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.6.3
# Inject remote state block
# This is required to enable remote state
- name: Inject Remote State
run: |
cat > backend.tf << EOF
terraform {
backend "s3" {
}
}
# Initialize S3 remote state
# The triggering commit hash is used as the key of the remote state
- name: Terraform init
id: init
run: |
terraform init -backend-config="bucket=${STATE_BUCKET_NAME}" -backend-config="key=${{ github.sha }}" -backend-config="region=${{ vars.AWS_REGION }}"
# Destroys the solution
- name: Terraform Destroy
run: |
terraform destroy -auto-approve