-
Notifications
You must be signed in to change notification settings - Fork 3
/
maven-Jenkinsfile
230 lines (197 loc) · 8.47 KB
/
maven-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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env groovy
/**
* Jenkinsfile for zanata-scripts
*/
@Field
public static final String ORG_BASE = 'github.com/zanata'
@Library('github.com/zanata/zanata-pipeline-library@v0.3.1')
import static org.zanata.jenkins.StackTraces.getStackTrace
import groovy.transform.Field
timestamps {
// We need a node with release label
node('release') {
currentBuild.displayName = currentBuild.displayName + " {${env.NODE_NAME}}"
// To override following variables, use
// "Properties Content" in "Prepare an environment for the run" in Jenkins Web UI
String REPO_NAME = (env.REPO_NAME) ?: 'zanata-platform'
String PROJ_BASE = (env.PROJ_BASE) ?: "${ORG_BASE}/${REPO_NAME}"
String PROJ_BRANCH = (env.PROJ_BRANCH) ?: 'release'
String ZANATA_SCRIPTS_BRANCH = (env.ZANATA_SCRIPTS_BRANCH) ?: 'master'
String WORK_ROOT = (env.WORK_ROOT) ?: "${env.JENKINS_HOME}/zanata-work-root"
String WORK_DIR = "${WORK_ROOT}/${REPO_NAME}"
def envArray = new ArrayList()
String releaseVersion = null
String artifactId = null
// chooses projects whose versions should be changed
// To be passed as -pl <Projects> in maven
String setPomVersionProjects = null
// List all artifacts to be released in sonatype nexus
// To be passed as -pl <Projects> in maven
String nexusStagingProjects = null
// pom.xml that has valid version
// e.g. parent/pom.xml in zanata-platform
String versionPomXml = "${WORK_DIR}/pom.xml"
boolean hasReleaseNotes = false
ansiColor('xterm') {
def projectProperties = [
[
$class: 'BuildDiscarderProperty',
strategy: [$class: 'LogRotator',
numToKeepStr: '20', // keep records for at most X builds
artifactDaysToKeepStr: '', // keep artifacts no more than X days
artifactNumToKeepStr: '10', // keep artifacts for at most X builds
]
],
[$class: 'GithubProjectProperty',
projectUrlStr: "https://${PROJ_BASE}"
],
[$class: 'ParametersDefinitionProperty',
parameterDefinitions: [
[$class: 'StringParameterDefinition',
defaultValue: 'auto',
description: 'Version to release like "4.4.0", "4.4.0-alpha-1", or "auto" to release the SNAPSHOT',
name: 'RELEASE_VERSION'
],
[$class: 'StringParameterDefinition',
defaultValue: 'testBuild',
description: '"" for build and commit; "testBuild" for build but not commit; "dryRun" for knowing commands to be run ',
name: 'ZANATA_RELEASE_MODE'
],
]
],
]
properties(projectProperties)
}
if (params.ZANATA_RELEASE_MODE != "" ){
currentBuild.displayName = currentBuild.displayName + " [${params.ZANATA_RELEASE_MODE}]"
}
stage('Checkout') {
// This checkout zanata-scripts
checkout scm
artifactId = sh(returnStdout: true,
script: "bash $WORKSPACE/zanata-functions run get_artifact_id ${REPO_NAME}"
).trim()
envArray.addAll(
sh( returnStdout: true,
script: "sed -rn -e '/[A-Z_]*=/ s/^[^A-Z_]*([A-Z_]*=[^}]*)/\\1/p' $WORKSPACE/zanata-env.sh",
)?.split("\n")
)
envArray.addAll([
"WORK_ROOT=${WORK_ROOT}",
"ZANATA_RELEASE_MODE=${params.ZANATA_RELEASE_MODE}",
"ZANATA_GIT_URL_PREFIX=https://${ORG_BASE}",
])
if (params.ZANATA_RELEASE_MODE == 'dryRun') {
envArray.add("DryRunMode=1")
}
withEnv(envArray) {
// Different repo has different configuration
if (REPO_NAME == 'zanata-platform') {
setPomVersionProjects = env.PLATFORM_MAVEN_VERSION_PROJECT
nexusStagingProjects = env.PLATFORM_MAVEN_NEXUS_RELEASE_PROJECTS
versionPomXml = "${WORK_DIR}/parent/pom.xml"
hasReleaseNotes = true
}
// sh("bash $WORKSPACE/zanata-functions run ensure_repo ${REPO_NAME}")
// sh("bash $WORKSPACE/zanata-functions run checkout_branch ${REPO_NAME} ${PROJ_BRANCH} ${WORK_ROOT}")
dir(WORK_DIR) {
// git branch: PROJ_BRANCH, changelog: false, credentialsId: 'zanata-jenkins', poll: false, url: "https://${PROJ_BASE}.git"
checkout changelog: false, poll: false, scm:
[$class: 'GitSCM', branches: [[name: "*/release"]],
doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'zanata-jenkins', url: "http://${ORG_BASE}/${REPO_NAME}"]]
]
if (params.RELEASE_VERSION == 'auto') {
releaseVersion = readMavenPom(file: versionPomXml).getVersion().replaceAll('-SNAPSHOT', '')
} else {
releaseVersion = params.RELEASE_VERSION
}
currentBuild.displayName = currentBuild.displayName + " $releaseVersion"
tagName = "${artifactId}-${releaseVersion}"
developmentVersion = sh(returnStdout: true,
script: "bash $WORKSPACE/zanata-functions run version_next $releaseVersion",
).trim() + '-SNAPSHOT'
echo "Release Version=$releaseVersion Development Version=$developmentVersion"
// Is this version already been tag/build ?
String remoteTagLine = sh(returnStdout: true,
script: "git ls-remote https://github.com/zanata/$REPO_NAME refs/tags/${tagName}",
)?.trim()
if ( remoteTagLine ){
error "tag ${tagName} is already in remote"
}
def tagExistExitStatus = sh(returnStatus: true,
script: "git show-ref ${tagName}",
)
if ( tagExistExitStatus == 0 ){
// Remove only local tag (possibly from a failed build)
sh "git tag -d $tagName"
}
}
}
}
withEnv(envArray) {
String developmentVersion = null
String tagName = null
dir (WORK_DIR) {
if (hasReleaseNotes){
stage('ReleaseNotes') {
String releaseNoteVersion = releaseVersion.replaceAll(/[-.]/, '')
echo "releaseNoteVersion: $releaseNoteVersion"
def releaseNoteStatus = sh(returnStatus: true,
script: "bash $WORKSPACE/zanata-release-notes-prepend -j $artifactId $releaseVersion",
)
echo "releaseNoteStatus: $releaseNoteStatus"
switch(releaseNoteStatus) {
case 0: // EXIT_OK
// ReleaseNotes updated. Git commit is done by zanata-release-notes-prepend
break
case 40: // EXIT_RETURN_FALSE
// ReleaseNotes does not changed
break
default:
error "Status($releaseNoteStatus) Failed to get related jira issues"
break
}
}
}
stage('SetReleaseVersion') {
sh(returnStatus: true,
script: "bash $WORKSPACE/zanata-pom-set-version ${artifactId}- $releaseVersion $setPomVersionProjects",
)
}
// We cannot use org.sonatype.plugins:nexus-staging-maven-plugin:1.6.8:deploy here
// It generate following error
// [ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.8:deploy (default-cli) on project build-tools: The packaging for this project did not assign a file to the build artifact
//stage('Nexus') {
// sh "bash $WORKSPACE/zanata-nexus $PLATFORM_MAVEN_NEXUS_RELEASE_PROJECTS"
//}
stage('NexusStaging') {
sh "bash $WORKSPACE/zanata-nexus-staging $nexusStagingProjects"
}
stage('NexusRelease') {
sh "bash $WORKSPACE/zanata-nexus-release -p $nexusStagingProjects"
}
stage('SetSnapshotVersion') {
sh(returnStatus: true,
script: "bash $WORKSPACE/zanata-pom-set-version ${artifactId}- $developmentVersion $setPomVersionProjects"
)
withCredentials([
[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'zanata-jenkins',
usernameVariable: 'GIT_USERNAME', passwordVariable: 'GITHUB_OAUTH2_TOKEN'
]
]) {
if ( params.ZANATA_RELEASE_MODE == '') {
sh "git push https://$GIT_USERNAME:$GITHUB_OAUTH2_TOKEN@${PROJ_BASE} release"
}
}
}
}
stage('ArtifactList') {
sh("bash $WORKSPACE/zanata-artifact-m2repo -l | sort -u > artifact-list.txt")
archiveArtifacts(artifacts: 'artifact-list.txt')
}
}
}
}