blob: 5ae2ab60dfbbf224631220545540ae8c9bad455a [file] [log] [blame]
Joey Armstrong013ad212024-01-05 17:10:24 -05001// Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Carmelo Cascone9c00dce2019-12-05 18:33:04 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Andrea Campanellac6382292021-03-26 13:17:43 +010015def appRepo = '${appRepo}'
16def appName = '${appName}'
17def apiVersion = '${apiVersion}'
18def nextApiVersion = '${nextApiVersion}'
Carmelo Cascone9c00dce2019-12-05 18:33:04 -080019def version = '${version}'
20def nextVersion = '${nextVersion}'
21def branch = '${branch}'
22def jdkDistro = '${jdkDistro}'
23
Joey Armstrong013ad212024-01-05 17:10:24 -050024// -----------------------------------------------------------------------
25// Intent: Identify running script. callstack() cannot be used here,
26// jenkins + serialization alters stack trace making display unreliable.
27// -----------------------------------------------------------------------
28String getIam(String func) {
29 // Cannot rely on a stack trace due to jenkins manipulation
30 String src = 'jjb/pipeline/onos-app-release.groovy'
31 String iam = [src, func].join('::')
32 return iam
33}
34
35// -----------------------------------------------------------------------
36// Intent: Log progress message
37// -----------------------------------------------------------------------
38void enter(String name) {
39 // Announce ourselves for log usability
40 String iam = getIam(name)
41 println("${iam}: ENTER")
42 return
43}
44
45// -----------------------------------------------------------------------
46// Intent: Log progress message
47// -----------------------------------------------------------------------
48void leave(String name) {
49 // Announce ourselves for log usability
50 String iam = getIam(name)
51 println("${iam}: LEAVE")
52 return
53}
54
55// -----------------------------------------------------------------------
56// https://jenkins.opencord.org/job/onos-app-release/285/consoleFull
57// -----------------------------------------------------------------------
58void git_debug(String name) {
59 enter(name)
60
61 println('''
62
63** -----------------------------------------------------------------------
64** git debugging: Commit-Id string MIA
65** -----------------------------------------------------------------------
66''')
67
68 sh 'echo "PWD: $(/bin/pwd)"'
69 sh '/bin/ls -l'
70 sh 'gitdir=$(git rev-parse --git-dir) && /bin/ls -ld ${gitdir}'
Joey Armstrong0c0f8212024-01-05 21:06:24 -050071 sh 'gitdir=$(git rev-parse --git-dir) && /bin/ls -l ${gitdir}/hooks/*'
Joey Armstrong013ad212024-01-05 17:10:24 -050072
73 leave(name)
74
75 return
76}
77
Carmelo Cascone9c00dce2019-12-05 18:33:04 -080078// This pipeline updates the <version> tag in the root pom.xml for the
79// given app repo, and pushes two new Gerrit changes:
80// 1) With version the given ${version} (e.g., 1.0.0)
81// 2) With ${nextVersion}-SNAPSHOT (e.g., 1.1.0-SNAPSHOT)
82//
83// Users must manually approve and merge these changes on Gerrit. Once merged,
84// it's up to the maven-publish and version-tag jobs to complete the release by
85// uploading artifacts to Sonatype and creating Git tags.
86
87def changeVersion(def newVersion) {
88 // Update the top-level <version> tag in the root pom.xml.
89 sh 'mvn versions:set -DnewVersion=' + newVersion + ' versions:commit'
90}
91
Andrea Campanellac6382292021-03-26 13:17:43 +010092def changeApiVersion(def appName, def newApiVersion) {
93 // Update the top-level <*appName*.api.version> tag in the root pom.xml.
94 sh 'mvn versions:set-property -Dproperty=' + appName + '.api.version -DnewVersion=' + newApiVersion + ' -DallowSnapshots=true versions:commit'
95}
96
Carmelo Cascone9c00dce2019-12-05 18:33:04 -080097// TODO: use the declarative pipeline syntax, like all other groovy files.
98// This implementation is based on the legacy cord-onos-publisher/Jenkinsfile.release
Hung-Wei Chiuf6cbde22021-04-22 22:15:23 -070099node ('ubuntu18.04-basebuild-1c-2g') {
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800100
Andrea Campanellac6382292021-03-26 13:17:43 +0100101 sh 'echo Releasing ' + appRepo + ' repository on ' + branch + ' branch'
Andrea Campanelladb8eac22021-03-30 14:27:16 +0200102 sh 'echo Releasing version ' + version + ' with API version ' + apiVersion + ' and starting ' + nextVersion + '-SNAPSHOT with API version ' + nextApiVersion + '-SNAPSHOT'
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800103
104 // Set the JDK version
105 sh 'echo Using JDK distribution: ' + jdkDistro
106 sh 'sudo update-java-alternatives --set ' + jdkDistro
107 sh 'echo Java Version:'
108 sh 'java -version'
109
110 def userId = wrap([$class: 'BuildUser']) {
111 return env.BUILD_USER_ID
112 }
113
114 stage ('Configure system') {
115 echo "Job triggered by " + userId
116 // FIXME: supply Jenkins-owned known_hosts file via config_file_provider
117 // https://jenkins.io/doc/pipeline/steps/config-file-provider/
118 sh 'ssh-keyscan -H -t rsa -p 29418 gerrit.opencord.org >> ~/.ssh/known_hosts'
119
120 sh 'git config --global user.name "Jenkins"'
Zack Williams7a7a3182020-08-13 14:15:40 -0700121 sh 'git config --global user.email "do-not-reply@opennetworking.org"'
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800122
123 // GPG key used to sign maven artifacts
124 withCredentials([file(credentialsId: 'gpg-creds-maven', variable: 'GPUPG')]) {
125 sh 'tar -xvf $GPUPG -C ~'
126 }
127 }
128
129 stage ('Check out code') {
130 cleanWs()
131
132 sshagent (credentials: ['gerrit-jenkins-user']) {
Andrea Campanellac6382292021-03-26 13:17:43 +0100133 git branch: branch, url: 'ssh://jenkins@gerrit.opencord.org:29418/' + appRepo, credentialsId: 'gerrit-jenkins-user'
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800134
Joey Armstrong013ad212024-01-05 17:10:24 -0500135 sh 'gitdir=$(git rev-parse --git-dir) && scp -p -P 29418 jenkins@gerrit.opencord.org:hooks/commit-msg ${gitdir}/hooks/'
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800136 }
137 }
138
139 stage ('Move to release version') {
Andrea Campanellab10b1362022-01-10 12:17:42 +0100140 sh 'echo app version ' + version
141 sh 'echo api version ' + apiVersion
142
Andrea Campanellac6382292021-03-26 13:17:43 +0100143 //Splitting version and apiVersion and check if apiVersion different from empty then update API it.
144 //Allows to release apps that dont' have api.version (e.g. bng,pppoe,kafka)
Andrea Campanella40778012021-03-29 11:36:16 +0200145 changeVersion(version)
Andrea Campanellab10b1362022-01-10 12:17:42 +0100146 if (!params.apiVersion.isEmpty()) {
147 sh 'echo releasing api version' + '"' + apiVersion +'"'
Andrea Campanellac6382292021-03-26 13:17:43 +0100148 changeApiVersion(appName, apiVersion)
149 }
Joey Armstrong013ad212024-01-05 17:10:24 -0500150
151 git_debug("Move to release version")
152
Andrea Campanellac6382292021-03-26 13:17:43 +0100153 sh 'git add -A && git commit -m "Release app version ' + version + ' with API version ' + apiVersion + '"'
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800154 }
155
156 stage ('Verify code') {
157 def found = sh script:'egrep -R SNAPSHOT .', returnStatus:true
158
159 if (found == 0) {
160 timeout(time: 1, unit: 'HOURS') {
161 metadata = input id: 'manual-verify',
162 message: 'Found references to SNAPSHOT in the code. Are you sure you want to release?',
163 submitter: userId
164 }
165 }
166 }
167
168 stage ('Push to Gerrit') {
169 sshagent (credentials: ['gerrit-jenkins-user']) {
170 sh 'git push origin HEAD:refs/for/' + branch
171 }
172 }
173
174 stage ('Move to next SNAPSHOT version') {
175 def snapshot = nextVersion + '-SNAPSHOT'
Andrea Campanellac6382292021-03-26 13:17:43 +0100176 def apiSnapshot = nextApiVersion + '-SNAPSHOT'
Andrea Campanellab10b1362022-01-10 12:17:42 +0100177
178 sh 'echo next app version ' + nextVersion
179 sh 'echo next api version ' + nextApiVersion
180
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800181 changeVersion(snapshot)
Andrea Campanellab10b1362022-01-10 12:17:42 +0100182 if (!params.nextApiVersion.isEmpty()) {
183 sh 'echo moving to next api version' + '"' + nextApiVersion +'"'
Andrea Campanellac6382292021-03-26 13:17:43 +0100184 changeApiVersion(appName, apiSnapshot)
185 }
Joey Armstrong013ad212024-01-05 17:10:24 -0500186
Joey Armstrongd2063532024-01-08 17:58:29 -0500187 git_debug("Move to next SNAPSHOT version")
Joey Armstrong013ad212024-01-05 17:10:24 -0500188
Andrea Campanellac6382292021-03-26 13:17:43 +0100189 sh 'git add -A && git commit -m "Starting snapshot ' + snapshot + ' with API version ' + apiSnapshot + '"'
Joey Armstrongd2063532024-01-08 17:58:29 -0500190
191 println("\nSnapshot commit message: branch=HEAD")
192 sh """git log -1 --pretty=format:'%b' HEAD"""
193
194 println("\nSnapshot commit message: branch=" + branch)
195 sh """git log -1 --pretty=format:'%b' """ + branch
196
Carmelo Cascone9c00dce2019-12-05 18:33:04 -0800197 sshagent (credentials: ['gerrit-jenkins-user']) {
198 sh 'git push origin HEAD:refs/for/' + branch
199 }
200 }
201
202 stage ('Finish') {
203 sh 'echo "Release done!"'
204 sh 'echo "Go to Gerrit and merge new changes"'
205 sh 'echo "Go to http://oss.sonatype.org and release the artifacts (after the maven-publish job completes)"'
206 }
207}
Joey Armstrongd2063532024-01-08 17:58:29 -0500208
209// [EOF]