This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-pipelines.yml
208 lines (192 loc) · 7.36 KB
/
azure-pipelines.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
trigger:
branches:
include:
- main
- dev
pool:
vmImage: "ubuntu-latest"
variables:
${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
dockerRegistryServiceConnection: "ACR_Connection"
imageRepository: "amundsen"
dockerfilePath: "$(Build.SourcesDirectory)/Dockerfile"
tag: "prod-$(Build.BuildId)"
acrurl: "acrisgreatniceprod.azurecr.io"
deploymentFolder: "$(Build.SourcesDirectory)/manifests/base"
environment: "Production"
${{ if ne(variables['Build.SourceBranch'], 'refs/heads/main') }}:
dockerRegistryServiceConnection: "ACR_Connection"
imageRepository: "amundsen"
dockerfilePath: "$(Build.SourcesDirectory)/Dockerfile"
tag: "dev-$(Build.BuildId)"
acrurl: "acrisgreatniceprod.azurecr.io"
deploymentFolder: "$(Build.SourcesDirectory)/manifests/overlays/dev"
environment: "Development"
parameters:
- name: ForceImagePushImageWithVulnerability
values:
- true
- false
default: false
displayName: Force Image Push Image With Vulnerability
stages:
# first stage will check the power status of AKS, if it is running then it will proceed to the next stage
- stage: CheckAKS
displayName: Check Power Status
jobs:
- deployment: CheckAKS
environment: $(environment)
# job: CheckAKS
displayName: Check Power Status
strategy:
runOnce:
deploy:
steps:
- task: AzureCLI@2
name: aksCheck
inputs:
azureSubscription: "DevOps-006"
scriptType: "bash"
scriptLocation: "scriptPath"
scriptPath: "scripts/aksinit.sh"
- stage: Build
displayName: Build & Test
dependsOn: CheckAKS
condition: and(succeeded(), eq(dependencies.CheckAKS.outputs['CheckAKS.aksCheck.aksRunning'], 'true'))
jobs:
- job: Build
displayName: Build
steps:
- template: docker-tags.yml
parameters:
branch: $(Build.SourceBranchName)
buildId: $(Build.BuildId)
- task: Docker@2
inputs:
containerRegistry: "ACR_Connection"
repository: $(imageRepository)
command: "build"
Dockerfile: "**/Dockerfile"
tags: $(tag)
displayName: Building image
- task: Bash@3
displayName: Save Docker Image as Tar
inputs:
targetType: "inline"
script: "docker save $(acrurl)/$(imageRepository):$(tag) -o $(Pipeline.Workspace)/$(imageRepository):$(tag).tar"
# installing trivy
- task: CmdLine@2
displayName: "Install Trivy"
inputs:
script: |
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy
# scanning image with trivy
- task: CmdLine@2
displayName: "Run trivy scan"
inputs:
script: |
trivy image --severity HIGH, CRITICAL --format template --template "@contrib/junit.tpl" -o test.xml acrisgreatniceprod.azurecr.io/$(imageRepository):$(tag) --ignore-unfixed
# publishing the test results
- ${{ if eq(parameters.ForceImagePushImageWithVulnerability, 'false') }}:
- task: PublishTestResults@2
inputs:
testResultsFormat: "JUnit"
testResultsFiles: "**/test.xml"
mergeTestResults: true
failTaskOnFailedTests: true
testRunTitle: "Trivy - Vulnerabilities Summary"
- ${{ if eq(parameters.ForceImagePushImageWithVulnerability, 'true') }}:
- task: PublishTestResults@2
inputs:
testResultsFormat: "JUnit"
testResultsFiles: "**/test.xml"
mergeTestResults: true
failTaskOnFailedTests: false
testRunTitle: "Trivy - Vulnerabilities Summary"
# publishing the image as artifact
- task: PublishPipelineArtifact@1
displayName: Publish Pipeline Artifacts
inputs:
targetPath: "$(Pipeline.Workspace)/$(imageRepository):$(tag).tar"
artifact: "$(imageRepository)"
publishLocation: "pipeline"
- stage: Push
displayName: Pushing Image to ACR
dependsOn: Build
condition: succeeded()
jobs:
- job: Push
displayName: Push
steps:
# downloading the artifact from the previous stage
- task: DownloadPipelineArtifact@2
displayName: Download Pipeline Artifacts
inputs:
artifact: "$(imageRepository)"
path: "$(Pipeline.Workspace)"
# usign the downloaded artifact to load the image
- task: Bash@3
displayName: Load Docker Image from Tar
inputs:
targetType: "inline"
script: "docker load -i $(Pipeline.Workspace)/$(imageRepository):$(tag).tar"
# pushing image in ACR
- task: Docker@2
inputs:
containerRegistry: "ACR_Connection"
repository: "$(imageRepository)"
command: "push"
tags: $(tag)
displayName: "Pushing image to ACR"
- stage: Deploy
displayName: Deploying to AKS
dependsOn: Push
condition: succeeded()
jobs:
- job: deployment
displayName: "Updating deployment"
steps:
- script: |
echo "##vso[task.setvariable variable=latest]$(tag)"
# this task will match the pattern and replace the value with pipeline variable
- task: replacetokens@5
inputs:
rootDirectory: "$(Build.SourcesDirectory)"
targetFiles: "**/*.yml"
encoding: "auto"
tokenPattern: "custom"
tokenPrefix: "_"
tokenSuffix: "_"
writeBOM: true
actionOnMissing: "warn"
keepToken: false
actionOnNoFiles: "continue"
enableTransforms: false
enableRecursion: false
useLegacyPattern: false
enableTelemetry: true
displayName: "Updating deployment"
- task: AzureCLI@2
inputs:
azureSubscription: "DevOps-006"
scriptType: "bash"
scriptLocation: "inlineScript"
inlineScript: |
az aks install-cli
displayName: "Initializing deployment"
- task: Kubernetes@1
inputs:
connectionType: "Azure Resource Manager"
azureSubscriptionEndpoint: "DevOps-006"
azureResourceGroup: "rg-amit-001"
kubernetesCluster: "aks-test-001"
namespace: "cdp"
command: "apply"
arguments: "-k $(deploymentFolder)"
secretType: "dockerRegistry"
containerRegistryType: "Azure Container Registry"
displayName: "Deploying to AKS"