Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 1 | // Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 2 | // |
| 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 Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 15 | def appRepo = '${appRepo}' |
| 16 | def appName = '${appName}' |
| 17 | def apiVersion = '${apiVersion}' |
| 18 | def nextApiVersion = '${nextApiVersion}' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 19 | def version = '${version}' |
| 20 | def nextVersion = '${nextVersion}' |
| 21 | def branch = '${branch}' |
| 22 | def jdkDistro = '${jdkDistro}' |
| 23 | |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 24 | // ----------------------------------------------------------------------- |
| 25 | // Intent: Identify running script. callstack() cannot be used here, |
| 26 | // jenkins + serialization alters stack trace making display unreliable. |
| 27 | // ----------------------------------------------------------------------- |
| 28 | String 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 | // ----------------------------------------------------------------------- |
| 38 | void 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 | // ----------------------------------------------------------------------- |
| 48 | void 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 | // ----------------------------------------------------------------------- |
| 58 | void 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 Armstrong | 0c0f821 | 2024-01-05 21:06:24 -0500 | [diff] [blame] | 71 | sh 'gitdir=$(git rev-parse --git-dir) && /bin/ls -l ${gitdir}/hooks/*' |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 72 | |
Joey Armstrong | d554d2a | 2024-01-08 19:10:08 -0500 | [diff] [blame] | 73 | println(''' |
| 74 | |
| 75 | ** ----------------------------------------------------------------------- |
| 76 | ** git config --list |
| 77 | ** ----------------------------------------------------------------------- |
| 78 | ''') |
| 79 | sh 'git config --list' |
| 80 | |
| 81 | println(''' |
| 82 | |
| 83 | ** ----------------------------------------------------------------------- |
| 84 | ** git config --global --list |
| 85 | ** ----------------------------------------------------------------------- |
| 86 | ''') |
| 87 | sh 'git config --global --list' |
| 88 | |
| 89 | println('\nWANTED: git config --bool --get gerrit.createChangeId') |
| 90 | sh '''git config --bool --get gerrit.createChangedId' && echo "gerrit.createChangedId=[$?]''' |
| 91 | |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 92 | leave(name) |
| 93 | |
| 94 | return |
| 95 | } |
| 96 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 97 | // This pipeline updates the <version> tag in the root pom.xml for the |
| 98 | // given app repo, and pushes two new Gerrit changes: |
| 99 | // 1) With version the given ${version} (e.g., 1.0.0) |
| 100 | // 2) With ${nextVersion}-SNAPSHOT (e.g., 1.1.0-SNAPSHOT) |
| 101 | // |
| 102 | // Users must manually approve and merge these changes on Gerrit. Once merged, |
| 103 | // it's up to the maven-publish and version-tag jobs to complete the release by |
| 104 | // uploading artifacts to Sonatype and creating Git tags. |
| 105 | |
| 106 | def changeVersion(def newVersion) { |
| 107 | // Update the top-level <version> tag in the root pom.xml. |
| 108 | sh 'mvn versions:set -DnewVersion=' + newVersion + ' versions:commit' |
| 109 | } |
| 110 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 111 | def changeApiVersion(def appName, def newApiVersion) { |
| 112 | // Update the top-level <*appName*.api.version> tag in the root pom.xml. |
| 113 | sh 'mvn versions:set-property -Dproperty=' + appName + '.api.version -DnewVersion=' + newApiVersion + ' -DallowSnapshots=true versions:commit' |
| 114 | } |
| 115 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 116 | // TODO: use the declarative pipeline syntax, like all other groovy files. |
| 117 | // This implementation is based on the legacy cord-onos-publisher/Jenkinsfile.release |
Hung-Wei Chiu | f6cbde2 | 2021-04-22 22:15:23 -0700 | [diff] [blame] | 118 | node ('ubuntu18.04-basebuild-1c-2g') { |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 119 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 120 | sh 'echo Releasing ' + appRepo + ' repository on ' + branch + ' branch' |
Andrea Campanella | db8eac2 | 2021-03-30 14:27:16 +0200 | [diff] [blame] | 121 | sh 'echo Releasing version ' + version + ' with API version ' + apiVersion + ' and starting ' + nextVersion + '-SNAPSHOT with API version ' + nextApiVersion + '-SNAPSHOT' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 122 | |
| 123 | // Set the JDK version |
| 124 | sh 'echo Using JDK distribution: ' + jdkDistro |
| 125 | sh 'sudo update-java-alternatives --set ' + jdkDistro |
| 126 | sh 'echo Java Version:' |
| 127 | sh 'java -version' |
| 128 | |
| 129 | def userId = wrap([$class: 'BuildUser']) { |
| 130 | return env.BUILD_USER_ID |
| 131 | } |
| 132 | |
| 133 | stage ('Configure system') { |
| 134 | echo "Job triggered by " + userId |
| 135 | // FIXME: supply Jenkins-owned known_hosts file via config_file_provider |
| 136 | // https://jenkins.io/doc/pipeline/steps/config-file-provider/ |
| 137 | sh 'ssh-keyscan -H -t rsa -p 29418 gerrit.opencord.org >> ~/.ssh/known_hosts' |
| 138 | |
| 139 | sh 'git config --global user.name "Jenkins"' |
Zack Williams | 7a7a318 | 2020-08-13 14:15:40 -0700 | [diff] [blame] | 140 | sh 'git config --global user.email "do-not-reply@opennetworking.org"' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 141 | |
| 142 | // GPG key used to sign maven artifacts |
| 143 | withCredentials([file(credentialsId: 'gpg-creds-maven', variable: 'GPUPG')]) { |
| 144 | sh 'tar -xvf $GPUPG -C ~' |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | stage ('Check out code') { |
| 149 | cleanWs() |
| 150 | |
| 151 | sshagent (credentials: ['gerrit-jenkins-user']) { |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 152 | git branch: branch, url: 'ssh://jenkins@gerrit.opencord.org:29418/' + appRepo, credentialsId: 'gerrit-jenkins-user' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 153 | |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 154 | sh 'gitdir=$(git rev-parse --git-dir) && scp -p -P 29418 jenkins@gerrit.opencord.org:hooks/commit-msg ${gitdir}/hooks/' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | stage ('Move to release version') { |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 159 | sh 'echo app version ' + version |
| 160 | sh 'echo api version ' + apiVersion |
| 161 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 162 | //Splitting version and apiVersion and check if apiVersion different from empty then update API it. |
| 163 | //Allows to release apps that dont' have api.version (e.g. bng,pppoe,kafka) |
Andrea Campanella | 4077801 | 2021-03-29 11:36:16 +0200 | [diff] [blame] | 164 | changeVersion(version) |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 165 | if (!params.apiVersion.isEmpty()) { |
| 166 | sh 'echo releasing api version' + '"' + apiVersion +'"' |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 167 | changeApiVersion(appName, apiVersion) |
| 168 | } |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 169 | |
| 170 | git_debug("Move to release version") |
| 171 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 172 | sh 'git add -A && git commit -m "Release app version ' + version + ' with API version ' + apiVersion + '"' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | stage ('Verify code') { |
| 176 | def found = sh script:'egrep -R SNAPSHOT .', returnStatus:true |
| 177 | |
| 178 | if (found == 0) { |
| 179 | timeout(time: 1, unit: 'HOURS') { |
| 180 | metadata = input id: 'manual-verify', |
| 181 | message: 'Found references to SNAPSHOT in the code. Are you sure you want to release?', |
| 182 | submitter: userId |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | stage ('Push to Gerrit') { |
| 188 | sshagent (credentials: ['gerrit-jenkins-user']) { |
| 189 | sh 'git push origin HEAD:refs/for/' + branch |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | stage ('Move to next SNAPSHOT version') { |
| 194 | def snapshot = nextVersion + '-SNAPSHOT' |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 195 | def apiSnapshot = nextApiVersion + '-SNAPSHOT' |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 196 | |
| 197 | sh 'echo next app version ' + nextVersion |
| 198 | sh 'echo next api version ' + nextApiVersion |
| 199 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 200 | changeVersion(snapshot) |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 201 | if (!params.nextApiVersion.isEmpty()) { |
| 202 | sh 'echo moving to next api version' + '"' + nextApiVersion +'"' |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 203 | changeApiVersion(appName, apiSnapshot) |
| 204 | } |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 205 | |
Joey Armstrong | d206353 | 2024-01-08 17:58:29 -0500 | [diff] [blame] | 206 | git_debug("Move to next SNAPSHOT version") |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 207 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 208 | sh 'git add -A && git commit -m "Starting snapshot ' + snapshot + ' with API version ' + apiSnapshot + '"' |
Joey Armstrong | d206353 | 2024-01-08 17:58:29 -0500 | [diff] [blame] | 209 | |
| 210 | println("\nSnapshot commit message: branch=HEAD") |
| 211 | sh """git log -1 --pretty=format:'%b' HEAD""" |
| 212 | |
| 213 | println("\nSnapshot commit message: branch=" + branch) |
| 214 | sh """git log -1 --pretty=format:'%b' """ + branch |
| 215 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 216 | sshagent (credentials: ['gerrit-jenkins-user']) { |
| 217 | sh 'git push origin HEAD:refs/for/' + branch |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | stage ('Finish') { |
| 222 | sh 'echo "Release done!"' |
| 223 | sh 'echo "Go to Gerrit and merge new changes"' |
| 224 | sh 'echo "Go to http://oss.sonatype.org and release the artifacts (after the maven-publish job completes)"' |
| 225 | } |
| 226 | } |
Joey Armstrong | d206353 | 2024-01-08 17:58:29 -0500 | [diff] [blame] | 227 | |
| 228 | // [EOF] |