From 77d8613b51ab3c906d2b467a562d8061f5b6a35f Mon Sep 17 00:00:00 2001 From: "Ramanathan.M" Date: Tue, 20 Feb 2018 20:35:35 +0530 Subject: [PATCH 1/4] fetch port number of the running container and port on the host --- .../dockercontainerPort.groovy | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 declarative-examples/simple-examples/dockercontainerPort.groovy diff --git a/declarative-examples/simple-examples/dockercontainerPort.groovy b/declarative-examples/simple-examples/dockercontainerPort.groovy new file mode 100644 index 0000000..40b4bd3 --- /dev/null +++ b/declarative-examples/simple-examples/dockercontainerPort.groovy @@ -0,0 +1,60 @@ +pipeline { + agent any + + options { + timestamps() + } + + environment { + IMAGE = "custom-tutum" + } + + stages { + stage('prep') { + steps { + script { + env.GIT_HASH = sh( + script: "git show --oneline | head -1 | cut -d' ' -f1", + returnStdout: true + ).trim() + } + } + } + stage('build') { + steps { + script { + image = docker.build("${IMAGE}") + println "Newly generated image, " + image.id + } + } + } + stage('test') { + steps { + script { + // https://hub.docker.com/r/tutum/hello-world/ + def container = image.run('-p 80') + def contport = container.port(80) + def resp = sh(returnStdout: true, + script: """ + set -x + curl -w "%{http_code}" -o /dev/null -s \ + http://\"${contport}\" + """ + ).trim() + if ( resp == "200" ) { + println "tutum hello world is alive and kicking!" + currentBuild.result = "SUCCESS" + } else { + println "Humans are mortals." + currentBuild.result = "FAILURE" + } + } + } + } + } + post { + always { + cleanWs() + } + } +} From fcff93c1f162fb64f60c686c0e2b908502305c9a Mon Sep 17 00:00:00 2001 From: "Ramanathan.M" Date: Thu, 5 Apr 2018 05:59:34 +0530 Subject: [PATCH 2/4] purge redundant stage --- .../simple-examples/dockercontainerPort.groovy | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/declarative-examples/simple-examples/dockercontainerPort.groovy b/declarative-examples/simple-examples/dockercontainerPort.groovy index 40b4bd3..5391de2 100644 --- a/declarative-examples/simple-examples/dockercontainerPort.groovy +++ b/declarative-examples/simple-examples/dockercontainerPort.groovy @@ -10,16 +10,6 @@ pipeline { } stages { - stage('prep') { - steps { - script { - env.GIT_HASH = sh( - script: "git show --oneline | head -1 | cut -d' ' -f1", - returnStdout: true - ).trim() - } - } - } stage('build') { steps { script { From 16beaa0089fa296f9de077eb20f11ac726a5a2f6 Mon Sep 17 00:00:00 2001 From: "Ramanathan.M" Date: Mon, 9 Apr 2018 20:17:09 +0530 Subject: [PATCH 3/4] code snippet trimmed to just bare minimum --- .../simple-examples/dockercontainerPort.groovy | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/declarative-examples/simple-examples/dockercontainerPort.groovy b/declarative-examples/simple-examples/dockercontainerPort.groovy index 5391de2..660458b 100644 --- a/declarative-examples/simple-examples/dockercontainerPort.groovy +++ b/declarative-examples/simple-examples/dockercontainerPort.groovy @@ -1,14 +1,11 @@ pipeline { - agent any - - options { - timestamps() - } environment { IMAGE = "custom-tutum" } + agent 'node-where-docker-commands-can-be-run' + stages { stage('build') { steps { @@ -42,9 +39,4 @@ pipeline { } } } - post { - always { - cleanWs() - } - } } From ee3fdf235a27e46459e39dbfa170d135788ded40 Mon Sep 17 00:00:00 2001 From: "Ramanathan.M" Date: Mon, 9 Apr 2018 20:58:48 +0530 Subject: [PATCH 4/4] code trimmed and, cleaned off extra file from the previous commit --- .../simple-examples/dockercontainerPort.groovy | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/declarative-examples/simple-examples/dockercontainerPort.groovy b/declarative-examples/simple-examples/dockercontainerPort.groovy index 660458b..a290729 100644 --- a/declarative-examples/simple-examples/dockercontainerPort.groovy +++ b/declarative-examples/simple-examples/dockercontainerPort.groovy @@ -1,15 +1,21 @@ pipeline { - environment { - IMAGE = "custom-tutum" + IMAGE = "docker-repository/custom-tutum" } - agent 'node-where-docker-commands-can-be-run' + // build agent where docker commands can be run + agent 'docker-node' stages { stage('build') { steps { script { + // Dockerfile should be stored at the dir level same as that of Jenkinsfile + // Contents of Dockerfile, + /* + FROM tutum/hello-world + EXPOSE 80 + */ image = docker.build("${IMAGE}") println "Newly generated image, " + image.id } @@ -19,6 +25,7 @@ pipeline { steps { script { // https://hub.docker.com/r/tutum/hello-world/ + // build agent should have 'curl' installed. def container = image.run('-p 80') def contport = container.port(80) def resp = sh(returnStdout: true, @@ -29,10 +36,10 @@ pipeline { """ ).trim() if ( resp == "200" ) { - println "tutum hello world is alive and kicking!" + println "tutum hello world app is alive and kicking!" currentBuild.result = "SUCCESS" } else { - println "Humans are mortals." + println "tutum hello world app isn't reachable!!!" currentBuild.result = "FAILURE" } }