1- /**
2- Jenkins Pipeline Project
3- (MULTIBRANCH - MULTIPROJECT - declarative)
4-
5- NOTES:
6- - This pipeline contains the SCM info/url and ignores the parent's
7- scm object. This allows setting up SCM pooling on specific paths,
8- only building a project if it has changes (for multi-project
9- Git repo).
10- (also see notes in the "Checkout" stage and getGitBranchName)
11-
12- - A requirement for proper multi-project functionality is that the
13- Jenkins project name MUST match the project's folder name in the
14- Git repository.
15- */
16-
17- // Git URL
18- def projectGitURL = ' https://gccode.ssc-spc.gc.ca/iitb-dgiit/sds/GOCWebTemplates/JavaTemplates.git'
19- def projectGitCredsName = ' '
20-
21- // applicationName is derived from Jenkins project name (which is second to last in the full name)
22- def jobPathElements = currentBuild. fullProjectName. split(' /' )
23- def applicationName = jobPathElements[jobPathElements. length >= 2 ? jobPathElements. length-2 : jobPathElements. length-1 ]
24-
251// Project's POM file
26- def projectPom = applicationName + ' /pom.xml'
27-
28- // Git Branch/Paths to match for changes
29- def projectGitWatchedPathRegex = applicationName + ' /.*'
30- def projectGitWatchedBranches = [[name : ' */' + env. BRANCH_NAME ]]
31- def projectReleaseBranchRegex = ' master.*'
2+ def applicationName = ' JavaTemplates'
3+ def projectPom = ' ./pom.xml'
324
335// Email extension plugin base parameters
346def emailextConfig = [
35- to : ' pierre.lupien@hrdc-drhc.net ' , // comma-separated lits of addresses
7+ to : ' pierre.lupien@hrsdc-rhdcc.gc.ca,ahmad.shahid@hrsdc-rhdcc.gc.ca ' , // comma-separated lits of addresses
368 from : ' Jenkins-CI <jenkins-ci@jade-build.intra.dev>' ,
379 body : ' ${SCRIPT, template="groovy-html.template"}' , // for details on body: https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin#Email-extplugin-Scriptcontent
3810 mimeType : ' text/html'
3911 ]
4012
41-
42- def getGitBranchName () {
43-
44- def branchName = env. BRANCH_NAME
45-
46- // If this is not run in a MULTIBRANCH pipeline, BRANCH_NAME will not
47- // be available. In that case the branch name can be acquired from
48- // Git (requires the "Chekcout to matching local branch" option to
49- // be enabled in SCM).
50- // def git = tool('git')
51- // def branchName = sh(script: "'${git}' branch | grep \"\\* \"", returnStdout: true).replace("* ", "").trim()
52-
53- echo(' Detected Git Branch: [' + branchName + ' ]' )
54-
55- return branchName
56- }
57-
58- /* Returns the specified projectModel's version by looking
59- first to see if a version is defined, and if not go to
60- the parent's version
61-
62- Fails build is no version is found at all.
63- */
64- def getEffectiveVersionFromProjectModel (projectModel ) {
65-
66- def projectVersion = projectModel. version
67-
68- if (! projectVersion) { // version is null, get from parent (if any)
69- if (projectModel. parent) projectVersion = projectModel. parent. version
70- }
71-
72- if (! projectVersion) { // still no version... fail
73- error(' Could not find version in project model. Make sure pom.xml specifies a version or a parent pom version.' )
74- }
75-
76- return projectVersion
77- }
78-
79- /* Returns the specified projectModel's groupId by looking
80- first to see if a groupId is defined, and if not go to
81- the parent's groupId
82-
83- Fails build is no groupId is found at all.
84- */
85- def getEffectiveGroupIdFromProjectModel (projectModel ) {
86-
87- def projectGroupId = projectModel. groupId
88-
89- if (! projectGroupId) { // groupId is null, get from parent (if any)
90- if (projectModel. parent) projectGroupId = projectModel. parent. groupId
91- }
92-
93- if (! projectGroupId) { // still no groupId... fail
94- error(' Could not find groupId in project model. Make sure pom.xml specifies a groupId or a parent pom groupId.' )
95- }
96-
97- return projectGroupId
98- }
99-
100-
10113pipeline {
10214
10315 agent any; // any: Run on any available agent - agent is same for all stages
@@ -107,83 +19,99 @@ pipeline {
10719 buildDiscarder logRotator(artifactNumToKeepStr : ' 5' , numToKeepStr : ' 10' )
10820 disableConcurrentBuilds()
10921 timestamps()
110- skipDefaultCheckout() // skip SCM pull before first stage, we'll do our own
11122 }
11223
11324 tools {
11425 maven(' maven' )
11526 git(' git' )
27+ ant(' Ant' )
28+ jdk(' JDK11' )
11629 }
117-
118- triggers {
119- // pollSCM('H H/4 * * *') //every 4 hours
120- snapshotDependencies()
30+
31+ parameters {
32+ string(defaultValue : ' ' , description : ' Deploy version (e.g. 2.0.1) Leave blank to use version form source code.' , name : ' DEPLOY_VERSION' , trim : true )
12133 }
12234
12335 stages {
124- stage(' Checkout from SCM' ) {
125- steps {
126- // We won't be using the simple "checkout(scm)" here because we want
127- // to setup our own path restriction and that is not supported
128- // by the default multibranch git provider.
129- // checkout(scm)
130-
131- checkout([$class : ' GitSCM' ,
132- userRemoteConfigs : [[credentialsId : projectGitCredsName,
133- url : projectGitURL]],
134- branches : projectGitWatchedBranches,
135- extensions : [[$class : ' PathRestriction' , // SCM poll filter by path
136- excludedRegions : ' ' ,
137- includedRegions : projectGitWatchedPathRegex],
138- [$class : ' LocalBranch' , // Checkout as named local branch, required for our getGitBranchName to work on non-multibranch pipeline
139- localBranch : ' **' ]]])
140- }
141- }
142-
14336 stage(' Project Artifact Version Check' ) {
144- // Could only use "when branch" in a multibranch build
145- // when {branch('master')}
146- // Could also use "when expression" for the "if", but we have an "else" and don't want to create two stages for this
147- // when { expression {return getGitBranchName().matches(projectReleaseBranchRegex)} }
148-
14937 steps {
15038 script {
15139 def projectModel = readMavenPom(file : projectPom)
15240
153- if (getGitBranchName(). matches(projectReleaseBranchRegex)) {
154- // RELEASE build: make sure our dependencies are not snapshots
155- def mavenDesc = Artifactory . mavenDescriptor()
41+ if (params. DEPLOY_VERSION . isEmpty()) {
42+ // No version parameter specified: make sure the one from pom file is Snapshot
15643
157- mavenDesc. pomFile = projectPom
158- if (mavenDesc. hasSnapshots()) {
44+ if (! projectModel. version. endsWith(' -SNAPSHOT' )) {
15945 currentBuild. result = ' ABORTED'
160- error(' Snapshot(s) detected in dependencies. Based on the branch, this is a release build. Snapshot dependencies are not allowed .' )
46+ error(' Building non-SNAPSHOT versions must be explicitly triggerd by specifying the DEPLOY_VERSION parameter .' )
16147 }
162-
163- // Also make sure our own version is not a snapshot
164- if (getEffectiveVersionFromProjectModel(projectModel). toUpperCase(). endsWith(' -SNAPSHOT' )) {
48+ }
49+ else {
50+ // A version parameter was specified, perform some checks
51+
52+ if (params. DEPLOY_VERSION . contains(" " )) {
16553 currentBuild. result = ' ABORTED'
166- error(' Trying to build a SNAPSHOT project version from a release branch. Please update the pom.xml ' )
54+ error(' DEPLOY_VERSION must not contain spaces. ' )
16755 }
168- } else {
169- // NOT a release build: Warn if building a release version.
170- if (! getEffectiveVersionFromProjectModel(projectModel). toUpperCase(). endsWith(' -SNAPSHOT' )) {
171- currentBuild. result = ' UNSTABLE'
172- echo(' WARNING: Trying to build RELEASE (ie non-SNAPSHOT) project version from a non-release branch. Are you sure this is what you want? If not you will want to update the pom.xml.' )
56+
57+ if (! params. DEPLOY_VERSION . toUpperCase(). endsWith(' -SNAPSHOT' )) {
58+ // RELEASE build: make sure our dependencies are not snapshots
59+ def mavenDesc = Artifactory . mavenDescriptor()
60+
61+ mavenDesc. pomFile = projectPom
62+ if (mavenDesc. hasSnapshots()) {
63+ currentBuild. result = ' ABORTED'
64+ error(' Snapshot(s) detected in dependencies. This is a release build. Snapshot dependencies are not allowed.' )
65+ }
17366 }
17467 }
17568 } // of script
17669 } // of steps
17770 } // of stage
71+
72+ stage(' Release build confirmation' ) {
73+ when {
74+ beforeInput true
75+ expression {
76+ return (! params. DEPLOY_VERSION . isEmpty()) && (! params. DEPLOY_VERSION . toUpperCase(). endsWith(' -SNAPSHOT' ));
77+ }
78+ }
79+ input {
80+ message " About to deploy RELEASE version [${ params.DEPLOY_VERSION} ] to Artifactory, are you sure?"
81+ ok ' Yes, deploy!'
82+ submitterParameter ' DEPLOY_SUBMITTER'
83+ }
84+ steps {
85+ script {
86+ sh(script : " echo Release build authorized." )
87+ }
88+ }
89+ }
17890
179- stage(' Build and Deploy to Artifactory ' ) {
91+ stage(' Build and Deploy' ) {
18092 steps {
18193 script {
18294 def git = tool(' git' )
18395 def gitCommitId = sh(script : " '${ git} ' rev-parse HEAD" , returnStdout : true ). trim()
18496
185- withMaven(maven : ' maven' ) {
186- sh(script : " mvn --batch-mode --errors --update-snapshots -Dbuild_number=${ BUILD_NUMBER} -Dbuild_git_commitid=${ gitCommitId} -f ${ applicationName} clean deploy" )
97+ withAnt(ant : ' Ant' , jdk : ' JDK11' ) {
98+ withMaven(maven : ' maven' , jdk : ' JDK11' ) {
99+ // ---[ If an explicit version was specified, override source's versions
100+ if (! params. DEPLOY_VERSION . isEmpty()) {
101+ sh(script : " ant -buildfile ./builds/build-setprojectversion.xml \" -Dgocwebtemplate.build.version=${ params.DEPLOY_VERSION} \" " )
102+ }
103+
104+ // ---[ Build/deploy main projects
105+ sh(script : " mvn --batch-mode --errors --update-snapshots -Dbuild_number=${ BUILD_NUMBER} -Dbuild_git_commitid=${ gitCommitId} --file ${ projectPom} clean deploy" )
106+
107+ // ---[ Build/deploy archetypes
108+ sh(script : " ant -buildfile ./builds/build-archetypes.xml" )
109+
110+ // ---[ If this is a release version, build ZIP file for external clients
111+ if ((! params. DEPLOY_VERSION . isEmpty()) && (! params. DEPLOY_VERSION . toUpperCase(). endsWith(' -SNAPSHOT' ))) {
112+ sh(script : " ant -buildfile ./builds/build-release.xml" )
113+ }
114+ }
187115 }
188116 }
189117 }
@@ -192,9 +120,10 @@ pipeline {
192120
193121 post {
194122 always { // Always run, regardless of build status
195- // archiveArtifacts(artifacts: "${applicationName}/target/*.?ar", allowEmptyArchive: true, fingerprint: true)
123+ archiveArtifacts(artifacts : " gocwebtemplate-*/**/target/*.?ar" , allowEmptyArchive : true , fingerprint : true )
124+ archiveArtifacts(artifacts : " builds/target/gocwebtemplate-*-${ params.DEPLOY_VERSION} .zip" , allowEmptyArchive : true , fingerprint : true )
196125
197- junit(testResults : " ${ applicationName } /target/surefire-reports/TEST-*.xml" , allowEmptyResults : true )
126+ junit(testResults : " gocwebtemplate-*/** /target/surefire-reports/TEST-*.xml" , allowEmptyResults : true )
198127
199128 emailext(to : emailextConfig. to,
200129 from : emailextConfig. from,
0 commit comments