-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
141 lines (114 loc) · 4.56 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
#!groovy
// Global scope required for multi-stage persistence
def artifactoryStr = 'onsart-01'
artServer = Artifactory.server "${artifactoryStr}"
buildInfo = Artifactory.newBuildInfo()
def agentPython3Version = 'python_3.10'
def artifactVersion
// Define a function to push packaged code to Artifactory
def pushToPyPiArtifactoryRepo_temp(String projectName, String version, String sourceDistLocation = 'python/dist/*', String artifactoryHost = 'onsart-01.ons.statistics.gov.uk') {
withCredentials([usernamePassword(credentialsId: env.ARTIFACTORY_CREDS, usernameVariable: 'ARTIFACTORY_USER', passwordVariable: 'ARTIFACTORY_PASSWORD')]){
sh "curl -u ${ARTIFACTORY_USER}:\${ARTIFACTORY_PASSWORD} -T ${sourceDistLocation} 'https://${artifactoryHost}/artifactory/${env.ARTIFACTORY_PYPI_REPO}/${projectName}/'"
}
}
// This section defines the Jenkins pipeline
pipeline {
agent any
libraries {
lib('jenkins-pipeline-shared@feature/dap-ci-scripts')
}
environment {
ARTIFACTORY_CREDS = 's_jenkins_epds'
ARTIFACTORY_PYPI_REPO = 'LR_EPDS_pypi'
PROJECT_NAME = 'resdev'
BUILD_BRANCH = 'main' // Any commits to this branch will create a build in artifactory
BUILD_TAG = '*-release' // Any commits tagged with this pattern will create a build in artifactory
MIN_COVERAGE_PC = '0'
GITLAB_CREDS = 'rdsa_token' // Credentials used for notifying GitLab of build status
}
options {
skipDefaultCheckout true
}
stages {
stage('Checkout') {
agent { label 'download.jenkins.slave' }
steps {
onStage()
colourText('info', "Checking out code from source control.")
checkout scm
script {
buildInfo.name = "${PROJECT_NAME}"
buildInfo.number = "${BUILD_NUMBER}"
buildInfo.env.collect()
}
colourText('info', "BuildInfo: ${buildInfo.name}-${buildInfo.number}")
stash name: 'Checkout', useDefaultExcludes: false
}
}
stage('Preparing virtual environment') {
agent { label "test.${agentPython3Version}" }
steps {
onStage()
colourText('info', "Create venv and install dependencies")
unstash name: 'Checkout'
sh '''
PATH=$WORKSPACE/venv/bin:/usr/local/bin:/root/.local/bin:$PATH
python3 -m pip install -U pip
pip3 install virtualenv
if [ ! -d "venv" ]; then
virtualenv venv
fi
. venv/bin/activate
export PIP_USER=false
pip3 install pypandoc
pip3 install -r requirements.txt
pip3 freeze
'''
stash name: 'venv', useDefaultExcludes: false
}
}
stage('Unit Test and coverage') {
agent { label "test.${agentPython3Version}" }
steps {
onStage()
colourText('info', "Running unit tests and code coverage.")
unstash name: 'Checkout'
unstash name: 'venv'
// Compatibility for PyArrow with Spark 2.4-legacy IPC format.
sh 'export ARROW_PRE_0_15_IPC_FORMAT=1'
// Running coverage first runs the tests
sh '''
. venv/bin/activate
python3 -m pytest --junitxml "junit-report.xml" "./tests"
'''
junit testResults: 'junit-report.xml'
}
}
stage('Build and publish Python Package') {
when {
anyOf{
branch BUILD_BRANCH
tag BUILD_TAG
}
beforeAgent true
}
agent { label "test.${agentPython3Version}" }
steps {
onStage()
colourText('info', "Building Python package.")
unstash name: 'Checkout'
unstash name: 'venv'
sh '''
. venv/bin/activate
export PIP_USER=false
pip3 install setuptools
pip3 install wheel
python3 setup.py build bdist_wheel
'''
script {
pushToPyPiArtifactoryRepo_temp("${buildInfo.name}", "", "dist/*")
}
}
}
}
}