-
Notifications
You must be signed in to change notification settings - Fork 408
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 #126 from marceldiass/improve-from-args
Add ARG Dockerfile values to the buildArgs collection
- Loading branch information
Showing
9 changed files
with
353 additions
and
72 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
101 changes: 101 additions & 0 deletions
101
src/main/java/org/jenkinsci/plugins/docker/workflow/Dockerfile.java
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,101 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015, 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. | ||
*/ | ||
package org.jenkinsci.plugins.docker.workflow; | ||
|
||
import hudson.FilePath; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
|
||
public final class Dockerfile { | ||
|
||
private static final String ARG = "ARG"; | ||
private static final String FROM = "FROM "; | ||
|
||
private FilePath dockerfilePath; | ||
private LinkedList<String> froms; | ||
private Map<String,String> args; | ||
|
||
@DataBoundConstructor public Dockerfile(FilePath dockerfilePath) throws IOException, InterruptedException { | ||
this.dockerfilePath = dockerfilePath; | ||
this.froms = new LinkedList<>(); | ||
this.args = new HashMap<>(); | ||
parse(); | ||
} | ||
|
||
public LinkedList<String> getFroms() { | ||
return froms; | ||
} | ||
|
||
public Map<String, String> getArgs() { | ||
return args; | ||
} | ||
|
||
private void parse() throws IOException, InterruptedException { | ||
InputStream is = dockerfilePath.read(); | ||
try { | ||
// encoding probably irrelevant since image/tag names must be ASCII | ||
BufferedReader r = new BufferedReader(new InputStreamReader(is, "ISO-8859-1")); | ||
try { | ||
String line; | ||
while ((line = r.readLine()) != null) { | ||
line = line.trim(); | ||
if (line.startsWith("#")) { | ||
continue; | ||
} | ||
if (line.startsWith(ARG)) { | ||
String[] keyVal = parseDockerfileArg(line.substring(4)); | ||
args.put(keyVal[0], keyVal[1]); | ||
continue; | ||
} | ||
|
||
if (line.startsWith(FROM)) { | ||
froms.add(line.substring(5)); | ||
continue; | ||
} | ||
} | ||
} finally { | ||
r.close(); | ||
} | ||
} finally { | ||
is.close(); | ||
} | ||
} | ||
|
||
protected String[] parseDockerfileArg(String argLine) { | ||
String[] keyValue = argLine.split("=", 2); | ||
String key = keyValue[0]; | ||
String value = ""; | ||
if (keyValue.length > 1) { | ||
value = keyValue[1]; | ||
} | ||
return new String[]{key, value}; | ||
} | ||
} |
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
122 changes: 122 additions & 0 deletions
122
src/test/java/org/jenkinsci/plugins/docker/workflow/DockerUtilsTest.java
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,122 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015, 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. | ||
*/ | ||
package org.jenkinsci.plugins.docker.workflow; | ||
|
||
import hudson.FilePath; | ||
import org.hamcrest.collection.IsCollectionWithSize; | ||
import org.hamcrest.core.IsCollectionContaining; | ||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class DockerUtilsTest { | ||
|
||
@Rule public final ExpectedException exception = ExpectedException.none(); | ||
|
||
@Test public void parseBuildArgs() throws IOException, InterruptedException { | ||
|
||
FilePath dockerfilePath = new FilePath(new File("src/test/resources/Dockerfile-withArgs")); | ||
Dockerfile dockerfile = new Dockerfile(dockerfilePath); | ||
|
||
final String imageToUpdate = "hello-world:latest"; | ||
final String key = "IMAGE_TO_UPDATE"; | ||
final String commangLine = "docker build -t hello-world --build-arg "+key+"="+imageToUpdate; | ||
Map<String, String> buildArgs = DockerUtils.parseBuildArgs(dockerfile, commangLine); | ||
|
||
Assert.assertThat(buildArgs.keySet(), IsCollectionWithSize.hasSize(1)); | ||
Assert.assertThat(buildArgs.keySet(), IsCollectionContaining.hasItems(key)); | ||
Assert.assertThat(buildArgs.get(key), IsEqual.equalTo(imageToUpdate)); | ||
} | ||
|
||
@Test public void parseBuildArgsWithDefaults() throws IOException, InterruptedException { | ||
|
||
Dockerfile dockerfile = getDockerfileDefaultArgs(); | ||
|
||
final String registry = ""; | ||
final String key_registry = "REGISTRY_URL"; | ||
final String key_tag = "TAG"; | ||
final String commangLine = "docker build -t hello-world"; | ||
Map<String, String> buildArgs = DockerUtils.parseBuildArgs(dockerfile, commangLine); | ||
|
||
Assert.assertThat(buildArgs.keySet(), IsCollectionWithSize.hasSize(2)); | ||
Assert.assertThat(buildArgs.keySet(), IsCollectionContaining.hasItems(key_registry, key_tag)); | ||
Assert.assertThat(buildArgs.get(key_registry), IsEqual.equalTo(registry)); | ||
Assert.assertThat(buildArgs.get(key_tag), IsEqual.equalTo("latest")); | ||
} | ||
|
||
@Test public void parseBuildArgsOverridingDefaults() throws IOException, InterruptedException { | ||
|
||
Dockerfile dockerfile = getDockerfileDefaultArgs(); | ||
|
||
final String registry = "http://private.registry:5000/"; | ||
final String key_registry = "REGISTRY_URL"; | ||
final String key_tag = "TAG"; | ||
final String tag = "1.2.3"; | ||
final String commangLine = "docker build -t hello-world --build-arg "+key_tag+"="+tag+ | ||
" --build-arg "+key_registry+"="+registry; | ||
Map<String, String> buildArgs = DockerUtils.parseBuildArgs(dockerfile, commangLine); | ||
|
||
Assert.assertThat(buildArgs.keySet(), IsCollectionWithSize.hasSize(2)); | ||
Assert.assertThat(buildArgs.keySet(), IsCollectionContaining.hasItems(key_registry, key_tag)); | ||
Assert.assertThat(buildArgs.get(key_registry), IsEqual.equalTo(registry)); | ||
Assert.assertThat(buildArgs.get(key_tag), IsEqual.equalTo(tag)); | ||
} | ||
|
||
@Test public void parseBuildArgWithKeyAndEqual() throws IOException, InterruptedException { | ||
final String commangLine = "docker build -t hello-world --build-arg key="; | ||
|
||
Map<String, String> buildArgs = DockerUtils.parseBuildArgs(null, commangLine); | ||
|
||
Assert.assertThat(buildArgs.keySet(), IsCollectionWithSize.hasSize(1)); | ||
Assert.assertThat(buildArgs.keySet(), IsCollectionContaining.hasItems("key")); | ||
Assert.assertThat(buildArgs.get("key"), IsEqual.equalTo("")); | ||
} | ||
|
||
@Test public void parseInvalidBuildArg() throws IOException, InterruptedException { | ||
final String commangLine = "docker build -t hello-world --build-arg"; | ||
|
||
exception.expect(IllegalArgumentException.class); | ||
DockerUtils.parseBuildArgs(null, commangLine); | ||
} | ||
|
||
@Test public void parseInvalidBuildArgWithKeyOnly() throws IOException, InterruptedException { | ||
final String commangLine = "docker build -t hello-world --build-arg key"; | ||
|
||
exception.expect(IllegalArgumentException.class); | ||
DockerUtils.parseBuildArgs(null, commangLine); | ||
} | ||
|
||
private Dockerfile getDockerfileDefaultArgs() throws IOException, InterruptedException { | ||
FilePath dockerfilePath = new FilePath(new File("src/test/resources/Dockerfile-defaultArgs")); | ||
return new Dockerfile(dockerfilePath); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.