Permalink
Switch branches/tags
Nothing to show
Find file Copy path
66efd61 Jul 27, 2017
1 contributor

Users who have contributed to this file

74 lines (62 sloc) 2.42 KB
{ -> docker.image('slave-sbt').inside
{
stage('Build') {
sh sbt('compile')
}
stage('Test') {
sh sbt('test')
}
stage('Deploy Locally') {
sh sbt('docker:publishLocal')
def dockerTag = (sh(script: "git describe --tags HEAD", returnStdout: true)).trim()
cleanUpOldContainers(dockerTag)
runContainer(dockerTag)
def containerUrl = getRunningContainerURL(dockerTag)
echo "Running on $containerUrl"
currentBuild.setDescription("$containerUrl")
}
stage('Push to GitHub') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'github',
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh "git branch --list ${env.BRANCH_NAME} || git checkout -b ${env.BRANCH_NAME}"
sh "git push -f https://$USERNAME:$PASSWORD@github.com/NET-A-PORTER/jenkinsworld-2017-sample-app HEAD:refs/heads/${env.BRANCH_NAME}"
}
}
}
}
def sbt(target) {
return "sbt -no-colors $target"
}
def containerName() {
return "play-scala-starter-example-" + env.BRANCH_NAME.replaceAll("/","-")
}
def cleanUpOldContainers(dockerTag) {
try {
def name = containerName()
def containers = sh(script: "docker ps -q --filter name=$name", returnStdout: true).trim()
def containersArgs = containers.replaceAll("\n"," ").trim()
if (!containersArgs.isEmpty()) {
sh "docker rm -f $containersArgs"
}
} catch (Exception e) {
echo "${e.toString()}"
echo "Docker container $dockerTag did not exist => error ignored"
}
}
def runContainer(dockerTag) {
def name = containerName()
sh "docker run -d -P -ti --name $name-$dockerTag play-scala-starter-example:$dockerTag"
}
def getRunningContainerURL(dockerTag) {
def name = containerName()
def portInfo = sh(script: "docker port $name-$dockerTag", returnStdout: true)
def port = portInfo.split(":")[1]
def baseUrl = env.JENKINS_URL.endsWith("/") ? env.JENKINS_URL[0..-2] : env.JENKINS_URL
return """
<a href='$baseUrl:$port/' target='_blank'>
Running in
<img src='https://playframework.com/assets/images/logos/play_full_color.png'
style='height: 16px; width: 30px;'></img>
</a>"""
}