-
Notifications
You must be signed in to change notification settings - Fork 2
/
azure-pipelines.yml
409 lines (364 loc) · 13.4 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Test, build and release a Python library from Github using Azure DevOps Pipelines.
#
# To use, customise the `variables` at the top of the file to suit your project.
# Everything else should just work :-)
#
# Be aware that this pipeline does not necessarily demonstrate best-practices
# for Python package management, although it's good enough. What it does
# demonstrate is to how to implement a complete Azure Pipeline template
# for Python, examples for which are hard to find.
variables:
# Default version of Python used in build steps
build_python_version: '3.6'
# Name of the zipped dist files when stored as an artifact in Azure
dist_artifact: Flying_Circus_Wheel
# Manually set flag for whether to release this build to PyPI (if other
# preconditions are also met).
#
# In order to do a release, this variable must be defined and set on a
# manually queued job
#
# Note that this is evaluated by the Pipelines condition as a truthy string,
# so False is set as the empty string, and almost anything else evaluates
# as True.
#do_release: '' # False
# The name of the library as a pip requirement
library_name: 'flying-circus'
# The name of a "Service Connection" in the "Settings" section of the
# Azure DevOps project. This should have the type "Python package upload"
# and be configured with PyPI credentials that can access the relevant
# PyPI project.
pypi_service_connection: 'PyPI'
# The EndpointName for the Service Connection
pypi_endpoint_name: 'pypi'
# Set of Python + OS combinations that we test against.
# This is expressed as a dictionary in a JSON string, that gets expanded
# by the pipeline when required.
test_matrix: |
{
'Linux_Py36': {
'vm_image': 'ubuntu-latest',
'py_version': '3.6'
},
'Linux_Py37': {
'vm_image': 'ubuntu-latest',
'py_version': '3.7'
},
'Linux_Py38': {
'vm_image': 'ubuntu-latest',
'py_version': '3.8'
},
'Mac_Py36': {
'vm_image': 'macOS-latest',
'py_version': '3.6'
},
'Mac_Py37': {
'vm_image': 'macOS-latest',
'py_version': '3.7'
},
'Mac_Py38': {
'vm_image': 'macOS-latest',
'py_version': '3.8'
},
'Windows_Py36': {
'vm_image': 'windows-latest',
'py_version': '3.6'
},
'Windows_Py37': {
'vm_image': 'windows-latest',
'py_version': '3.7'
},
'Windows_Py38': {
'vm_image': 'windows-latest',
'py_version': '3.8'
}
}
# Simple command line script that validates an install of the library.
# The script should exit cleanly iff the library has been installed correctly.
validation_cli_script: ''
# Simple Python script that validates an install of the library.
# The script should exit cleanly iff the library has been installed correctly.
validation_python_code: |
import flyingcircus
s = flyingcircus.core.Stack()
print(s.export())
# Perform a build every time code is pushed to the master branch
trigger:
# Each time we push code to master we get a separate build
batch: false
branches:
include:
- master
# Perform a validation build every time a PR is created that will merge into
# the master branch
pr:
# Changes to the branch will cancel existing builds against the PR branch
autoCancel: true
branches:
include:
- master
stages:
- stage: Checks
jobs:
- job: CodeStyle
timeoutInMinutes: 5
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: $(build_python_version)
displayName: 'Use Python $(build_python_version)'
- script: |
python -m pip install --upgrade pip
pip install black==19.3b0
black --check src tests tools
displayName: 'black'
- job: Spelling
timeoutInMinutes: 5
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: $(build_python_version)
displayName: 'Use Python $(build_python_version)'
- script: |
python -m pip install --upgrade pip
# TODO disabled pending bug fixes in `spelling` package
#pip install spelling
#python -m spelling
displayName: 'spelling'
- job: Test
timeoutInMinutes: 10
strategy:
matrix:
$[ variables.test_matrix ]
pool:
vmImage: '$(vm_image)'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(py_version)'
displayName: 'Use Python $(py_version)'
- script: |
python -m pip install --upgrade pip
pip install poetry
echo Install a venv in the working dir as "venv".
echo Allow for the script to be run in Windows CMD or bash.
python -m venv venv
call venv\Scripts\activate.bat || source venv/bin/activate
python -m pip install --upgrade pip
poetry install
pip install pytest-azurepipelines
pip list
displayName: 'Install dependencies'
- script: |
call venv\Scripts\activate.bat || source venv/bin/activate
pytest tests
displayName: 'pytest'
- job: WillThisBeARelease
dependsOn: Test
condition: |
and(
succeeded(),
variables['do_release'],
eq(variables['Build.SourceBranch'], 'refs/heads/master')
)
timeoutInMinutes: 1
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: none
- script: |
echo "This build will be released to PyPI"
displayName: 'Just a print statement'
- stage: Build
condition: |
and(
succeeded(),
eq(variables['Build.SourceBranch'], 'refs/heads/master')
)
jobs:
- job: BuildWheel
timeoutInMinutes: 5
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: $(build_python_version)
displayName: 'Use Python $(build_python_version)'
- script: |
python -m pip install --upgrade pip
pip install poetry
poetry build
displayName: 'Build wheel'
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: dist/
artifactName: $(dist_artifact)
displayName: 'Publish Artifacts in Azure'
- job: Validate
dependsOn: BuildWheel
timeoutInMinutes: 5
strategy:
matrix:
$[ variables.test_matrix ]
pool:
vmImage: '$(vm_image)'
steps:
- checkout: none
- task: UsePythonVersion@0
inputs:
versionSpec: '$(py_version)'
displayName: 'Use Python $(py_version)'
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: '$(dist_artifact)'
itemPattern: '**'
downloadPath: '$(Build.ArtifactStagingDirectory)'
displayName: 'Download Artifact'
- script: |
echo This script is circumlocutious because it needs to be cross-platform
python -m pip install --upgrade pip
echo You cant list a directory or change into it directly, because of
echo platform-specific directory separators.
echo Also, note that the ArtifactStagingDirectory variable contains a
echo drive letter, so it cant be used in a Windows bash script.
echo Hence we change into the directory one level at a time
cd $(Build.ArtifactStagingDirectory)
cd $(dist_artifact)
echo You cant just do `pip install *.whl` because on Windows this looks
echo for a file literally called "*.whl".
echo Also, you cant pipe the `ls` output into pip, because theres
echo no way to use stdin as a requirements file.
echo Hence we use 2 commands with an intermediate file
ls *.whl > localreq.txt
pip install -r localreq.txt
displayName: 'Install RC Wheel'
- task: PythonScript@0
condition: and(succeeded(),variables['validation_python_code'])
inputs:
scriptSource: inline
script: |
$(validation_python_code)
displayName: 'Validate Python'
- script: |
$(validation_cli_script)
condition: and(succeeded(),variables['validation_cli_script'])
displayName: 'Validate CLI'
- stage: Release
# There isn't a way to add a manual approval step to a pipeline (I think
# you're supposed to use a separate Release Pipeline, but they aren't
# available as IaC yet), so we fudge it by using a variable that can be
# overridden with a manual build.
condition: |
and(
succeeded(),
variables['do_release'],
eq(variables['Build.SourceBranch'], 'refs/heads/master')
)
jobs:
- job: PyPiLive
timeoutInMinutes: 5
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: none
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: '$(dist_artifact)'
itemPattern: '**'
downloadPath: '$(Build.ArtifactStagingDirectory)'
displayName: 'Download Artifact'
- task: UsePythonVersion@0
inputs:
versionSpec: $(build_python_version)
displayName: 'Use Python $(build_python_version)'
- script: |
python -m pip install --upgrade pip
pip install -U twine wheel
displayName: 'Install dependencies'
- task: TwineAuthenticate@0
inputs:
externalFeeds: '$(pypi_service_connection)'
displayName: 'Authenticate For PyPI'
- script: |
twine upload -r $(pypi_endpoint_name) --config-file $(PYPIRC_PATH) --disable-progress-bar $(Build.ArtifactStagingDirectory)/$(dist_artifact)/*
displayName: 'Upload to PyPI'
- script: |
PYPI_VERSION=$(ls -l $(Build.ArtifactStagingDirectory)/$(dist_artifact)/*.tar.gz | tail -n 1 | sed -r s,"^.*-(.+)\.tar\.gz$","\1",)
PRERELEASE=$(echo ${PYPI_VERSION} | sed -r s,"^[0-9.]+",,)
echo "##vso[task.setvariable variable=prerelease;isOutput=true]${PRERELEASE}"
echo "##vso[task.setvariable variable=pypiversion;isOutput=true]${PYPI_VERSION}"
name: exportVars
displayName: 'Export Build Variables'
- job: PostReleaseValidate
dependsOn: PyPiLive
timeoutInMinutes: 5
strategy:
matrix:
$[ variables.test_matrix ]
pool:
vmImage: '$(vm_image)'
variables:
releasedPyPIVersion: $[ dependencies.PyPiLive.outputs['exportVars.pypiversion'] ]
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(py_version)'
displayName: 'Use Python $(py_version)'
- script: |
python -m pip install --upgrade pip
pip install $(library_name)==$(releasedPyPIVersion)
displayName: 'Install Library'
- task: PythonScript@0
condition: and(succeeded(),variables['validation_python_code'])
inputs:
scriptSource: inline
script: |
$(validation_python_code)
displayName: 'Validate Python'
- script: |
$(validation_cli_script)
condition: and(succeeded(),variables['validation_cli_script'])
displayName: 'Validate CLI'
- job: PostReleaseVersionBump
dependsOn: PyPiLive
timeoutInMinutes: 5
pool:
vmImage: 'ubuntu-latest'
variables:
releasedPyPIVersion: $[ dependencies.PyPiLive.outputs['exportVars.pypiversion'] ]
steps:
- checkout: self
persistCredentials: true
- task: UsePythonVersion@0
inputs:
versionSpec: '$(build_python_version)'
displayName: 'Use Python $(build_python_version)'
- script: |
python -m pip install --upgrade pip
pip install bump2version==0.5.10
displayName: 'Install Tools'
- script: |
TAGNAME=release-$(releasedPyPIVersion)
git tag -a ${TAGNAME} -m "Release version $(releasedPyPIVersion)"
git push origin ${TAGNAME}
displayName: 'Tag Code'
- script: |
BRANCHNAME=bump-from-$(releasedPyPIVersion)
git checkout -b ${BRANCHNAME}
bumpversion patch --commit
git push --set-upstream origin ${BRANCHNAME}
condition: |
and(
succeeded(),
eq(variables['Build.SourceBranch'], 'refs/heads/master')
)
displayName: 'Bump Version'