#!groovy
/*
* This work is protected under copyright law in the Kingdom of
* The Netherlands. The rules of the Berne Convention for the
* Protection of Literary and Artistic Works apply.
* Digital Me B.V. is the copyright owner.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Load Jenkins shared libraries common to all projects
def libLazy = [
remote: 'https://code.in.digital-me.nl/git/DEVops/JenkinsLibLazy.git',
branch: env.BRANCH_NAME,
credentialsId: null,
]
library(
identifier: "libLazy@${libLazy.branch}",
retriever: modernSCM([
$class: 'GitSCMSource',
remote: libLazy.remote,
credentialsId: libLazy.credentialsId
])
)
// Load Jenkins shared libraries to customize this project
def libCustom = [
remote: 'ssh://git@code.in.digital-me.nl:2222/DEVops/JenkinsLibCustom.git',
branch: env.BRANCH_NAME,
credentialsId: 'bot-ci-dgm-rsa',
]
library(
identifier: "libCustom@${libCustom.branch}",
retriever: modernSCM([
$class: 'GitSCMSource',
remote: libCustom.remote,
credentialsId: libCustom.credentialsId
])
)
// Define the remotes and the working and deploy branches
def remote = 'origin'
def workingBranch = 'master'
def releaseBranch = 'release'
// Initialize configuration
lazyConfig(
name: 'dummy-jenkins-pl',
env: [
RELEASE: false,
DRYRUN: false,
BUILD_DIR: 'target',
GIT_CRED: 'bot-ci-dgm',
DEPLOY_USER: 'root',
DEPLOY_HOST_STST: 'bxtsvctwas001.boxtel',
DEPLOY_HOST_ACC1: 'alasvctwas001.infra.qiy.nl',
DEPLOY_HOST_ACC2: 'alasvctwas002.infra.qiy.nl',
DEPLOY_HOST_PRD1: 'alpsvctwas001.infra.qiy.nl',
DEPLOY_HOST_PRD2: 'alpsvctwas002.infra.qiy.nl',
DEPLOY_DIR: '/var/www/html/public/dummy-jenkins-pl',
DEPLOY_CRED: 'bot-ci-dgm-rsa',
],
inLabels: [ 'ubuntu-16', 'centos-7' ],
onLabels: [ default: 'master', docker: 'docker', mac: 'mac', android: 'android', ],
noPoll: '(.+_.+)',
)
lazyStage {
name = 'validate'
tasks = [
[
pre: {
def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
},
run: {
echo "This is my first task"
},
],
[ run: { echo "This is my second task" }, in: '*', on: 'docker' ],
[ run: "third.sh -v", in: [ 'ubuntu-16', 'centos-7'], on: 'docker',],
[ run: [ "fourth-a.sh -v -m c", "fourth-b.sh -v -m d"], on: 'mac' ],
[ run: [ "fourth-a.sh -v -m e", "fourth-b.sh -v -m f"], on: 'android' ],
]
}
lazyStage {
name = 'test'
tasks = [
[ run: { echo "This is my fith task" }, ],
[
run: { echo "This is my sixth task" },
in: '*', on: 'docker'
],
[
run: [ 'junit.sh', 'jmeter.sh', ],
in: '*', on: 'docker',
post: { echo 'Archiving test reports' },
],
]
}
lazyStage {
name = 'package'
tasks = [
// [ run: { echo "Building packages"; echo "Archive packages"; }, on: 'windows', ]
// [ run: { echo "Building packages"; echo "Archive packages"; }, on: 'mac', ]
[
run: {
echo "Building packages";
def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
sh "mkdir -p ${env.BUILD_DIR} && echo 'testpkg-${currentVersione}' > ${env.BUILD_DIR}/\${LAZY_LABEL}.pkg"
},
in: '*',
on: 'docker',
post: {
echo "Archiving packages"
archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false)
},
],
]
}
// Release stage only only if criteria are met
lazyStage {
name = 'release'
onlyif = ( lazyConfig['branch'] == workingBranch && lazyConfig.env.RELEASE )
// Ask version if release flag and set and we are in the branch to fork release from
input = [
message: 'Version string',
parameters: [string(
defaultValue: '',
description: "Version to be release: 'build', 'micro', 'minor', 'major' or a specific string (i.e.: 1.2.3-4)",
name: 'VERSION'
)]
]
tasks = [
pre: {
gitAuth(env.GIT_CRED, {
// Switch to release branch to bump version
sh("git checkout ${releaseBranch}")
def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
// Define next version based on optional input
def nextVersion = null
if (env.lazyInput) {
if (env.lazyInput ==~ /[a-z]+/) {
nextVersion = bumpVersion(env.lazyInput, currentVersion)
} else {
nextVersion = env.lazyInput
}
} else {
nextVersion = bumpVersion('build', currentVersion)
}
// Merge changes from working branch into release branch
gitMerge(workingBranch, releaseBranch)
// Bump version and tag the release branch before deploying
sh("git checkout ${releaseBranch}")
writeFile(encoding: 'UTF-8', text: nextVersion, file: 'version.txt')
gitCommit("Update version to ${nextVersion}", 'version.txt')
})
unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
},
run: {
echo("Rebuild for new version ...")
},
post: {
def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
gitAuth(env.GIT_CRED, {
// Uncomment the following to merge version bump back into the working branch
//gitMerge(releaseBranch, workingBranch)
//gitPush(remote, workingBranch)
gitPush(remote, releaseBranch)
gitTag("${currentVersion}", remote)
})
archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false)
},
// Can not be done in parallel
//in: '*', on: 'docker',
]
}
lazyStage {
name = 'systemtest'
onlyif = ( env.LAZY_BRANCH == releaseBranch )
input = 'Deploy on systemtest?'
tasks = [
pre: {
def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
},
run: {
echo "Creating repo with packages"
},
in: '*',
on: 'docker',
post: {
echo "Publishing package repos"
sshagent(credentials: [env.DEPLOY_CRED]) {
sshDeploy(env.BUILD_DIR, "${env.DEPLOY_USER}@${env.DEPLOY_HOST_STST}", env.DEPLOY_DIR, 'rsync')
}
archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false)
},
]
}
lazyStage {
name = 'acceptance'
onlyif = ( lazyConfig['branch'] == releaseBranch )
input = 'Deploy to acceptance?'
tasks = [
pre: {
unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
},
run: {
sshagent(credentials: [env.DEPLOY_CRED]) {
sshDeploy(env.BUILD_DIR, "${env.DEPLOY_USER}@${env.DEPLOY_HOST_ACC1}", env.DEPLOY_DIR, 'rsync')
sshDeploy(env.BUILD_DIR, "${env.DEPLOY_USER}@${env.DEPLOY_HOST_ACC2}", env.DEPLOY_DIR, 'rsync')
}
},
in: '*',
on: 'docker',
]
}
lazyStage {
name = 'production'
onlyif = ( lazyConfig['branch'] == releaseBranch )
input = 'Deploy to production?'
tasks = [
pre: {
unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
},
run: {
sshagent(credentials: [env.DEPLOY_CRED]) {
sshDeploy(env.BUILD_DIR, "${env.DEPLOY_USER}@${env.DEPLOY_HOST_PRD1}", env.DEPLOY_DIR, 'rsync')
sshDeploy(env.BUILD_DIR, "${env.DEPLOY_USER}@${env.DEPLOY_HOST_PRD2}", env.DEPLOY_DIR, 'rsync')
}
},
in: '*',
on: 'docker',
]
}