Repository navigation
Expand file tree
/
Copy pathJenkinsfile
More file actions
200 lines (180 loc) · 7.54 KB
/
Copy pathJenkinsfile
File metadata and controls
200 lines (180 loc) · 7.54 KB
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
/*
GetFinancing Jenkinsfile for GFBase library/service.
*/
// Define here Project default constants
def docker_registry = 'https://118864902010.dkr.ecr.us-east-1.amazonaws.com';
def docker_registry_credential = 'ecr:us-east-1:AWSJenkins';
def docker_util_image = 'getfinancing/libs/testsbase';
def master_branch_name = 'gfmaster';
def docker_allowed_branches = ['develop', master_branch_name];
def current_date = new Date().format('yyyyMMddhhmmss');
def should_build = true;
def skip_steps = false;
def release_version = '';
pipeline {
agent any
options {
disableConcurrentBuilds(abortPrevious: true)
}
environment {
DOCKER_TAG = "${env.BRANCH_NAME == master_branch_name ? 'latest' : env.BRANCH_NAME}"
// If not in master branch, use develop for any other branches,
// as the utility image will not exists for those branches.
UTIL_TAG = "${env.BRANCH_NAME == master_branch_name ? 'latest' : 'develop'}"
//SOFT_FAIL = "${env.BRANCH_NAME == master_branch_name ? '|| false' : '|| true'}"
SOFT_FAIL = "${env.BRANCH_NAME == master_branch_name ? '|| true' : '|| true'}"
DOCKER_COMMAND = "-e HOME=/var/jenkins_home"
}
stages {
stage('Clone') {
steps {
checkout scm
script {
result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true)
if (result == 0) {
echo ("'ci skip' spotted in git commit. Aborting.")
should_build = false
}
}
withCredentials([file(credentialsId: "pip_conf", variable: 'PIP_CONF')]) {
sh "mkdir -p ~/.config/pip; cp -f $PIP_CONF ~/.config/pip/pip.conf"
}
withCredentials([file(credentialsId: "pypirc", variable: 'PYPIRC')]) {
sh "cp -f $PYPIRC ~/.pypirc"
}
}
}
// stage('Security') {
// when {
// expression {
// !skip_steps && should_build
// }
// }
// steps {
// echo "Checking ${env.JOB_NAME} #${env.BUILD_ID}"
// script {
// docker.withRegistry(docker_registry, docker_registry_credential) {
// docker.image(docker_util_image).inside(env.DOCKER_COMMAND) {
// sh ". /py27; tox -e 'py27-security' ${SOFT_FAIL}"
// }
// }
// }
// }
// }
stage('Build Library') {
when {
expression {
should_build && env.BRANCH_NAME in docker_allowed_branches
}
}
steps {
echo "Going to build ${env.JOB_NAME} #${env.BUILD_ID}"
script {
sh 'git config --global push.default simple'
sshagent (credentials: ['JenkinsGitKey']) {
sh "git fetch --all"
sh "git checkout ${env.BRANCH_NAME}"
sh "git pull"
}
docker.withRegistry(docker_registry, docker_registry_credential) {
docker.image(docker_util_image).inside(env.DOCKER_COMMAND) {
if (env.BRANCH_NAME == master_branch_name) {
sh '. /py27; tox -e build-rel'
} else {
sh '. /py27; tox -e build-dev'
}
release_version= sh(returnStdout: true, script:'. /py27; python setup.py --version').trim()
}
}
sh "git commit -a -m 'Deployment of version ${release_version}, build ${env.BUILD_ID} [ci skip]'"
sshagent (credentials: ['JenkinsGitKey']) {
sh "git push"
}
echo "Pushed release: ${release_version}"
if (env.BRANCH_NAME == master_branch_name) {
sh "git tag v${release_version}"
sshagent (credentials: ['JenkinsGitKey']) {
sh "git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'"
sh "git fetch"
sh "git checkout develop"
sh "git pull"
sh "git checkout origin/${master_branch_name} setup.cfg setup.py"
sh "git commit -a -m 'Deployment of version ${release_version}, build ${env.BUILD_ID} [ci skip]'"
sh "git merge ${master_branch_name}"
sh "git push origin develop --tags"
sh "git checkout ${master_branch_name}"
}
}
}
}
}
stage('Publish Library') {
when {
expression {
should_build && env.BRANCH_NAME in docker_allowed_branches
}
}
steps {
echo "Going to publish ${env.JOB_NAME} #${env.BUILD_ID}"
script {
docker.withRegistry(docker_registry, docker_registry_credential) {
docker.image(docker_util_image).inside(env.DOCKER_COMMAND) {
sh '. .tox/py27/bin/activate; twine upload -r getfinancing dist/*'
}
}
}
slackSend(color: "#0000FF0", message: "Congratulations! I have published ${env.JOB_NAME} #${env.BUILD_ID} v${release_version} within the pip repository. Enjoy the new release!")
dir('dist') {
deleteDir();
}
}
}
}
post {
// always {
// cleanWs()
// }
// success {
// notifyBuild(currentBuild.result);
// }
failure {
notifyBuild(currentBuild.result);
cleanWs()
}
}
}
/*
Sniped copied from: https://www.cloudbees.com/blog/sending-notifications-pipeline
*/
def notifyBuild(String buildStatus = 'STARTED') {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
// Default values
def recipientProviders = [[$class: 'DevelopersRecipientProvider']]
def colorName = 'RED'
def colorCode = '#FF0000'
def subject = "${buildStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
def summary = "${subject} (${env.BUILD_URL})"
def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>"""
// Override default values based on build status
if (buildStatus == 'STARTED') {
color = 'YELLOW'
colorCode = '#FFFF00'
} else if (buildStatus == 'SUCCESSFUL') {
color = 'GREEN'
colorCode = '#00FF00'
} else {
color = 'RED'
colorCode = '#FF0000'
recipientProviders = [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']]
}
// Send notifications
slackSend(color: colorCode, message: summary)
emailext(
subject: "[Jenkins] ${subject}",
mimeType: 'text/html',
body: '''${JELLY_SCRIPT,template="html"}''',
recipientProviders: recipientProviders
)
}