-
Notifications
You must be signed in to change notification settings - Fork 5
/
Jenkinsfile
364 lines (326 loc) · 11.8 KB
/
Jenkinsfile
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
def python_versions
def python_executables_and_wheels_map
pipeline {
agent {
node {
label 'zOS_pyRACF'
}
}
parameters {
// Note: Each Python version listed must be installed on the
// build agent and must be added to '$PATH' and '$LIBPATH'.
string (
name: "pythonVersions",
defaultValue: "",
description: "(Required Always) Comma separated list of Python versions to build wheels for (i.e., Use '10,11' for Python 3.10 and Python 3.11)."
)
booleanParam(
name: "createRelease",
defaultValue: false,
description: "Toggle whether or not to create a release from this revision."
)
string(
name: "releaseTag",
defaultValue: "",
description: "(Required When Creating Releases) This will be the git tag and version number of the release."
)
string(
name: "gitHubMilestoneLink",
defaultValue: "",
description: "(Required When Creating Releases) This is the GitHub Milestore URL that coresponds to the release."
)
booleanParam(
name: "preRelease",
defaultValue: true,
description: "Toggle whether or not this is a pre-release."
)
}
options {
ansiColor('css')
}
stages {
stage('Parameter Validation') {
steps {
script {
if (params.pythonVersions == "") {
error("'pythonVersions' is required parameter.")
}
if (params.createRelease) {
if (params.releaseTag == "") {
error("'releaseTag' is a required parameter when creating a release.")
}
if (params.gitHubMilestoneLink == "") {
error("'gitHubMilestoneLink' is a required parameter when creating a release.")
}
}
}
}
}
stage('Build Python Executables & Wheels Map') {
steps {
script {
python_versions = params.pythonVersions.split(",")
python_executables_and_wheels_map = (
create_python_executables_and_wheels_map(python_versions)
)
}
}
}
stage('Install Poetry') {
steps {
clean_python_environment()
install_poetry(python_executables_and_wheels_map.keySet()[-1])
}
}
stage('Build Virtual Environments') {
steps {
script {
for (python in python_executables_and_wheels_map.keySet()) {
build_poetry_environment(python)
}
}
}
}
stage('Lint & Unit Test') {
steps {
script {
for (python in python_executables_and_wheels_map.keySet()) {
lint_and_unit_test(python)
}
}
}
}
stage('Function Test') {
steps {
script {
for (python in python_executables_and_wheels_map.keySet()) {
function_test(
python,
python_executables_and_wheels_map[python]["wheelDefault"]
)
}
}
}
}
stage('Publish') {
when {
expression { params.createRelease == true }
}
steps {
publish(
python_executables_and_wheels_map,
params.releaseTag,
env.BRANCH_NAME,
params.gitHubMilestoneLink,
params.preRelease
)
}
}
}
post {
always {
echo "Cleaning up workspace..."
cleanWs()
clean_python_environment()
}
}
}
def create_python_executables_and_wheels_map(python_versions) {
def os = sh(
returnStdout: true,
script: "uname"
).trim().replace("/", "").toLowerCase()
def zos_release = sh(
returnStdout: true,
script: "uname -r"
).trim().replace(".", "_")
def processor = sh(
returnStdout: true,
script: "uname -m"
).trim()
def pyracf_version = sh(
returnStdout: true,
script: "cat pyproject.toml | grep version | cut -d'=' -f2 | cut -d'\"' -f2"
).trim()
python_executables_and_wheels_map = [:]
for (version in python_versions) {
python_executables_and_wheels_map["python3.${version}"] = [
"wheelDefault": (
"pyracf-${pyracf_version}-cp3${version}-cp3${version}-${os}_${zos_release}_${processor}.whl"
),
"wheelPublish": "pyracf-${pyracf_version}-cp3${version}-none-any.whl",
"tarPublish": "pyracf-${pyracf_version}.tar.gz"
]
}
return python_executables_and_wheels_map
}
def clean_python_environment() {
echo "Cleaning Python environment..."
sh """
rm -rf ~/.cache
rm -rf ~/.local
"""
}
def install_poetry(python) {
echo "Installing Poetry..."
sh "bash -c 'curl -sSL https://install.python-poetry.org' | ${python} -"
}
def build_poetry_environment(python) {
echo "Building Poetry environment for '${python}'..."
sh """
poetry env use ${python}
poetry install --no-root
poetry env info
"""
}
def lint_and_unit_test(python) {
echo "Running linters and unit tests for '${python}'..."
sh """
poetry env use ${python}
poetry env info
poetry run flake8 .
poetry run pylint --recursive=y .
poetry run coverage run tests/test_runner.py
poetry run coverage report -m
"""
}
def function_test(python, wheel) {
echo "Running function test for '${python}'..."
sh """
git clean -fdx
poetry env use ${python}
poetry env info
poetry build
${python} -m pip install dist/${wheel}
cd tests/function_test
${python} function_test.py
"""
}
def publish(
python_executables_and_wheels_map,
release,
git_branch,
milestone,
pre_release
) {
if (pre_release == true) {
pre_release = "true"
}
else {
pre_release = "false"
}
withCredentials(
[
string(
credentialsId: 'pyracf-github-access-token',
variable: 'github_access_token'
),
string(
credentialsId: 'pyracf-pypi-repository',
variable: 'pypi_repository'
),
string(
credentialsId: 'pyracf-pypi-username',
variable: 'pypi_username'
),
string(
credentialsId: 'pyracf-pypi-password',
variable: 'pypi_password'
)
]
) {
// Creating GitHub releases:
// https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
// Uploading release assets:
// https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset
// Use single quotes for credentials since it is the most secure
// method for interpolating secrets according to the Jenkins docs:
// https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation
echo "Creating '${release}' GitHub release..."
def description = build_description(python_executables_and_wheels_map, release, milestone)
def release_id = sh(
returnStdout: true,
script: (
'curl -f -v -L '
+ '-X POST '
+ '-H "Accept: application/vnd.github+json" '
+ '-H "Authorization: Bearer ${github_access_token}" '
+ '-H "X-GitHub-Api-Version: 2022-11-28" '
+ "https://api.github.com/repos/ambitus/pyracf/releases "
+ "-d '{"
+ " \"tag_name\": \"${release}\","
+ " \"target_commitish\": \"${git_branch}\","
+ " \"name\": \"${release}\","
+ " \"body\": \"${description}\","
+ " \"draft\": false,"
+ " \"prerelease\": ${pre_release},"
+ " \"generate_release_notes\":false"
+ "}' | grep '\"id\": ' | head -n1 | cut -d':' -f2 | cut -d',' -f1"
)
).trim()
sh 'poetry config repositories.test ${pypi_repository}'
def tar_published = false
for (python in python_executables_and_wheels_map.keySet()) {
def wheel_default = python_executables_and_wheels_map[python]["wheelDefault"]
def wheel_publish = python_executables_and_wheels_map[python]["wheelPublish"]
def tar_publish = python_executables_and_wheels_map[python]["tarPublish"]
echo "Cleaning repo and building '${wheel_default}'..."
sh """
git clean -fdx
poetry env use ${python} && poetry env info
poetry build
mv dist/${wheel_default} dist/${wheel_publish}
"""
echo "Uploading '${wheel_default}' as '${wheel_publish}' to '${release}' GitHub release..."
upload_asset(release_id, wheel_publish)
if (tar_published == false) {
upload_asset(release_id, tar_publish)
tar_published = true
}
else {
sh "rm dist/${tar_publish}"
}
echo "Uploading '${wheel_default}' as '${wheel_publish}' and 'pyracf-${release}.tar.gz' to PyPi repository..."
sh (
'poetry publish \\'
+ '--repository=test \\'
+ '--username=${pypi_username} \\'
+ '--password=${pypi_password}'
)
}
}
}
def upload_asset(release_id, release_asset) {
sh(
'curl -f -v -L '
+ '-X POST '
+ '-H "Accept: application/vnd.github+json" '
+ '-H "Authorization: Bearer ${github_access_token}" '
+ '-H "X-GitHub-Api-Version: 2022-11-28" '
+ '-H "Content-Type: application/octet-stream" '
+ "\"https://uploads.github.com/repos/ambitus/pyracf/releases/${release_id}/assets?name=${release_asset}\" "
+ "--data-binary \"@dist/${release_asset}\""
)
}
def build_description(python_executables_and_wheels_map, release, milestone) {
def description = "Release Milestone: ${milestone}\\n \\n \\n"
for (python in python_executables_and_wheels_map.keySet()) {
def wheel = python_executables_and_wheels_map[python]["wheelPublish"]
def python_executable = python
def python_label = python.replace("python", "Python ")
description += (
"Install From **${python_label} Wheel Distribution** *(pre-built)*:\\n"
+ "```\\ncurl -O -L https://github.com/ambitus/pyracf/releases/download/${release}/${wheel} "
+ "&& ${python_executable} -m pip install ${wheel}\\n```\\n"
)
}
def python = python_executables_and_wheels_map.keySet()[-1]
def tar = python_executables_and_wheels_map[python]["tarPublish"]
description += (
"Install From **Source Distribution** *(build on install)*:\\n"
+ "> :warning: _Requires z/OS XLC compiler._\\n"
+ "```\\ncurl -O -L https://github.com/ambitus/pyracf/releases/download/${release}/${tar} "
+ "&& python3 -m pip install ${tar}\\n```\\n"
)
return description
}