-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathJenkinsfile
134 lines (123 loc) · 4.45 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
pipeline {
agent {
label 'linux && python'
}
environment {
GIT_CREDENTIALS = credentials("${GITHUB_CREDENTIALS}")
DOCKER_BUILDKIT = 0
}
stages {
stage('Build') {
agent { docker { image "python:3" } }
steps {
echo 'Building..'
sh '''
rm -rf dist
python -m venv ./sdk-deploy
. ./sdk-deploy/bin/activate
pip install wheel setuptools
python setup.py sdist bdist_wheel
'''
}
}
stage('Test') {
agent {
dockerfile {
label 'linux && python'
dir 'tests'
}
}
steps {
echo 'Testing..'
sh '''
virtualenv ./sdk-test
. ./sdk-test/bin/activate
tox
'''
}
}
stage("Version Check") {
agent { docker { image "python:3" } }
steps {
checkVersion(discoverVersion())
}
}
stage('Deploy') {
agent { docker { image "python:3" } }
steps {
echo 'Deploying....'
sh '''
. ./sdk-deploy/bin/activate
python -m pip install twine
twine upload dist/* -u ${PYPI_USER_ID} -p ${PYPI_PASSWORD}
rm -r dist
'''
tagRepo(discoverVersion())
}
}
}
post {
success {
notifyComplete('SUCCESS')
}
failure {
notifyComplete('FAILURE')
}
}
}
// Get version number from PKG-INFO file
def discoverVersion() {
def pkgFile = readFile("src/rev_ai.egg-info/PKG-INFO")
versionLine = pkgFile.split('\n')[2] // get line with version number
assert versionLine =~ /Version(.*)/
return versionLine.substring(versionLine.lastIndexOf(':')+2, versionLine.length())
}
// Ensure that the version number has been incremented since last release
def checkVersion(version) {
def tags = sh (script: "git ls-remote https://${GIT_CREDENTIALS}@${getRepoUrl()} --tags origin ${version}", returnStdout: true)
if (tags.trim() != "") {
error "${version} is already released, please increase the version number for build to publish"
}
}
// Tag repo with version number so we know this version has been released
def tagRepo(tag) {
echo "tagging repo with ${tag}"
// deletes current tag locally, this will make sure if the tag is deleted from remote it would be set again
sh "git tag -d ${tag} || (exit 0)"
// see if it's still on remote - needed to avoid overwriting the tag
sh "git fetch https://${GIT_CREDENTIALS}@${getRepoUrl()} --tags"
// try to tag
sh "git tag ${tag}"
sh "git push https://${GIT_CREDENTIALS}@${getRepoUrl()} ${tag}"
}
// Get url of github repo
def getRepoUrl() {
def httpsPrefix = '^(https://)?'
def matchesHttpsPrefix = "${GIT_URL}" =~ httpsPrefix
return matchesHttpsPrefix.replaceFirst('')
}
// sends notifications on failed builds or when the build becomes stable again
def notifyComplete(String buildStatus) {
String SUCCESS = 'SUCCESS'
String FAILURE = 'FAILURE'
if (env.NOSLACK_NOTIFICATIONS == 'true')
{
echo "Slack notifications are disabled"
return
}
// build status of null means successful
buildStatus = buildStatus ?: SUCCESS
def prevBuildResult = currentBuild.getPreviousBuild()?.getResult()
echo "notifyComplete with status ${buildStatus}, previous build was ${prevBuildResult}"
// Slack channel ID CFPMB0BK4 refers to #revai-alerts-nonprod, does not change even if channel name changes
if (buildStatus == FAILURE && prevBuildResult != FAILURE)
slackSend (
color: '#FF0000', // red
channel: 'CFPMB0BK4',
message: "${env.JOB_NAME} ${env.BUILD_NUMBER} for release ${discoverVersion()} failed after ${currentBuild.durationString} (<${env.BUILD_URL}|Open>)")
else if (buildStatus == SUCCESS && prevBuildResult != SUCCESS)
slackSend (
color: '#00FF00', // green
channel: 'CFPMB0BK4',
message: "${env.JOB_NAME} ${env.BUILD_NUMBER} for release ${discoverVersion()} back to normal after ${currentBuild.durationString} (<${env.BUILD_URL}|Open>)")
}