forked from drajer-health/eCRNow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure-pipelines-ecrnow.yml
More file actions
210 lines (191 loc) Β· 7.17 KB
/
Copy pathazure-pipelines-ecrnow.yml
File metadata and controls
210 lines (191 loc) Β· 7.17 KB
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
trigger:
branches:
include:
- resp-net-fixes
pr: none
variables:
- group: ix-prod
- name: vmImageName
value: 'Default'
- name: imageRepository
value: 'ecrNow'
- name: dockerfilePath
value: 'Dockerfile'
- name: dockerRegistryServiceConnection
value: 'acr-ixdatabridge-connection'
- name: helmChartPath
value: 'helm/ixdatabridge-prod'
- name: azureRMServiceConnection
value: 'azsc-ixdatabridge'
- name: aksNamespace
value: 'ixdatabridge'
- name: aksResourceGroup
value: 'rg-infra-nonprod-eus2'
- name: aksClusterName
value: 'aks-infra-nonprod-eus2'
- name: environment
value: 'dev'
- name: Repository
value: 'acrdcbnpinfranonprodeus2.azurecr.io/ecrNow'
- name: snykServiceConnection
value: 'snyk-ixdata-connection'
- name: sonarQubeServiceConnection
value: 'sonar-ixdata-connection'
stages:
- stage: Build
displayName: 'Build Stage'
jobs:
- job: BuildJob
displayName: 'Build WARs, SonarQube Analysis, Snyk Scan, Create Docker Image'
pool:
name: 'Default'
steps:
- checkout: self
- task: JavaToolInstaller@0
displayName: 'Install JDK 17'
inputs:
versionSpec: '17'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
- task: SonarQubePrepare@5
displayName: 'Prepare SonarQube Analysis'
inputs:
SonarQube: '$(sonarQubeServiceConnection)'
scannerMode: 'CLI'
configMode: 'manual'
cliProjectKey: 'eCRNow'
cliProjectName: 'eCRNow'
extraProperties: |
sonar.projectVersion=$(Build.BuildId)
sonar.coverage.jacoco.xmlReportPaths=**/target/site/jacoco/jacoco.xml
- task: Maven@3
displayName: 'Run SonarQube Analysis (Maven Build + Scan)'
inputs:
mavenPomFile: 'pom.xml'
goals: 'clean verify sonar:sonar'
publishJUnitResults: false
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.17'
mavenVersionOption: 'Default'
options: >
-DskipTests=false
-Dng.env=$(uiconfigurations)
-Dsonar.sources=src/main/java
-Dsonar.java.test.inclusions=**/*Test.java
env:
MAVEN_OPTS: "-Xms4096m"
- task: PublishTestResults@2
displayName: 'Publish Unit Test Results'
inputs:
testResultsFiles: '**/target/surefire-reports/TEST-*.xml'
testRunTitle: 'Java Unit Tests'
mergeTestResults: true
failTaskOnFailedTests: false
- task: SonarQubePublish@5
displayName: 'Publish SonarQube Quality Gate Result'
inputs:
pollingTimeoutSec: '300'
- task: SnykSecurityScan@1
displayName: 'Snyk Security Scan (Maven Dependencies)'
inputs:
serviceConnectionEndpoint: $(snykServiceConnection)
testType: 'repo'
severityThreshold: 'low'
monitorWhen: 'always'
failOnIssues: true
- script: |
echo "π Determining image tag..."
BRANCH_NAME=$(echo $(Build.SourceBranchName) | sed 's|/|-|g')
if [ "$BRANCH_NAME" == "master" ]; then
TAG="eCRNow-$(Build.BuildId)"
else
TAG="${BRANCH_NAME}-$(Build.BuildId)"
fi
echo "β
TAG determined: $TAG"
echo "##vso[task.setvariable variable=tag;isOutput=true]$TAG"
echo $TAG > tag.txt
name: SetTag
displayName: 'Set and Persist Image Tag'
- script: |
TAG=$(cat tag.txt)
echo "Docker tag read from file: $TAG"
echo "##vso[task.setvariable variable=tag]$TAG"
displayName: 'Read Persisted Tag for Docker'
- task: Docker@2
displayName: 'Build and Push Docker Image'
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
buildContext: $(System.DefaultWorkingDirectory)
tags: |
$(tag)
latest
- task: SnykSecurityScan@1
displayName: 'Snyk Security Scan (Docker Image)'
continueOnError: true
inputs:
serviceConnectionEndpoint: $(snykServiceConnection)
testType: 'container'
dockerImageName: '$(Repository):$(tag)'
severityThreshold: 'low'
monitorWhen: 'always'
failOnIssues: true
- stage: Deploy
displayName: 'Prod'
dependsOn: Build
condition: succeeded('Build')
variables:
tag: $[ stageDependencies.Build.BuildJob.outputs['SetTag.tag'] ]
jobs:
- deployment: Deploy
displayName: 'Deploy to AKS using Helm'
environment: '$(aksNamespace)'
pool:
name: 'Default'
strategy:
runOnce:
deploy:
steps:
- checkout: self
- script: |
safe_branch=$(echo "$(Build.SourceBranchName)" | tr '[:upper:]' '[:lower:]' | sed 's#[^a-z0-9]#-#g' | cut -c1-30)
release_name="ixdatabridge-$safe_branch"
echo "Sanitized Helm release name: $release_name"
echo "##vso[task.setvariable variable=helmReleaseName]$release_name"
displayName: 'Sanitize Helm Release Name'
- task: AzureCLI@2
displayName: 'Configure kubectl with AKS credentials'
inputs:
azureSubscription: '$(azureRMServiceConnection)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "π Getting AKS credentials..."
az aks get-credentials --resource-group $(aksResourceGroup) --name $(aksClusterName) --overwrite-existing
- script: |
echo "π₯ Deploying Helm chart with tag: $(tag)"
echo "π Checking if Helm release $(helmReleaseName) exists..."
if helm status "$(helmReleaseName)" --namespace $(aksNamespace); then
echo "π Helm release exists. Upgrading the release..."
helm upgrade "$(helmReleaseName)" $(System.DefaultWorkingDirectory)/$(helmChartPath) \
--namespace $(aksNamespace) \
--values $(System.DefaultWorkingDirectory)/$(helmChartPath)/values-$(environment).yml \
--set image.repository=$(Repository),image.tag=$(tag) \
--wait \
--timeout 15m
else
echo "π Helm release does not exist. Installing the release..."
helm install "$(helmReleaseName)" $(System.DefaultWorkingDirectory)/$(helmChartPath) \
--namespace $(aksNamespace) \
--values $(System.DefaultWorkingDirectory)/$(helmChartPath)/values-$(environment).yml \
--set image.repository=$(Repository),image.tag=$(tag) \
--wait \
--timeout 15m
fi
displayName: 'Helm Upgrade if Exists, Otherwise Install - ixdatabridge'
- script: |
echo "β³ Waiting for an additional 2 minutes to ensure full stabilization..."
sleep 120
displayName: 'Post-Deployment Wait for Stabilization'