-
Notifications
You must be signed in to change notification settings - Fork 0
255 lines (251 loc) · 9.24 KB
/
owasp_pygoat_ci_cd.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
name: OWASP_PyGoat_ci_cd
on:
workflow_dispatch:
inputs:
deployInfra:
default: true
type: boolean
required: false
subscriptionName:
default: OWASP_IT Test
type: string
required: false
baseName:
default: gh-pygoat
type: string
required: false
imageName:
default: gh-pygoat
type: string
required: false
location:
description: "Azure location to deploy to"
required: true
default: "canadacentral"
type: choice
options:
- westeurope
- eastus
- eastus2
- westus
- centralus
- northcentralus
- southcentralus
- canadacentral
- canadaeast
- uksouth
- ukwest
environmentName:
description: "Environment to deploy to"
required: true
default: "dev"
type: choice
options:
- dev
- test
- prod
instanceNumber:
default: "006"
type: string
required: false
teardown:
description: Should teardown infrastructure?
default: false
type: boolean
required: true
push:
branches:
- master
- "!features/experimental/*"
paths:
- Dockerfile
- ".azuredevops/pipelines/main-ci-cd.yml"
- infra/bicep/appservice/main.bicep
- "!README.md"
env:
addAzureAdAppSettings: false
azureSubscription: "${{ inputs.subscriptionName }}"
baseName: ${{ inputs.baseName }}-${{ inputs.environmentName }}-${{ inputs.instanceNumber }}
deployInfra: ${{ inputs.deployInfra }}
deploymentName: infra-deployment
dockerfilePath: "${{ github.workspace }}/Dockerfile"
environmentName: ${{ inputs.environmentName }}-${{ inputs.instanceNumber }}
imageName: ${{ inputs.imageName }}
location: ${{ inputs.location }}
resourceGroupName: rg-${{ inputs.baseName }}-${{ inputs.environmentName }}-${{ inputs.instanceNumber }}
templateFile: "${{ github.workspace }}/infra/bicep/appservice/main.bicep"
workingDirectory: "${{ github.workspace }}"
workingDirectoryDockerContext: "${{ github.workspace }}"
permissions:
id-token: write # This is required for requesting the JWT to login to Azure with GitHub Actions and federate identity
contents: read
# federate identity needed:
# repo:devopsabcs-engineering/pygoat:ref:refs/heads/master
# repo:devopsabcs-engineering/pygoat:environment:dev-006
# repo:devopsabcs-engineering/pygoat:environment:dev-006_DESTROYED
# repo:devopsabcs-engineering/pygoat:environment:dev-007
# repo:devopsabcs-engineering/pygoat:environment:dev-007_DESTROYED
# and so on
jobs:
set_version-set_version:
name: Set version
runs-on: ubuntu-latest
if: ${{ github.event.inputs.teardown != 'true' }}
# Permission can be added at job level or workflow level
permissions:
id-token: write #This is required for requesting the JWT
contents: write # This is required to create/push the new git tag
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v3
with:
versionSpec: "5.x"
- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/execute@v3
- name: Display GitVersion outputs
run: |
echo "SemVer: ${{ steps.gitversion.outputs.semVer }}"
- name: Create or update tag
uses: actions/github-script@v3
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT_TOKEN }}
with:
script: |
const tagName = `v${{ steps.gitversion.outputs.semVer }}`;
const ref = `refs/tags/${tagName}`;
const { data: refs } = await github.git.listMatchingRefs({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `tags/${tagName}`
});
if (refs.length > 0) {
await github.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `tags/${tagName}`,
sha: context.sha,
force: true
});
} else {
await github.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: ref,
sha: context.sha
});
}
deployInfra-deploy:
name: Deploy Infra job
needs:
- set_version-set_version
runs-on: ubuntu-latest
environment:
name: ${{ inputs.environmentName }}-${{ inputs.instanceNumber }}
if: success() && ${{ github.event.inputs.deployInfra == 'true' }} && ${{ github.event.inputs.teardown != 'true' }}
permissions:
id-token: write # This is required for requesting the JWT to login to Azure with GitHub Actions and federate identity
contents: read
steps:
- name: checkout
uses: actions/checkout@v4
- name: download artifact
uses: actions/download-artifact@v4
- name: Azure CLI Login ${{ env.azureSubscription }} - deployInfra
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Deploy Infra to ${{ env.azureSubscription }}
run: |-
echo "Deploying to Azure"
az group create --name ${{ env.resourceGroupName }} \
--location ${{ env.location }}
az deployment group create --name ${{ env.deploymentName }} \
--resource-group ${{ env.resourceGroupName }} \
--template-file ${{ env.templateFile }} \
--parameters baseName=${{ env.baseName }} \
--parameters imageName=${{ env.imageName }} \
--parameters addAzureAdAppSettings=${{ env.addAzureAdAppSettings }}
shell: bash
BuildAndPush-BuildImage:
name: Build and push an image to container registry
needs:
- deployInfra-deploy
runs-on: ubuntu-latest
if: success() && ${{ github.event.inputs.teardown == 'false' }}
permissions:
id-token: write # This is required for requesting the JWT to login to Azure with GitHub Actions and federate identity
contents: read
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Azure Login to ${{ env.azureSubscription }}
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Get container registry Name ${{ env.azureSubscription }}
run: |-
echo "Getting container registry"
containerRegistryName=$(az deployment group show --name ${{ env.deploymentName }} \
--resource-group ${{ env.resourceGroupName }} \
--query properties.outputs.containerRegistryName.value \
-o tsv)
echo "Container Registry: $containerRegistryName"
# set output variable to environment variable
echo "containerRegistryName=$containerRegistryName" >> $GITHUB_ENV
shell: bash
working-directory: "${{ env.workingDirectory }}"
- name: Display Container Registry Name
run: echo ${{ env.containerRegistryName }}
- name: Azure Login to ${{ env.azureSubscription }}
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Build and push image ${{ env.azureSubscription }}
run: |-
echo "Building and pushing image to container registry"
az acr build --registry ${{ env.containerRegistryName }} \
--image ${{ env.imageName }}:${{ github.run_id }} \
--image ${{ env.imageName }}:latest \
--file ${{ env.dockerfilePath }} \
${{ env.workingDirectoryDockerContext }}
shell: bash
Teardown-Teardown:
name: Teardown infrastructure
needs: []
runs-on: ubuntu-latest
environment:
name: "${{ inputs.environmentName }}-${{ inputs.instanceNumber }}_DESTROYED"
if: ${{ github.event.inputs.teardown == 'true' }}
permissions:
id-token: write # This is required for requesting the JWT to login to Azure with GitHub Actions and federate identity
contents: read
steps:
- uses: actions/checkout@v4
- uses: azure/login@v2
name: Sign in to Azure with OIDC
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Delete Resource Group ${{ env.resourceGroupName }} if exist
uses: azure/CLI@v2
with:
inlineScript: |
if [[ $(az group exists -n ${{ env.resourceGroupName }}) == true ]]
then
echo "Resource group exists. Deleting..."
az group delete -n ${{ env.resourceGroupName }} --yes
else
echo "Resource group does not exist in the subscription. Nothing to delete."
fi