-
-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buildDockerImage() #147
buildDockerImage() #147
Changes from all commits
d41d7f3
d344343
150f23d
5f796c9
87a3473
b2e941d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" .']) | ||
} | ||
} |
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a master tag needed as well as latest? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, latest really could be anything, while master is master branch But yea, i donno, i mostly copy and pasted it from my own shared pipeline which had this, but I'm not sure i ever used anything other than latest and tag |
||
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}" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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 | ||
--> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this already added by declarative pipeline or is it too long?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/shrug
is it? its not super googleable