Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
node {
// https://registry.hub.docker.com/_/maven/
def maven32 = docker.image('maven:3.2-jdk-7-onbuild');
stage 'Mirror'
// First make sure the slave has this image.
// (If you could set your registry below to mirror Docker Hub,
// this would be unnecessary as maven32.inside would pull the image.)
maven32.pull()
// We are pushing to a private secure docker registry in this demo.
// 'docker-registry-login' is the username/password credentials ID
// as defined in Jenkins Credentials.
// This is used to authenticate the docker client to the registry.
docker.withRegistry('https://docker.example.com/', 'docker-registry-login') {
stage 'Build'
// spin up a Maven container to build the petclinic app from source
// (we are only using -v here to share the Maven local repository across demo runs;
// otherwise would set localRepository=${pwd}/m2repo)
maven32.inside('-v /m2repo:/m2repo') {
// Check out the source code.
git 'https://github.com/tfennelly/spring-petclinic.git'
// Set up a shared Maven repo so we don't need to download all dependencies on every build.
writeFile file: 'settings.xml',
text: '<settings><localRepository>/m2repo</localRepository></settings>'
// Build with Maven settings.xml file that specs the local Maven repo.
sh 'mvn -B -s settings.xml clean install -DskipTests'
// The app .war and Dockerfile are now available in the workspace. See below.
}
stage 'Bake Docker image'
// use the spring-petclinic Dockerfile (see above 'maven32.inside()' block)
// to build container that can run the app. The Dockerfile is in the cwd of the active workspace
// (see above maven32.inside() block), so we pass '.' as the build PATH param. The Dockerfile
// (see https://github.com/tfennelly/spring-petclinic/blob/master/Dockerfile) expects the petclinic.war
// file to be in the 'target' dir of the workspace, which will be the case.
def pcImg = docker.build("examplecorp/spring-petclinic:${env.BUILD_TAG}", '.')
// Let's tag and push the newly built image. Will tag using the image name provided
// in the 'docker.build' call above (which included the build number on the tag).
pcImg.push();
stage 'Test Image'
// Run the petclinic app in its own docker container
pcImg.withRun {petclinic ->
// Spin up a maven test container, linking it to the petclinic app container allowing
// the maven tests to fire HTTP requests between the containers.
maven32.inside("-v /m2repo:/m2repo --link=${petclinic.id}:petclinic") {
git 'https://github.com/tfennelly/spring-petclinic-tests.git'
writeFile file: 'settings.xml',
text: '<settings><localRepository>/m2repo</localRepository></settings>'
sh 'mvn -B -s settings.xml clean package'
}
}
stage name: 'Promote Image', concurrency: 1
// All the tests passed. We can now retag and push the 'latest' image
pcImg.push('latest');
}
}
@IronicBadger

This comment has been minimized.

Copy link

commented Jul 20, 2015

thanks for this, extremely helpful on a subject with little docs.
how do you handle the masses of images this creates under docker images ?

@yogeshmsharma

This comment has been minimized.

Copy link

commented Dec 8, 2015

I am not able to checkout using svn, how do I provide credential

@dashaun

This comment has been minimized.

Copy link

commented Dec 23, 2016

Thank you for the kickstart!

@thismatters

This comment has been minimized.

Copy link

commented Aug 5, 2019

I am not able to checkout using svn, how do I provide credential

You have to provide credentials through Jenkins. https://emilwypych.com/2017/05/28/how-to-create-credentials-in-jenkins/?cn-reloaded=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.