Exercise 2 - Solution
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'com.palantir.docker' version '0.22.1'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
apply plugin: 'com.palantir.docker'
apply from: 'gradle/version.gradle'
group = 'ch.hsr.projectautomation'
version = versionFromGit
sourceCompatibility = '1.8'
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compile 'org.apache.commons:commons-lang3:3.0.1'
compile 'commons-io:commons-io:2.4'
compile 'io.springfox:springfox-swagger2:2.8.0'
compile 'io.springfox:springfox-swagger-ui:2.8.0'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.assertj:assertj-core:3.11.1'
}
bootJar.doLast {
// Copy to a version free artifact for easier reference of last build result for testing purpose
copy {
from bootJar
into 'build/libs'
rename('project-automation.*.jar', 'project-automation-chatbot.jar')
}
}
// General config for docker image publishing
def dockerUserName = "exercise4automation"
def dockerAppName = "project-automation"
def dockerImageName = "${dockerUserName}/${dockerAppName}:${version}"
// Option A with Gradle Tasks:
task dockerBuildAsTask {
doLast() {
println "Build Docker: ${dockerImageName}"
exec {
commandLine 'docker', 'build', '-t', dockerImageName, '.'
}
}
}
task dockerPublishAsTask {
doLast() {
println "Publish Docker: ${dockerImageName}"
exec {
commandLine 'docker', 'push', dockerImageName
}
}
}
// Option B - with Plugin:
docker {
name dockerImageName
files bootJar.archiveFile
copySpec.from("build/libs").into("build/libs")
}
gradle/version.gradle
def gitDescribe() {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--always'
standardOutput = stdout
}
return stdout.toString().trim()
}
def gitBranch() {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'symbolic-ref', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
def getVersionFromGit() {
def version = gitBranch().replaceAll("\\/", "-") + gitDescribe()
println "Version: ${version}"
return version
}
ext.versionFromGit = getVersionFromGit()
Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh './gradlew clean build -x test -i -s --no-daemon'
}
}
stage('Test') {
steps {
sh './gradlew test -i -s --no-daemon'
}
post {
always {
// JUnit Test Reports
junit keepLongStdio: true, testResults: 'build/test-results/**/*.xml'
// JaCoCo Test Coverage Report
jacoco()
}
}
}
stage('Package') {
steps {
sh "./gradlew docker -i -s --no-daemon"
}
}
stage('Publish') {
steps {
withCredentials([usernamePassword(
credentialsId: 'dockerhub-exercise4automation',
passwordVariable: 'dockerUsername',
usernameVariable: 'dockerPassword')]) {
sh "docker login --username='${dockerUsername}' --password='${dockerPassword}'"
sh "./gradlew dockerPush -i -s --no-daemon"
}
}
}
}
}
Parametrized Release Build
Here is the final Jenkinsfile
including parametrized pipeline for tagging a relesse:
pipeline {
agent any
parameters {
string(name: 'releaseVersion',
defaultValue: '',
description: 'version number that will be used for the release and for tagging it in git repo.')
}
stages {
stage('Create Release Tag') {
when {
expression {
return params.releaseVersion != "";
}
}
steps {
echo "Tag for the release: ${params.releaseVersion}"
// The folloiwng is only needed cause our jenkins is not optimaly configured yet
sh "git config --global user.email '[email protected]'"
sh "git config --global user.name 'Project Automation CI Server'"
// create the tag
sh "git tag -a ${params.releaseVersion} -m 'Release Version ${params.releaseVersion}'"
}
}
stage('Build') {
steps {
sh './gradlew clean build -x test -i -s --no-daemon'
}
}
stage('Test') {
steps {
sh './gradlew test -i -s --no-daemon'
}
post {
always {
// JUnit Test Reports
junit keepLongStdio: true, testResults: 'build/test-results/**/*.xml'
// JaCoCo Test Coverage Report
jacoco()
}
}
}
stage('Package') {
steps {
sh "./gradlew docker -i -s --no-daemon"
}
}
stage('Publish') {
steps {
withCredentials([usernamePassword(
credentialsId: 'dockerhub-exercise4automation',
usernameVariable: 'dockerUsername',
passwordVariable: 'dockerPassword')]) {
sh "docker login --username='${dockerUsername}' --password='${dockerPassword}'"
sh "./gradlew dockerPush -i -s --no-daemon"
}
}
}
stage('Push Release Tag') {
when {
expression {
return params.releaseVersion != "";
}
}
steps {
echo "Push release tag: ${params.releaseVersion}"
// currently there is a credential issue here with this step on our jenkins
// sh "git push --tags"
}
}
}
}