Newer
Older
DummyJnkPL / Jenkinsfile
#!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://github.com/digital-me/jenkins-lib-lazy.git',
	branch:			'stable',
	credentialsId:	null,
]

library(
	identifier: "libLazy@${libLazy.branch}",
	retriever: modernSCM([
		$class:			'GitSCMSource',
		remote:			libLazy.remote,
		credentialsId:	libLazy.credentialsId
	]),
	changelog: false,
)

// Load Jenkins shared libraries to customize this project
def libCustom = [
	remote:			'ssh://git@code.in.digital-me.nl:2222/DEVops/JenkinsLibCustom.git',
	branch:			'stable',
	credentialsId:	'bot-ci-dgm-rsa',
]

library(
	identifier: "libCustom@${libCustom.branch}",
	retriever: modernSCM([
		$class:			'GitSCMSource',
		remote:			libCustom.remote,
		credentialsId:	libCustom.credentialsId
	]),
	changelog: false,
)

// 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', ],
	noIndex:	"(${releaseBranch}|.+_.+)",	// Avoid automatic indexing for release and private branches
)

lazyStage {
	name = 'validate'
	onlyif = ( lazyConfig['branch'] != releaseBranch ) // Skip when releasing
	tasks = [
		[
			pre: {
				def currentVersion = null
				gitAuth(env.GIT_CRED, {
					currentVersion = gitLastTag()
				})
				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'
	onlyif = ( lazyConfig['branch'] != releaseBranch ) // Skip when releasing
	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' },
		],
	]
}

// Generate packages
lazyStage {
	name = 'package'
	tasks = [
//		[ run: { echo "Building packages"; echo "Archive packages"; }, on: 'windows', ]
//		[ run: { echo "Building packages"; echo "Archive packages"; }, on: 'mac', ]
		[
			pre: {
				// Read version from last git tag first
				def currentVersion = null
				gitAuth(env.GIT_CRED, {
					currentVersion = gitLastTag()
				})
				if (lazyConfig['branch'] != releaseBranch) {
					// Write version from last tag to generate the site
					writeFile(encoding: 'UTF-8', file: 'version.txt', text: currentVersion)
				}
				currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
			},
			run: {
				// Update package metadata to the current version
				def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
				sh "mkdir -p ${env.BUILD_DIR} && echo 'testpkg-${currentVersion}' > ${env.BUILD_DIR}/\${LAZY_LABEL}.pkg"
			},
			post: {
				archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false)
			},
			in: '*',
			on: 'docker',
		],
	]
}

// Release stage 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' (default), 'micro', 'minor', 'major' or a specific string (i.e.: 1.2.3-4)",
			name: 'VERSION'
		)]
	]
	tasks = [
		run: {
			gitAuth(env.GIT_CRED, {
				// Define next version based on optional input
				def currentVersion = readFile(encoding: 'UTF8', file: 'version.txt')
				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 into release branch
				gitMerge(workingBranch, releaseBranch)
				// Bump version into release branch
				writeFile(encoding: 'UTF-8', file: 'version.txt', text: nextVersion)
				gitCommit("Update version to ${nextVersion}", 'version.txt')
				// Uncomment the following to merge version bump back into the working branch
				//gitMerge(releaseBranch, workingBranch)
				// Tag and publish changes in release branch
				gitTag("${nextVersion}")
				gitPush(remote, "${releaseBranch} ${nextVersion}")
				// Update the displayed version for this build
				currentVersion = gitLastTag()
				currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
			})
		},
		// Can not be done in parallel
	]
}

// Deliver the site on each environment
lazyStage {
	name = 'systemtest'
	onlyif = ( env.LAZY_BRANCH == releaseBranch )
	tasks = [
		pre: {
			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', false, '-hrlpgolzciu')
			}
			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', false, '-hrlpgolzciu')
				sshDeploy(env.BUILD_DIR, "${env.DEPLOY_USER}@${env.DEPLOY_HOST_ACC2}", env.DEPLOY_DIR, 'rsync', false, '-hrlpgolzciu')
			}
		},
		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',
	]
}