Newer
Older
vueDummy / Jenkinsfile
@Benoit Donneaux Benoit Donneaux on 25 Oct 2022 7 KB [DPR-1361] Use last tag to bump version
#!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 = 'main'
def releaseBranch = 'stable'

// Initialize configuration
lazyConfig(
	name: 'vue-dummy',
	env: [
		RELEASE: true,
		DRYRUN: false,
		BUILD_DIR: 'dist',	// directory where the site be build
		GIT_CRED: 'bot-ci-dgm',
		DEPLOY_USER: 'root',
		DEPLOY_HOST_TST: 'bxtsvctwas001.in.dolden.net',
		DEPLOY_HOST_STS: 'bxtsvctwas002.in.dolden.net',
		DEPLOY_HOST_ACC: 'bxtsvctwas003.in.dolden.net',
		DEPLOY_HOST_PRD: 'bxtsvctwas004.in.dolden.net',
		DEPLOY_DIR: '/var/www/html/dummyvue',
		DEPLOY_CRED: 'bot-ci-dgm-rsa',
		BASE_URL: '/dummyvue/',
	],
	inLabels:   	[ 'centos7', ],
	onLabels:   	[ default: 'master', docker: 'docker', ],
	noIndex:		"(.+_.+)",	// Avoid automatic indexing for release and private branches
	xmppTargets:	'design@conference.qiy.nl',
)

// Validate by linting the code
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: { sh("npm run-script validate") },
		in: [ 'centos7'] , on: 'docker'
	]
}

// Test the code
lazyStage {
	name = 'test'
	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: { sh("npm run-script test") },
		in: [ 'centos7'] , on: 'docker'
	]
}

// Generate and package the site
lazyStage {
	name = 'generate'
	tasks = [
		pre: {
			// Read version from last git tag first
			def currentVersion = null
			gitAuth(env.GIT_CRED, {
				currentVersion = gitLastTag()
			})
			if (lazyConfig['branch'] != releaseBranch) { 
				// Write version from tag to generate the site
				jsVersion(currentVersion)
			}
			currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}"
		},
		run: { sh("npm run-script build") },
		post: {
			archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false)
		},
		in: [ 'centos7'],
		on: 'docker',
	]
}

// Deliver the site on each environment
lazyStage {
	name = 'test'
	onlyif = ( env.LAZY_BRANCH ==~ /^devel_.+/ )
	input = 'Deploy to test?'
	tasks = [
		pre: {
			unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
		},
		run: {
			sshagent(credentials: [env.DEPLOY_CRED]) {
			  env.DEPLOY_HOST_TST.split(',').each { host ->
					sshDeploy(
						srcUrl: "${env.BUILD_DIR}/",
						dstUrl: "${env.DEPLOY_USER}@${host}:${env.DEPLOY_DIR}/"
					)
				}
			}
		},
		on: 'linux',
	]
}

// 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 = [
		run: {
			gitAuth(env.GIT_CRED, {
				// Define next version based on optional input
				def currentVersion = gitLastTag()
				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
				jsVersion(nextVersion)
				gitCommit("Update version to ${nextVersion}", 'lazyDir/package.json')
				// 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: {
			sshagent(credentials: [env.DEPLOY_CRED]) {
			  env.DEPLOY_HOST_STS.split(',').each { host ->
					sshDeploy(
						srcUrl: "${env.BUILD_DIR}/",
						dstUrl: "${env.DEPLOY_USER}@${host}:${env.DEPLOY_DIR}/"
					)
				}
			}
		},
		on: 'linux',
	]
}

lazyStage {
	name = 'acceptance'
	onlyif = ( env.LAZY_BRANCH == releaseBranch )
	tasks = [
		pre: {
			unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
		},
		run: {
			sshagent(credentials: [env.DEPLOY_CRED]) {
			  env.DEPLOY_HOST_ACC.split(',').each { host ->
					sshDeploy(
						srcUrl: "${env.BUILD_DIR}/",
						dstUrl: "${env.DEPLOY_USER}@${host}:${env.DEPLOY_DIR}/"
					)
				}
			}
		},
		on: 'linux',
	]
}
/*
lazyStage {
	name = 'production'
	onlyif = ( env.LAZY_BRANCH == releaseBranch )
	input = 'Deploy to production?'
	tasks = [
		pre: {
			unarchive(mapping:["${env.BUILD_DIR}/" : '.'])
		},
		run: {
			sshagent(credentials: [env.DEPLOY_CRED]) {
			  env.DEPLOY_HOST_PRD.split(',').each { host ->
					sshDeploy(
						srcUrl: "${env.BUILD_DIR}/",
						dstUrl: "${env.DEPLOY_USER}@${host}:${env.DEPLOY_DIR}/"
					)
				}
			}
		},
		on: 'linux',
	]
}
*/