Jenkinspipeline Build Tools
Jenkinspipeline Build Tools
groovy
def releasedVersion
node('master') {
def dockerTool = tool name: 'docker', type:
'org.jenkinsci.plugins.docker.commons.tools.DockerTool'
withEnv(["DOCKER=${dockerTool}/bin"]) {
stage('Prepare') {
deleteDir()
parallel Checkout: {
checkout scm
}, 'Run Zalenium': {
dockerCmd '''run -d --name zalenium -p 4444:4444 \
-v /var/run/docker.sock:/var/run/docker.sock \
--network="host" \
--privileged dosel/zalenium:3.4.0a start --videoRecordingEnabled false
--chromeContainers 1 --firefoxContainers 0'''
}
}
stage('Build') {
withMaven(maven: 'Maven 3') {
dir('app') {
sh 'mvn clean package'
dockerCmd 'build --tag automatingguy/sparktodo:SNAPSHOT .'
}
}
}
stage('Deploy') {
stage('Deploy') {
dir('app') {
dockerCmd 'run -d -p 9999:9999 --name "snapshot" --network="host"
automatingguy/sparktodo:SNAPSHOT'
}
}
}
stage('Tests') {
try {
dir('tests/rest-assured') {
sh './gradlew clean test'
}
} finally {
junit testResults: 'tests/rest-assured/build/*.xml', allowEmptyResults:
true
archiveArtifacts 'tests/rest-assured/build/**'
}
try {
withMaven(maven: 'Maven 3') {
dir('tests/bobcat') {
sh 'mvn clean test -Dmaven.test.failure.ignore=true'
}
}
} finally {
junit testResults: 'tests/bobcat/target/*.xml', allowEmptyResults: true
archiveArtifacts 'tests/bobcat/target/**'
}
stage('Release') {
withMaven(maven: 'Maven 3') {
dir('app') {
releasedVersion = getReleasedVersion()
withCredentials([usernamePassword(credentialsId: 'github',
passwordVariable: 'password', usernameVariable: 'username')]) {
sh "git config user.email test@automatingguy.com && git config
user.name Jenkins"
sh "mvn release:prepare release:perform -Dusername=${username}
-Dpassword=${password}"
}
dockerCmd "build --tag automatingguy/sparktodo:$
{releasedVersion} ."
}
}
}
stage('Deploy @ Prod') {
dockerCmd "run -d -p 9999:9999 --name 'production' automatingguy/sparktodo:
${releasedVersion}"
}
}
}
def dockerCmd(args) {
sh "sudo ${DOCKER}/docker ${args}"
}
def getReleasedVersion() {
return (readFile('pom.xml') =~ '<version>(.+)-SNAPSHOT</version>')[0][1]
}
https://bulldogjob.com/readme/exploring-jenkins-pipelines-a-simple-delivery-flow
https://stackoverflow.com/questions/60263150/using-withenv-in-a-declarative-
pipeline
https://automatingguy.com/2017/11/06/jenkins-pipelines-simple-delivery-flow/