-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-container-app.yaml
209 lines (188 loc) · 6.72 KB
/
build-container-app.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
parameters:
- name: name
type: string
- name: containerRepository
type: string
default:
- name: framework
type: string
default: node
- name: sonarcloud
type: boolean
default: true
- name: project
type: string
default:
- name: deploy
type: boolean
default: false
variables:
- group: SonarCloud
- 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: containerRepository
value:
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"
- 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: 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 OWASP 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 OWASP 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
- 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.4.0-dotnet6.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))