-
Notifications
You must be signed in to change notification settings - Fork 5
/
release-lib-master.Jenkinsfile
203 lines (200 loc) · 7.53 KB
/
release-lib-master.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
pipeline {
agent {
kubernetes {
cloud params.JENKINS_AGENT_CLOUD_NAME
label "jenkins-slave-${UUID.randomUUID().toString()}"
serviceAccount "jenkins"
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
app: "jenkins-${env.JOB_BASE_NAME}"
indy-pipeline-build-number: "${env.BUILD_NUMBER}"
spec:
containers:
- name: jnlp
image: quay.io/kaine/indy-stress-tester:latest
imagePullPolicy: Always
tty: true
env:
- name: USER
value: 'jenkins-k8s-config'
- name: HOME
value: /home/jenkins
- name: JAVA_TOOL_OPTIONS
value: '-XX:+UnlockExperimentalVMOptions -Dsun.zip.disableMemoryMapping=true -Xms1024m -Xmx4g'
- name: MAVEN_OPTS
value: -Xmx6g -Xms1024m -XX:MaxPermSize=512m -Xss8m
resources:
requests:
memory: 4Gi
cpu: 2000m
limits:
memory: 8Gi
cpu: 4000m
volumeMounts:
- mountPath: /home/jenkins/sonatype
name: volume-0
- mountPath: /home/jenkins/gnupg_keys
name: volume-1
- mountPath: /mnt/ocp
name: volume-2
workingDir: /home/jenkins
volumes:
- name: volume-0
secret:
defaultMode: 420
secretName: sonatype-secrets
- name: volume-1
secret:
defaultMode: 420
secretName: gnupg
- name: volume-2
configMap:
defaultMode: 420
name: jenkins-openshift-mappings
"""
}
}
options {
//timestamps()
timeout(time: 120, unit: 'MINUTES')
}
environment {
JAVA_HOME = "/usr/lib/jvm/java-${params.JDK_VERSION}-openjdk"
PIPELINE_NAMESPACE = readFile('/run/secrets/kubernetes.io/serviceaccount/namespace').trim()
PIPELINE_USERNAME = sh(returnStdout: true, script: 'id -un').trim()
}
stages {
stage('git checkout') {
steps{
script{
checkout([$class : 'GitSCM', branches: [[name: params.LIB_GIT_BRANCH]], doGenerateSubmoduleConfigurations: false,
extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: params.LIB_NAME]], submoduleCfg: [],
userRemoteConfigs: [[url: params.LIB_GIT_REPO]]])
echo "Prepare the release of ${params.LIB_GIT_REPO} branch: ${params.LIB_GIT_BRANCH}"
sh """#!/bin/bash
cd ${params.LIB_NAME}
git checkout ${params.LIB_GIT_BRANCH}
gpg --allow-secret-key-import --import /home/jenkins/gnupg_keys/private_key.txt
"""
}
}
}
stage('Setup and Formating'){
steps{
script{
withCredentials([
usernamePassword(credentialsId:'GitHub_Bot', passwordVariable:'BOT_PASSWORD', usernameVariable:'BOT_USERNAME'),
usernamePassword(credentialsId:'OSS-Nexus-Bot', passwordVariable:'OSS_BOT_PASSWORD', usernameVariable:'OSS_BOT_USERNAME'),
string(credentialsId: 'gnupg_passphrase', variable: 'PASSPHRASE')
]) {
dir(params.LIB_NAME){
env.LIB_PMD_VIOLATION = sh (
script: 'mvn -B -s ../settings.xml -Pformatting,cq clean install',
returnStatus: true
) == 0
sh """
mkdir -p /home/jenkins/.m2
cp ../settings-release.xml /home/jenkins/.m2/settings.xml
sed -i 's/{{_PASSPHRASE}}/${PASSPHRASE}/g' /home/jenkins/.m2/settings.xml
sed -i 's/{{_USERNAME}}/${OSS_BOT_USERNAME}/g' /home/jenkins/.m2/settings.xml
sed -i 's/{{_PASSWORD}}/${OSS_BOT_PASSWORD}/g' /home/jenkins/.m2/settings.xml
sed -i s,[email protected]:Commonjava/${params.LIB_NAME}.git,https://`python3 -c 'print("${params.LIB_GIT_REPO}".split("//")[1])'`,g pom.xml
sed -i s,[email protected]:release-engineering/${params.LIB_NAME}.git,https://`python3 -c 'print("${params.LIB_GIT_REPO}".split("//")[1])'`,g pom.xml
sed -i s,https://github.com/Commonjava/${params.LIB_NAME}.git,https://`python3 -c 'print("${params.LIB_GIT_REPO}".split("//")[1])'`,g pom.xml
sed -i s,https://github.com/release-engineering/${params.LIB_NAME}.git,https://`python3 -c 'print("${params.LIB_GIT_REPO}".split("//")[1])'`,g pom.xml
"""
catchError(buildResult: 'SUCCESS', stageResult: 'SUCCESS') {
sh """
git config --global user.email "${params.BOT_EMAIL}"
git config --global user.name "${BOT_USERNAME}"
git commit -am "Update license header" #commit nothing when there is no file needs to be modified
git push https://${BOT_USERNAME}:${BOT_PASSWORD}@`python3 -c 'print("${params.LIB_GIT_REPO}".split("//")[1])'` --all
"""
}
}
}
}
}
}
stage('Release Prepare'){
when{
expression {
return params.SKIP_PREPARE == false
}
}
steps{
script{
withCredentials([
usernamePassword(credentialsId:'GitHub_Bot', passwordVariable:'BOT_PASSWORD', usernameVariable:'BOT_USERNAME')
]){
dir(params.LIB_NAME){
env.LIB_NEXT_VERSION = params.LIB_DEV_VERSION ? params.LIB_DEV_VERSION : sh (
script: """ echo ${params.LIB_MAJOR_VERSION} | awk -F. -v OFS=. 'NF==1{print ++\$NF}; NF>1{if(length(\$NF+1)>length(\$NF))\$(NF-1)++; \$NF=sprintf("%0*d", length(\$NF), (\$NF+1)%(10^length(\$NF))); print}' """,
returnStdout: true
).trim()
echo "Releasing ${params.LIB_NAME}:${params.LIB_MAJOR_VERSION} and setup next dev version ${env.LIB_NEXT_VERSION}-SNAPSHOT"
sh """
mvn help:effective-settings
mvn --batch-mode release:prepare -DreleaseVersion=${params.LIB_MAJOR_VERSION} -DdevelopmentVersion=${env.LIB_NEXT_VERSION}-SNAPSHOT -Dtag=${params.LIB_NAME}-${params.LIB_MAJOR_VERSION} -Dusername=${BOT_USERNAME} -Dpassword=${BOT_PASSWORD}
"""
}
}
}
}
}
stage('Release performing'){
steps{
script{
dir(params.LIB_NAME){
if (params.SKIP_PREPARE == true){
sh """
cat > release.properties << EOF
scm.url=scm:git:https://`python3 -c 'print("${params.LIB_GIT_REPO}".split("//")[1])'`
scm.tag=${params.LIB_NAME}-${params.LIB_MAJOR_VERSION}
EOF
"""
}
sh """
mvn --batch-mode release:perform
"""
}
}
}
}
}
post {
success {
script {
echo "SUCCEED"
}
}
failure {
script {
if (params.MAIL_ADDRESS){
try {
sendBuildStatusEmail('failed')
} catch (e) {
echo "Error sending email: ${e}"
}
}
}
}
}
}
def sendBuildStatusEmail(String status) {
def recipient = params.MAIL_ADDRESS
def subject = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} ${status}."
def body = "Build URL: ${env.BUILD_URL}"
if (env.PR_NO) {
subject = "Jenkins job ${env.JOB_NAME}, PR #${env.PR_NO} ${status}."
body += "\nPull Request: ${env.PR_URL}"
}
if (!env.INDY_PMD_VIOLATION){
body += "\nFound code PMD Violations; please check"
}
emailext to: recipient, subject: subject, body: body
}