-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile.package
159 lines (147 loc) · 5.24 KB
/
Jenkinsfile.package
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
#!/usr/bin/env groovy
/**
TEMPLATE DIRECTIONS:
1. Copy this file to Jenkinsfile at root of new repo, or merge in appropriate items if one already exists
2. Find/replace "{{PACKAGE}}" with your package name
3. If {{PACKAGE}} doesn't live at the root level, replace all ${STAGE_NAME} references
with the path to your package, relative to root
4. Change {{TWINE_USERNAME}} and {{TWINE_PASSWORD}} to your credentials for PyPI upload (preferablly
these should live as Jenkins SECRET variables instead).
This Jenkinsfile utilizes a pre-built python3.6 image to lint, test, package, and release a package. It
relies on a proper setuptools configuration for test, pip3 being accessible, and twine pre-configured
DISCLAIMER: This template was modified from a project-specific Jenkinsfile. It has not been tested
explicitly so some modifications may be required. The general structure should be valid. YMMV.
Document generation assumes sphinx is installed and managed in setup.cfg. A good example for this is
the cookiecutter-pypackage: https://github.com/audreyr/cookiecutter-pypackage
**/
/**
* Executes code style linting and unit tests
* using setuptools and pytest directly
*
* @param packageName Name of package to test
*/
def runTests(packageName) {
sh """
cd ${packageName}
# Run linting / code-formatt checks
python3 -m pip install --upgrade pip black flake8
python3 -m black .
python3 -m flake8 --config=${env.WORKSPACE}/.flake8 .
# Now run unit tests
python3 setup.py test
"""
}
/**
* Package python module using setuptools and upload to PyPI
* with Twine.
*
* @param packageName Name of package to build release for
*/
def packageUpload(packageName) {
sh """
# Build
cd ${packageName}
python3 -m pip install --upgrade pip setuptools twine
python3 setup.py sdist bdist_wheel
ls -l dist
# Upload
python3 -m twine upload dist/*
"""
}
/**
* Builds docs using setuptools
*
* @param packageName Name of package to document
*/
def buildDocs(packageName) {
sh """
# Install package so docs can be built from setuptools
cd ${packageName}
python3 setup.py install --user
# Build docs
python3 setup.py docs
"""
}
pipeline {
agent {
docker {
image "python:3.6-stretch"
alwaysPull "true"
}
}
options {
newContainerPerStage()
}
environment {
// Set credentials for PyPI uploads.
TWINE_USERNAME = {{TWINE_USERNAME}}
TWINE_PASSWORD = {{TWINE_PASSWORD}}
}
stages {
// Test Suite includes linting and unit tests
stage('Execute Test Suite') {
parallel {
stage('{{PACKAGE}}') {
steps {
runTests("${STAGE_NAME}")
}
post {
always {
junit(
allowEmptyResults: true,
keepLongStdio: true,
testResults: "**/test-results/*.xml"
)
publishHTML([
allowMissing: true,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: "${env.WORKSPACE}/${STAGE_NAME}/test-results/coverage",
reportFiles: "index.html",
reportName: "Coverage Report - ${STAGE_NAME}",
reportTitles: "${STAGE_NAME}:${env.BUILD_NUMBER} Coverage Index"
])
}
}
}
}
}
stage('Package and Upload') {
when {
branch 'dev'
}
parallel {
stage('{{PACKAGE}}') {
steps {
packageUpload("${STAGE_NAME}")
}
}
}
}
stage('Generate Docs') {
when {
branch 'dev'
}
parallel {
stage('{{PACKAGE}}') {
steps {
buildDocs("${STAGE_NAME}")
}
post {
always {
publishHTML([
allowMissing: true,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: "${env.WORKSPACE}/${STAGE_NAME}/build/sphinx/html",
reportFiles: "index.html",
reportName: "Software Docs - ${STAGE_NAME}",
reportTitles: "${STAGE_NAME}:${env.BUILD_NUMBER} Software Docs Index"
])
}
}
}
}
}
}
}