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://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 = 'devel_bear'
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: 'bxtsvctwas002.boxtel',
		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:		'(.+_.+|release)',
)

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";
				sh "mkdir -p ${env.BUILD_DIR} && echo 'testpkg' > ${env.BUILD_DIR}/\${LAZY_LABEL}.pkg"
			},
			in: [ 'ubuntu-16', 'centos-7', ],
			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 string to be use for the next release', name: 'VERSION')]
	]
	tasks = [
		pre: {
			def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
			gitAuth(env.GIT_CRED, {
				// Define next version based on optional input
				// TODO: impelement/import a generic helper for semantic versionning
				def nextVersion = 'new'
				if (env.lazyInput) {
					nextVersion = env.lazyInput
				}
				// Merge master 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')
			})
			currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
			currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
			unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
		},
		run: {
			def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
			// Rebuild the site with the new version
			echo("Rebuild for version ${currentVersion}")
		},
		post: {
			def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt')
			gitAuth(env.GIT_CRED, {
				gitTag("${currentVersion}", remote)
				gitPush(remote, releaseBranch)
			})
			archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false)
		},
		in: [ 'ubuntu-16', 'centos-7', ], on: 'docker',
	]
}

lazyStage {
	name = 'publish'
	onlyif = ( env.LAZY_BRANCH == 'master' || lazyConfig.env.RELEASE )
	input = 'Publish?'
	tasks = [
		[
			pre: {
				unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
			},
			run: {
				echo "Creating repo with packages"
			},
			in: [ 'ubuntu-16', 'centos-7', ],
			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)
				}
			},
		],
	]
}