-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from halkeye/buildDockerImage
buildDockerImage()
- Loading branch information
Showing
12 changed files
with
202 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest | ||
import mock.CurrentBuild | ||
import mock.Infra | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.Ignore | ||
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString | ||
import static org.junit.Assert.assertTrue | ||
import static org.junit.Assert.assertFalse | ||
import static org.junit.Assert.assertEquals | ||
import static org.junit.Assert.assertNotNull | ||
|
||
class BuildDockerImageTests extends DeclarativePipelineTest { | ||
static final String scriptName = "vars/buildDockerImage.groovy" | ||
Map env = [:] | ||
|
||
@Override | ||
@Before | ||
void setUp() throws Exception { | ||
super.setUp() | ||
|
||
binding.setVariable('env', env) | ||
binding.setProperty('scm', new String()) | ||
binding.setProperty('mvnSettingsFile', 'settings.xml') | ||
binding.setProperty('infra', new Infra()) | ||
|
||
helper.registerAllowedMethod('ansiColor', [String.class], { s -> s }) | ||
|
||
helper.registerAllowedMethod('sh', [Map], {m-> | ||
if (m.returnStdout) { | ||
cmd = m.script | ||
// cmd.contains is helpful to filter sh call which should fail the pipeline | ||
if (cmd.contains("git log -n 1 --pretty=format:'%h'")) { | ||
return 'abc123' | ||
} | ||
if (cmd.contains('git remote show origin')) { | ||
return 'git@github.com:jenkins-infra/pipeline-library.git' | ||
} | ||
if (cmd.contains('date --rfc-3339=seconds')) { | ||
return '2020-05-25T07:11:16+00:00' | ||
} | ||
} | ||
}) | ||
} | ||
|
||
@Test | ||
@Ignore("until https://github.com/jenkinsci/JenkinsPipelineUnit/pull/220 is merged and released") | ||
void testCallsDockerBuild() throws Exception { | ||
helper.registerAllowedMethod("buildingTag", [], { false }) | ||
|
||
def script = loadScript(scriptName) | ||
script("jenkins-wiki-exporter") | ||
|
||
def calls = helper.callStack.findAll { call -> | ||
call.methodName == 'sh' | ||
}.findAll { call -> | ||
callArgsToString(call).contains('docker build') | ||
}.collect { call -> | ||
callArgsToString(call).replaceAll(/\s+/, ' ').trim() | ||
} | ||
//printCallStack() | ||
assertEquals(calls, ['docker build -t jenkins4eval/jenkins-wiki-exporter --build-arg "GIT_COMMIT_REV=abc123" --build-arg "GIT_SCM_URL=git@github.com:jenkins-infra/pipeline-library.git" --build-arg "BUILD_DATE=2020-05-25T07:11:16+00:00" --label "org.opencontainers.image.source=git@github.com:jenkins-infra/pipeline-library.git" --label "org.label-schema.vcs-url=git@github.com:jenkins-infra/pipeline-library.git" --label "org.opencontainers.image.url==https://github.com/jenkins-infra/pipeline-library.git" --label "org.label-schema.url=https://github.com/jenkins-infra/pipeline-library.git" --label "org.opencontainers.image.revision=abc123" --label "org.label-schema.vcs-ref=abc123" --label "org.opencontainers.created=2020-05-25T07:11:16+00:00" --label "org.label-schema.build-date=2020-05-25T07:11:16+00:00" .']) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
def call(String imageName, Map config=[:]) { | ||
if (!config.registry) { | ||
if (infra.isTrusted()) { | ||
config.registry = "jenkinsciinfra/" | ||
} else { | ||
config.registry = "jenkins4eval/" | ||
} | ||
} | ||
|
||
pipeline { | ||
agent { | ||
label 'docker&&linux' | ||
} | ||
|
||
options { | ||
disableConcurrentBuilds() | ||
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5')) | ||
timeout(time: 60, unit: "MINUTES") | ||
ansiColor("xterm") | ||
} | ||
|
||
stages { | ||
stage("Build") { | ||
steps { | ||
script { | ||
GIT_COMMIT_REV = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim() | ||
GIT_SCM_URL = sh(returnStdout: true, script: "git remote show origin | grep 'Fetch URL' | awk '{print \$3}'").trim() | ||
SCM_URI = GIT_SCM_URL.replace("git@github.com:", "https://github.com/") | ||
BUILD_DATE = sh(returnStdout: true, script: "TZ=UTC date --rfc-3339=seconds | sed 's/ /T/'").trim() | ||
} | ||
sh """ | ||
docker build \ | ||
-t ${config.registry}${imageName} \ | ||
--build-arg "GIT_COMMIT_REV=${GIT_COMMIT_REV}" \ | ||
--build-arg "GIT_SCM_URL=${GIT_SCM_URL}" \ | ||
--build-arg "BUILD_DATE=${BUILD_DATE}" \ | ||
--label "org.opencontainers.image.source=${GIT_SCM_URL}" \ | ||
--label "org.label-schema.vcs-url=${GIT_SCM_URL}" \ | ||
--label "org.opencontainers.image.url==${SCM_URI}" \ | ||
--label "org.label-schema.url=${SCM_URI}" \ | ||
--label "org.opencontainers.image.revision=${GIT_COMMIT_REV}" \ | ||
--label "org.label-schema.vcs-ref=${GIT_COMMIT_REV}" \ | ||
--label "org.opencontainers.created=${BUILD_DATE}" \ | ||
--label "org.label-schema.build-date=${BUILD_DATE}" \ | ||
. | ||
""" | ||
} | ||
} | ||
stage("Deploy master as latest") { | ||
when { branch "master" } | ||
steps { | ||
sh "docker tag ${config.registry}${imageName} ${config.registry}${imageName}:master" | ||
sh "docker tag ${config.registry}${imageName} ${config.registry}${imageName}:${GIT_COMMIT}" | ||
script { | ||
infra.withDockerCredentials { | ||
sh "docker push ${config.registry}${imageName}:master" | ||
sh "docker push ${config.registry}${imageName}:${GIT_COMMIT}" | ||
sh "docker push ${config.registry}${imageName}" | ||
} | ||
if (currentBuild.description) { | ||
currentBuild.description = currentBuild.description + " / " | ||
} | ||
currentBuild.description = "master / ${GIT_COMMIT}" | ||
} | ||
} | ||
} | ||
stage("Deploy tag as tag") { | ||
// semver regex from https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39 | ||
// when { tag pattern: "v([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?\$ ", comparator: "REGEXP"} | ||
// for now since testing only handles simple string, start with that | ||
when { tag "v*" } | ||
steps { | ||
sh "docker tag ${config.registry}${imageName} ${config.registry}${imageName}:${TAG_NAME}" | ||
script { | ||
infra.withDockerCredentials { | ||
sh "docker push ${config.registry}${imageName}:${TAG_NAME}" | ||
} | ||
if (currentBuild.description) { | ||
currentBuild.description = currentBuild.description + " / " | ||
} | ||
currentBuild.description = "${TAG_NAME}" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<p> | ||
Builds a docker image with some caveats | ||
if on trusted, use jenkinsciinfra dockerhub registry, otherwise use jenkins4eval | ||
|
||
</p> | ||
|
||
<!-- | ||
vim: ft=html | ||
--> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters