Skip to content

Commit

Permalink
[FIXED JENKINS-42693] Add additionalBuildArgs dockerfile parameter
Browse files Browse the repository at this point in the history
Allows specifying like
dockerfile {
  additionalBuildArgs "--build-arg someArg=someValue"
}
which will get added to the "docker build ..." command line call.
  • Loading branch information
abayer committed Mar 13, 2017
1 parent cc83a09 commit 04206b8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
Expand Up @@ -29,18 +29,16 @@
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.AbstractDockerAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class DockerPipelineFromDockerfile extends AbstractDockerAgent<DockerPipelineFromDockerfile> {
private String filename;
private String dir;
private String additionalBuildArgs;

@DataBoundConstructor
public DockerPipelineFromDockerfile() {
Expand All @@ -64,6 +62,15 @@ public void setDir(String dir) {
this.dir = dir;
}

public String getAdditionalBuildArgs() {
return additionalBuildArgs;
}

@DataBoundSetter
public void setAdditionalBuildArgs(String additionalBuildArgs) {
this.additionalBuildArgs = additionalBuildArgs;
}

@Nonnull
public String getActualDir() {
if (!StringUtils.isEmpty(dir)) {
Expand Down
Expand Up @@ -77,7 +77,8 @@ public class DockerPipelineFromDockerfileScript extends AbstractDockerPipelineSc
try {
def hash = Utils.stringToSHA1(script.readFile("${describable.getDockerfilePath()}"))
def imgName = "${hash}"
script.sh "docker build -t ${imgName} -f \"${describable.getDockerfilePath()}\" \"${describable.getActualDir()}\""
def additionalBuildArgs = describable.getAdditionalBuildArgs() ? " ${describable.additionalBuildArgs}" : ""
script.sh "docker build -t ${imgName}${additionalBuildArgs} -f \"${describable.getDockerfilePath()}\" \"${describable.getActualDir()}\""
script.dockerFingerprintFrom dockerfile: describable.dockerfilePath, image: imgName, toolName: script.env.DOCKER_TOOL_NAME
return script.getProperty("docker").image(imgName)
} catch (FileNotFoundException f) {
Expand Down
Expand Up @@ -198,6 +198,26 @@ public void fromDockerfile() throws Exception {
.go();
}

@Test
public void additionalDockerBuildArgs() throws Exception {
assumeDocker();
// Bind mounting /var on OS X doesn't work at the moment
onAllowedOS(PossibleOS.LINUX);

sampleRepo.write("Dockerfile", "FROM ubuntu:14.04\n\nARG someArg=thisArgHere\n\nRUN echo $someArg > /hi-there\n\n");
sampleRepo.git("init");
sampleRepo.git("add", "Dockerfile");
sampleRepo.git("commit", "--message=Dockerfile");

expect("fromDockerfile")
.logContains("[Pipeline] { (foo)",
"The answer is 42",
"-v /tmp:/tmp -p 8000:8000",
"thisOtherArg")
.logNotContains("thisArgHere")
.go();
}

@Issue("JENKINS-41668")
@Test
public void fromDockerfileInOtherDir() throws Exception {
Expand Down
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
agent {
dockerfile {
args "-v /tmp:/tmp -p 8000:8000"
additionalBuildArgs "--build-arg someArg=thisOtherArg"
}
}
stages {
stage("foo") {
steps {
sh 'cat /hi-there'
sh 'echo "The answer is 42"'
}
}
}
}



0 comments on commit 04206b8

Please sign in to comment.