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 | ** ----------------------------------------------------------------------- |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 76 | ** DEBUG: git config --list |
Joey Armstrong | d554d2a | 2024-01-08 19:10:08 -0500 | [diff] [blame] | 77 | ** ----------------------------------------------------------------------- |
| 78 | ''') |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 79 | |
| 80 | sh '''echo "RUNNING: git config --list" ''' |
Joey Armstrong | d554d2a | 2024-01-08 19:10:08 -0500 | [diff] [blame] | 81 | sh 'git config --list' |
| 82 | |
| 83 | println(''' |
| 84 | |
| 85 | ** ----------------------------------------------------------------------- |
| 86 | ** git config --global --list |
| 87 | ** ----------------------------------------------------------------------- |
| 88 | ''') |
| 89 | sh 'git config --global --list' |
| 90 | |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 91 | leave(name) |
| 92 | |
| 93 | return |
| 94 | } |
| 95 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 96 | // This pipeline updates the <version> tag in the root pom.xml for the |
| 97 | // given app repo, and pushes two new Gerrit changes: |
| 98 | // 1) With version the given ${version} (e.g., 1.0.0) |
| 99 | // 2) With ${nextVersion}-SNAPSHOT (e.g., 1.1.0-SNAPSHOT) |
| 100 | // |
| 101 | // Users must manually approve and merge these changes on Gerrit. Once merged, |
| 102 | // it's up to the maven-publish and version-tag jobs to complete the release by |
| 103 | // uploading artifacts to Sonatype and creating Git tags. |
| 104 | |
| 105 | def changeVersion(def newVersion) { |
| 106 | // Update the top-level <version> tag in the root pom.xml. |
| 107 | sh 'mvn versions:set -DnewVersion=' + newVersion + ' versions:commit' |
| 108 | } |
| 109 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 110 | def changeApiVersion(def appName, def newApiVersion) { |
| 111 | // Update the top-level <*appName*.api.version> tag in the root pom.xml. |
| 112 | sh 'mvn versions:set-property -Dproperty=' + appName + '.api.version -DnewVersion=' + newApiVersion + ' -DallowSnapshots=true versions:commit' |
| 113 | } |
| 114 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 115 | // TODO: use the declarative pipeline syntax, like all other groovy files. |
| 116 | // 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] | 117 | node ('ubuntu18.04-basebuild-1c-2g') { |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 118 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 119 | sh 'echo Releasing ' + appRepo + ' repository on ' + branch + ' branch' |
Andrea Campanella | db8eac2 | 2021-03-30 14:27:16 +0200 | [diff] [blame] | 120 | 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] | 121 | |
| 122 | // Set the JDK version |
| 123 | sh 'echo Using JDK distribution: ' + jdkDistro |
| 124 | sh 'sudo update-java-alternatives --set ' + jdkDistro |
| 125 | sh 'echo Java Version:' |
| 126 | sh 'java -version' |
| 127 | |
| 128 | def userId = wrap([$class: 'BuildUser']) { |
| 129 | return env.BUILD_USER_ID |
| 130 | } |
| 131 | |
| 132 | stage ('Configure system') { |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 133 | |
| 134 | String attempt = "1" |
| 135 | enter('Configure system: ' + attempt) |
| 136 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 137 | echo "Job triggered by " + userId |
| 138 | // FIXME: supply Jenkins-owned known_hosts file via config_file_provider |
| 139 | // https://jenkins.io/doc/pipeline/steps/config-file-provider/ |
| 140 | sh 'ssh-keyscan -H -t rsa -p 29418 gerrit.opencord.org >> ~/.ssh/known_hosts' |
| 141 | |
| 142 | sh 'git config --global user.name "Jenkins"' |
Zack Williams | 7a7a318 | 2020-08-13 14:15:40 -0700 | [diff] [blame] | 143 | sh 'git config --global user.email "do-not-reply@opennetworking.org"' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 144 | |
| 145 | // GPG key used to sign maven artifacts |
| 146 | withCredentials([file(credentialsId: 'gpg-creds-maven', variable: 'GPUPG')]) { |
| 147 | sh 'tar -xvf $GPUPG -C ~' |
| 148 | } |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 149 | leave('Configure system: ' + attempt) |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | stage ('Check out code') { |
| 153 | cleanWs() |
| 154 | |
| 155 | sshagent (credentials: ['gerrit-jenkins-user']) { |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 156 | 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] | 157 | |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 158 | 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] | 159 | } |
| 160 | } |
| 161 | |
| 162 | stage ('Move to release version') { |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 163 | sh 'echo app version ' + version |
| 164 | sh 'echo api version ' + apiVersion |
| 165 | |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 166 | //Splitting version and apiVersion and check if apiVersion different from empty then update API it. |
| 167 | //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] | 168 | changeVersion(version) |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 169 | if (!params.apiVersion.isEmpty()) { |
| 170 | sh 'echo releasing api version' + '"' + apiVersion +'"' |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 171 | changeApiVersion(appName, apiVersion) |
| 172 | } |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 173 | |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 174 | git_debug("Move to release version") |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 175 | |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 176 | // Explicitly set hooksPath. Need to figure out logic to detect and |
| 177 | // test if unset -- else multiple cases where Exception is thrown. |
| 178 | sh """git config --local core.hooksPath './.git/hooks'""" // limited scope |
| 179 | sh 'git add -A && git commit -sm "Release app version ' + version + ' with API version ' + apiVersion + '"' |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | stage ('Verify code') { |
| 183 | def found = sh script:'egrep -R SNAPSHOT .', returnStatus:true |
| 184 | |
| 185 | if (found == 0) { |
| 186 | timeout(time: 1, unit: 'HOURS') { |
| 187 | metadata = input id: 'manual-verify', |
| 188 | message: 'Found references to SNAPSHOT in the code. Are you sure you want to release?', |
| 189 | submitter: userId |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | stage ('Push to Gerrit') { |
| 195 | sshagent (credentials: ['gerrit-jenkins-user']) { |
| 196 | sh 'git push origin HEAD:refs/for/' + branch |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | stage ('Move to next SNAPSHOT version') { |
| 201 | def snapshot = nextVersion + '-SNAPSHOT' |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 202 | def apiSnapshot = nextApiVersion + '-SNAPSHOT' |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 203 | |
| 204 | sh 'echo next app version ' + nextVersion |
| 205 | sh 'echo next api version ' + nextApiVersion |
| 206 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 207 | changeVersion(snapshot) |
Andrea Campanella | b10b136 | 2022-01-10 12:17:42 +0100 | [diff] [blame] | 208 | if (!params.nextApiVersion.isEmpty()) { |
| 209 | sh 'echo moving to next api version' + '"' + nextApiVersion +'"' |
Andrea Campanella | c638229 | 2021-03-26 13:17:43 +0100 | [diff] [blame] | 210 | changeApiVersion(appName, apiSnapshot) |
| 211 | } |
Joey Armstrong | 013ad21 | 2024-01-05 17:10:24 -0500 | [diff] [blame] | 212 | |
Joey Armstrong | d206353 | 2024-01-08 17:58:29 -0500 | [diff] [blame] | 213 | git_debug("Move to next SNAPSHOT version") |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 214 | sh 'git add -A && git commit -sm "Starting snapshot ' + snapshot + ' with API version ' + apiSnapshot + '"' |
Joey Armstrong | d206353 | 2024-01-08 17:58:29 -0500 | [diff] [blame] | 215 | |
| 216 | println("\nSnapshot commit message: branch=HEAD") |
| 217 | sh """git log -1 --pretty=format:'%b' HEAD""" |
| 218 | |
| 219 | println("\nSnapshot commit message: branch=" + branch) |
| 220 | sh """git log -1 --pretty=format:'%b' """ + branch |
| 221 | |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 222 | sshagent (credentials: ['gerrit-jenkins-user']) { |
| 223 | sh 'git push origin HEAD:refs/for/' + branch |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | stage ('Finish') { |
| 228 | sh 'echo "Release done!"' |
| 229 | sh 'echo "Go to Gerrit and merge new changes"' |
| 230 | sh 'echo "Go to http://oss.sonatype.org and release the artifacts (after the maven-publish job completes)"' |
Eric Ball | e5a79d7 | 2025-01-17 15:37:36 -0800 | [diff] [blame] | 231 | |
| 232 | // Restore config: secure but what changed (?) |
| 233 | // Should revert completely to 'factory defaults' |
| 234 | println("Revert core.hooksPath") |
| 235 | sh """git config --local core.hooksPath '/dev/null'""" // limited scope |
| 236 | } |
Carmelo Cascone | 9c00dce | 2019-12-05 18:33:04 -0800 | [diff] [blame] | 237 | } |
Joey Armstrong | d206353 | 2024-01-08 17:58:29 -0500 | [diff] [blame] | 238 | |
| 239 | // [EOF] |