-
Notifications
You must be signed in to change notification settings - Fork 10
/
azure-pipelines.yml
283 lines (273 loc) · 10.2 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
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
282
283
# - requires:
# https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarcloud
# https://marketplace.visualstudio.com/items?itemName=SimondeLang.sonarcloud-buildbreaker
# https://marketplace.visualstudio.com/items?itemName=gittools.gitversion
#
# - CI/CD for git flow
# - for linux environment
variables:
organization: 'barradas'
solution_path: 'Mongo.CRUD.sln'
sdk_project_name: 'Mongo.CRUD'
sdk_project_path: 'Mongo.CRUD/Mongo.CRUD.csproj'
net_version: 'netstandard2.0'
execute_test: 'true'
test_project_base_folder: 'Mongo.CRUD.Tests'
test_project_path: 'Mongo.CRUD.Tests/Mongo.CRUD.Tests.csproj'
execute_sonarcloud: 'true'
sonarcloud_project: 'ThiagoBarradas_mongo-crud-dotnet'
sonarcloud_organization: 'thiagobarradas-github'
sonarcloud_account: 'SonarCloudBarradas'
trigger:
branches:
include:
- release/*
- hotfix/*
- develop
tags:
include:
- '*'
pr:
branches:
include:
- develop
- master
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: BuildApp
jobs:
- job: UpdateBuildVersion
workspace:
clean: all
variables:
current_counter: $[counter(variables['Build.SourceBranchName'], 1)]
steps:
- bash: |
if [[ "$(Build.SourceVersionMessage)" =~ "hotfix/" ]];
then
echo "##vso[task.setvariable variable=IsHotfix;isOutput=true]true"
echo "##vso[task.setvariable variable=PreviousPrefix;isOutput=true]hotfix"
else
echo "##vso[task.setvariable variable=IsHotfix;isOutput=true]false"
echo "##vso[task.setvariable variable=PreviousPrefix;isOutput=true]release"
fi
if [[ "$(Build.SourceBranch)" =~ "/hotfix/" ]] ||
[[ "$(Build.SourceBranch)" =~ "/release/" ]];
then
echo "Generate Preview Release Version"
echo "Version: $(Build.SourceBranchName)"
echo " $(Build.SourceBranchName)-preview.$(current_counter)"
echo "##vso[build.updatebuildnumber]$(Build.SourceBranchName)-preview.$(current_counter)"
echo "##vso[task.setvariable variable=PureVersion;isOutput=true]$(Build.SourceBranchName)"
elif [[ "$(Build.SourceBranch)" =~ "/tags/" ]];
then
echo "Generate Release Version"
echo "Version: $(Build.SourceBranchName)"
echo "##vso[build.updatebuildnumber]$(Build.SourceBranchName)"
echo "##vso[task.setvariable variable=SonarMasterWhenTag;isOutput=true]sonar.branch.name=master"
echo "##vso[task.setvariable variable=PureVersion;isOutput=true]$(Build.SourceBranchName)"
else
echo "Generate Development Version"
echo "##vso[build.updatebuildnumber]$(Build.BuildNumber)-develop"
echo "Version: $(Build.BuildNumber)-develop"
fi
displayName: Update Version Number
name: UpdateBuildVersionTask
- bash: |
STAGING_URL="https://dev.azure.com/$(organization)/$(System.TeamProject)/_apis/build/status/$(System.DefinitionId)?branchName=$(UpdateBuildVersionTask.PreviousPrefix)/$(Build.BuildNumber)&stageName=DeployPackage"
STAGING_RESULT=$`curl --silent $STAGING_URL`
echo "URL: $STAGING_URL"
echo "RESULT: $STAGING_RESULT"
SUCCEEDED=$`echo $STAGING_RESULT | grep -P 'succeeded' -o | head -n 1`
if [[ "$STAGING_RESULT" =~ "succeeded" ]];
then
echo "$PREVIOUS_PREFIX branch is ok!"
else
echo "$PREVIOUS_PREFIX branch is not ok!"
exit 1
fi
condition: contains(variables['Build.SourceBranch'], '/tags/')
displayName: Break if release/hotfix branch is not passing
name: BreakIsReleaseOrHotfixIsNotPassing
- job: Build
dependsOn:
- UpdateBuildVersion
condition: succeeded()
workspace:
clean: all
steps:
- script: |
pwd && ls -la
dotnet restore "$(solution_path)"
dotnet publish -c Release -o $(System.DefaultWorkingDirectory)/bin "$(sdk_project_path)"
mkdir artifact
cp -r $(System.DefaultWorkingDirectory)/bin artifact/bin
displayName: Build Application
- task: PublishPipelineArtifact@1
displayName: Store Application Artifact
inputs:
path: $(System.DefaultWorkingDirectory)/artifact
artifact: $(sdk_project_name)
- job: UnitTests
dependsOn:
- UpdateBuildVersion
- Build
workspace:
clean: all
condition: and(succeeded(), eq(variables['execute_test'], 'true'))
steps:
- script: |
dotnet tool install --global coverlet.console
dotnet tool install dotnet-reportgenerator-globaltool --tool-path .
dotnet add $(test_project_path) package coverlet.msbuild
dotnet add $(test_project_path) package XunitXml.TestLogger
dotnet test /p:CollectCoverage=true \
/p:CoverletOutputFormat=\"opencover,cobertura\" \
/p:CoverletOutput=results/result \
/p:Exclude=[xunit.*]* "$(solution_path)" \
--test-adapter-path:. --logger:"xunit;LogFilePath=results/result.xunit.xml"
./reportgenerator "-reports:$(test_project_base_folder)/results/result.opencover.xml" \
"-targetdir:$(test_project_base_folder)/results/Cobertura" \
"-reporttypes:Cobertura;HTMLInline;HTMLChart"
echo "##vso[task.setvariable variable=OpencoverSonar;isOutput=true]sonar.cs.opencover.reportsPaths=$(Pipeline.Workspace)/result.opencover.xml"
ls $(System.DefaultWorkingDirectory)/$(test_project_base_folder)/results
displayName: Running Unit Tests
name: UnitTestsTask
- task: PublishPipelineArtifact@1
displayName: Store Test Result
inputs:
path: $(System.DefaultWorkingDirectory)/$(test_project_base_folder)/results
artifact: UnitTestResults
- task: PublishCodeCoverageResults@1
displayName: Publish Code Coverage
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: $(System.DefaultWorkingDirectory)/$(test_project_base_folder)/results/result.cobertura.xml
pathToSources: .
failIfCoverageEmpty: true
- task: PublishTestResults@2
displayName: Publish Test Result
inputs:
testResultsFormat: 'XUnit'
testResultsFiles: '$(System.DefaultWorkingDirectory)/$(test_project_base_folder)/results/result.xunit.xml'
testRunTitle: 'Collecting Test Results'
- job: QualityAnalysis
dependsOn:
- UpdateBuildVersion
- Build
- UnitTests
condition: |
and
(
in(dependencies.UpdateBuildVersion.result, 'Succeeded'),
in(dependencies.Build.result, 'Succeeded', 'Skipped'),
in(dependencies.UnitTests.result, 'Succeeded', 'Skipped'),
eq(variables['execute_sonarcloud'], 'true')
)
workspace:
clean: all
variables:
SonarMasterWhenTag: $[ dependencies.UpdateBuildVersion.outputs['UpdateBuildVersionTask.SonarMasterWhenTag'] ]
OpencoverSonar: $[ dependencies.UnitTests.outputs['UnitTestsTask.OpencoverSonar'] ]
steps:
- task: DownloadPipelineArtifact@2
displayName: Get Test Result
condition: eq(variables['execute_test'], 'true')
inputs:
artifact: UnitTestResults
- task: SonarCloudPrepare@1
displayName: Start Sonarqube Analysis
inputs:
SonarCloud: '$(sonarcloud_account)'
organization: '$(sonarcloud_organization)'
scannerMode: 'MSBuild'
projectKey: '$(sonarcloud_project)'
projectName: '$(sdk_project_name)'
extraProperties: |
sonar.sourceEncoding=UTF-8
sonar.scm.forceReloadAll=true
$(OpencoverSonar)
$(SonarMasterWhenTag)
- script: |
dotnet build "$(solution_path)"
displayName: Runnig Build For Analysis
- task: SonarCloudAnalyze@1
displayName: Finish Sonarqube Analysis
- task: SonarCloudPublish@1
displayName: Publish Sonarqube Analysis
inputs:
pollingTimeoutSec: '300'
- task: sonarcloud-buildbreaker@2
inputs:
SonarCloud: '$(sonarcloud_account)'
organization: '$(sonarcloud_organization)'
- job: PackingNuget
dependsOn:
- UpdateBuildVersion
- Build
- UnitTests
- QualityAnalysis
workspace:
clean: all
condition: |
and
(
eq(dependencies.UpdateBuildVersion.result, 'Succeeded'),
in(dependencies.Build.result, 'Succeeded', 'Skipped'),
in(dependencies.UnitTests.result, 'Succeeded', 'Skipped'),
in(dependencies.QualityAnalysis.result, 'Succeeded', 'Skipped'),
or
(
contains(variables['Build.SourceBranch'], '/hotfix/'),
contains(variables['Build.SourceBranch'], '/release/'),
contains(variables['Build.SourceBranch'], '/tags/')
)
)
variables:
ReleaseVersion: $(Build.BuildNumber)
PureVersion: $[ dependencies.UpdateBuildVersion.outputs['UpdateBuildVersionTask.PureVersion'] ]
steps:
- task: DotNetCoreCLI@2
displayName: 'Packing Release'
inputs:
command: 'custom'
projects: '$(sdk_project_path)'
custom: 'pack'
arguments: '-p:Configuration=Release -p:PackageVersion=$(ReleaseVersion) -p:Version=$(PureVersion) --output $(Build.ArtifactStagingDirectory)'
- task: PublishPipelineArtifact@1
displayName: Store Nuget Packages
inputs:
path: '$(Build.ArtifactStagingDirectory)'
artifact: NugetPackage
- stage: DeployPackage
dependsOn: BuildApp
condition: |
and
(
succeeded(),
or
(
contains(variables['Build.SourceBranch'], '/hotfix/'),
contains(variables['Build.SourceBranch'], '/release/'),
contains(variables['Build.SourceBranch'], '/tags/')
)
)
variables:
- group: NugetCredentials
jobs:
- job: PublishNugetPackage
steps:
- task: DownloadPipelineArtifact@2
displayName: Get Application Artifact
inputs:
artifact: NugetPackage
- task: DotNetCoreCLI@2
displayName: 'Publish package in NuGet'
inputs:
command: custom
custom: nuget
arguments: >
push $(Pipeline.Workspace)/$(sdk_project_name).$(Build.BuildNumber).nupkg
-s $(NugetServerUrl)
-k $(NugetApiKey)