-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.yaml
281 lines (253 loc) · 8.98 KB
/
build.yaml
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
parameters:
- name: name
type: string
- name: containerRepository
type: string
default:
- name: helmChartPath
type: string
default:
- name: framework
type: string
default: node
- name: sonarcloud
type: boolean
default: true
- name: project
type: string
default:
- name: namespace
type: string
default: default
- name: deploy
type: boolean
default: false
variables:
- group: SonarCloud
- group: Helm
- name: repo
value: ${{ parameters.name }}
- name: testFileExists
value: false
- name: zapFileExists
value: false
- name: releaseExists
value: false
- name: version
value:
- name: targetFramework
value:
- name: sonarImageVersion
value:
- name: helmRepo
value: helm-charts
- name: containerRepository
value:
- name: helmChartPath
value:
- name: helmRepository
value: services.lynxmagnus.com:8080
jobs:
- job: Build
pool:
vmImage: ubuntu-latest
workspace:
clean: all
steps:
- task: PowerShell@2
displayName: Set repository name
inputs:
targetType: 'inline'
script: |
$repo = '$(Build.Repository.Name)'.Split('/')[-1]
Write-Host "##vso[task.setvariable variable=repo]$repo"
- task: PowerShell@2
displayName: Set variables
inputs:
targetType: 'inline'
script: |
if (!([string]::IsNullOrEmpty('${{ parameters.containerRepository }}')))
{
$containerRepository = '${{ parameters.containerRepository }}'
}
else
{
$containerRepository = 'johnwatson484/${{ parameters.name }}'
}
Write-Host "##vso[task.setvariable variable=containerRepository;]$containerRepository"
if (!([string]::IsNullOrEmpty('${{ parameters.helmChartPath }}')))
{
$helmChartPath = '${{ parameters.helmChartPath }}'
}
else
{
$helmChartPath = 'helm/${{ parameters.name }}'
}
Write-Host "##vso[task.setvariable variable=helmChartPath;]$helmChartPath"
- task: PowerShell@2
displayName: Set version (Node.js)
inputs:
targetType: 'inline'
script: |
$packageVersion = (Get-Content 'package.json' | ConvertFrom-Json).version
Write-Host "##vso[task.setvariable variable=version;]$packageVersion"
condition: and(succeeded(), eq('${{ parameters.framework }}', 'node'))
- task: PowerShell@2
displayName: Set version (.NET)
inputs:
targetType: 'inline'
script: |
$csprojVersion = ([xml](Get-Content '${{ parameters.project }}/${{ parameters.project }}.csproj')).Project.PropertyGroup.Version
Write-Host "##vso[task.setvariable variable=version;]$csprojVersion"
condition: and(succeeded(), eq('${{ parameters.framework }}', 'net'))
- task: PowerShell@2
displayName: Set version (File)
inputs:
targetType: 'inline'
script: |
$fileVersion = (Get-Content 'VERSION')
Write-Host "##vso[task.setvariable variable=version;]$fileVersion"
condition: and(succeeded(), ne('${{ parameters.framework }}', 'net'), ne('${{ parameters.framework }}', 'node'))
- task: PowerShell@2
displayName: Set target framework (.NET)
inputs:
targetType: 'inline'
script: |
$csprojVersion = ([xml](Get-Content '${{ parameters.project }}/${{ parameters.project }}.csproj')).Project.PropertyGroup.TargetFramework
Write-Host "##vso[task.setvariable variable=targetFramework;]$csprojVersion"
condition: and(succeeded(), eq('${{ parameters.framework }}', 'net'))
- task: HelmInstaller@1
displayName: Install Helm
inputs:
helmVersionToInstall: 'latest'
- script: |
helm dependency update $(helmChartPath)
helm lint $(helmChartPath)
displayName: Helm lint
- task: PowerShell@2
displayName: Check for tests
inputs:
targetType: 'inline'
script: |
$fileExists = Test-Path -Path "docker-compose.test.yaml"
Write-Host "##vso[task.setvariable variable=testFileExists;]$fileExists"
- script: |
docker compose -f docker-compose.yaml -f docker-compose.test.yaml -p $(repo)-test-$(Build.BuildId) build
displayName: Build test image
condition: and(succeeded(), eq(variables.testFileExists, true))
- script: |
docker compose -f docker-compose.yaml -f docker-compose.test.yaml -p $(repo)-test-$(Build.BuildId) run --rm $(repo)
displayName: Run tests
condition: and(succeeded(), eq(variables.testFileExists, true))
- task: PowerShell@2
displayName: Check for ZAP tests
inputs:
targetType: 'inline'
script: |
$fileExists = Test-Path -Path "docker-compose.zap.yaml"
Write-Host "##vso[task.setvariable variable=zapFileExists;]$fileExists"
- script: |
docker compose -f docker-compose.yaml -f docker-compose.zap.yaml -p $(repo)-zap-$(Build.BuildId) run --rm $(repo)-zap
displayName: Run ZAP tests
condition: and(succeeded(), eq(variables.zapFileExists, true))
- task: Docker@2
displayName: Build image
inputs:
containerRegistry: 'DockerHub'
repository: '$(containerRepository)'
command: 'build'
Dockerfile: 'Dockerfile'
tags: |
$(version)
latest
addPipelineData: false
- task: Docker@2
displayName: Push image
inputs:
containerRegistry: 'DockerHub'
repository: '$(containerRepository)'
command: 'push'
tags: |
$(version)
latest
addPipelineData: false
- script: |
sed -i -e 's/image: .*/image: johnwatson484\/${{ parameters.name }}:$(version)/' $(helmChartPath)/values.yaml
displayName: Update Helm values
- script: |
helm package $(helmChartPath) --version $(version) --destination $(helmRepo) --dependency-update
displayName: Package Helm
- script: |
result=$(curl -v -s -o /dev/null -w "%{http_code}" --data-binary "@$(helmRepo)/$(repo)-$(version).tgz" http://$(username):$(password)@$(helmRepository)/api/charts)
if [ "$result" == "201" ]
then
echo "OK"
else
echo $result
exit 1
fi
displayName: Publish Helm chart to Chart Museum
- task: PowerShell@2
displayName: Set Sonar image version (.NET)
inputs:
targetType: 'inline'
script: |
switch ("$(targetFramework)")
{
"netcoreapp3.1" { $tag = "1.2.3-dotnet3.1"; Break }
"net6.0" { $tag = "1.5.0-dotnet6.0"; Break }
"net8.0" { $tag = "1.6.0-dotnet8.0"; Break }
}
Write-Host "##vso[task.setvariable variable=sonarImageVersion;]$tag"
condition: and(succeeded(), eq('${{ parameters.framework }}', 'net'))
- script: |
cd $(Agent.BuildDirectory)/s/$(repo)
docker run \
-v $(pwd)/:/home/dotnet/project \
-e SONAR_PROJECT_KEY=johnwatson484_$(repo) \
-e SONAR_TOKEN=$(token) \
-e SONAR_ORGANIZATION_KEY=johnwatson484 \
--rm \
defradigital/ffc-dotnet-core-sonar:$(sonarImageVersion)
displayName: SonarCloud analysis (.NET)
condition: and(succeeded(), eq('${{ parameters.framework }}', 'net'), eq('${{ parameters.sonarcloud }}', true))
- task: PowerShell@2
displayName: Check GitHub release
inputs:
targetType: 'inline'
script: |
$releaseExists = (Invoke-RestMethod -SkipHttpErrorCheck -Uri 'https://api.github.com/repos/johnwatson484/$(repo)/releases/tags/$(version)').tag_name -eq '$(version)'
Write-Host "##vso[task.setvariable variable=releaseExists;]$releaseExists"
- task: GitHubRelease@1
displayName: GitHub release
inputs:
gitHubConnection: 'John D Watson PAT'
repositoryName: '$(Build.Repository.Name)'
action: 'create'
target: '$(Build.SourceVersion)'
tagSource: 'userSpecifiedTag'
tag: '$(version)'
title: '$(version)'
releaseNotesSource: 'inline'
changeLogCompareToRelease: 'lastFullRelease'
changeLogType: 'commitBased'
continueOnError: true
condition: and(succeeded(), eq(variables.releaseExists, false))
- script: |
helm repo add lynxmagnus http://$(helmRepository)
helm repo update
displayName: 'Add Helm repository'
condition: and(succeeded(), eq(${{ parameters.deploy }}, true))
- task: HelmDeploy@1
displayName: Deploy to Kubernetes
inputs:
connectionType: 'Kubernetes Service Connection'
kubernetesServiceConnection: 'Contabo'
namespace: '${{ parameters.namespace }}'
command: 'upgrade'
chartType: 'Name'
chartName: 'lynxmagnus/$(repo)'
chartVersion: '$(version)'
releaseName: '$(repo)'
arguments: '--create-namespace --atomic'
condition: and(succeeded(), eq(${{ parameters.deploy }}, true))