blob: e6c759a131fdda59c196a673347aa4f31f761eeb [file] [log] [blame]
pierventre0289d492020-09-04 21:26:53 +02001// Copyright 2020-present Open Networking Foundation
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
15def project = '${project}'
16def version = '${version}'
17def nextVersion = '${nextVersion}'
18def branch = '${branch}'
19def snapshot = '${nextVersion}-SNAPSHOT'
20
21// This pipeline updates the <version> tag according to the
22// given repo, and pushes two new Gerrit changes:
23// 1) With version the given ${version} (e.g., 1.0.0)
24// 2) With ${nextVersion}-SNAPSHOT (e.g., 1.1.0-SNAPSHOT)
25//
26// Users must manually approve and merge these changes on Gerrit. Once merged,
27// it's up to the maven-publish and version-tag jobs to complete the release by
28// uploading artifacts to Sonatype and creating Git tags.
29
30def changeVersion(def newVersion) {
31 /* Update the top-level <version> tag. */
32 sh( script: """
33 #!/usr/bin/env bash
34 set -eu -o pipefail
35
36 # Artifact with VERSION file
37 if [ -f "VERSION" ]
38 then
39 echo "${newVersion}" > VERSION
40 # Maven artifact
41 elif [ -f "pom.xml" ]
42 then
43 mvn versions:set -DnewVersion="${newVersion}" versions:commit
44 else
45 echo "ERROR: No versioning file found!"
46 exit 1
47 fi
48 """)
49}
50
51/* artifact-release pipeline */
52pipeline {
53
54 /* executor is determined by JJB */
55 agent {
56 label "${params.buildNode}"
57 }
58
59 stages {
60
61 /* clone the artifact and install the commit hook */
62 stage('Checkout') {
63 steps {
64 sshagent (credentials: ['onos-jenkins-ssh']) {
65 git branch: branch, url: "ssh://jenkins@gerrit.onosproject.org:29418/${params.project}", credentialsId: 'onos-jenkins-ssh'
66 sh 'gitdir=$(git rev-parse --git-dir); scp -p -P 29418 jenkins@gerrit.onosproject.org:hooks/commit-msg ${gitdir}/hooks/'
67 }
68 }
69 }
70
71 /* configure the repository */
72 stage('Configure') {
73 steps {
74 sh 'echo Releasing ' + project + ' repository on ' + branch + ' branch'
75 sh 'echo Releasing version ' + version + ' and starting ' + nextVersion + '-SNAPSHOT'
76 sh 'git config --global user.name "Jenkins"'
77 sh 'git config --global user.email "jenkins@onlab.us"'
78 }
79 }
80
81 /* release commit */
82 stage ('Move to release version') {
83 steps {
84 changeVersion(version)
85 sh 'git add -A && git commit -m "Release version ' + version + '"'
86 }
87 }
88
89 /* verify step */
90 stage ('Verify code') {
91 steps {
92 script {
93 found = sh(script:"egrep -R SNAPSHOT . --exclude=$egrepExclude || true", returnStdout: true).trim()
94 }
95 sh( script: """
96 #!/usr/bin/env bash
97 set -eu -o pipefail
98 if [ -n "$found" ]; then
99 echo "Found references to SNAPSHOT in the code. Are you sure you want to release?"
100 echo "$found"
101 exit 1
102 fi
103 """)
104 }
105 }
106
107 /* push to gerrit the release/tag commit */
108 stage ('Push to Gerrit') {
109 steps {
110 sshagent (credentials: ['onos-jenkins-ssh']) {
111 sh 'git push origin HEAD:refs/for/' + branch
112 }
113 }
114 }
115
116 /* snapshot commit */
117 stage ('Move to next SNAPSHOT version') {
118 steps {
119 changeVersion(snapshot)
120 sh 'git add -A && git commit -m "Starting snapshot ' + snapshot + '"'
121 sshagent (credentials: ['onos-jenkins-ssh']) {
122 sh 'git push origin HEAD:refs/for/' + branch
123 }
124 }
125 }
126
127 /* finish step */
128 stage ('Finish') {
129 steps {
130 sh 'echo "Release done!"'
131 sh 'echo "Go to Gerrit and merge new changes"'
132 sh 'echo "Go to http://oss.sonatype.org and release the artifacts (after the maven-publish job completes)"'
133 }
134 }
135 }
136
137}