Skip to content
This repository was archived by the owner on Feb 1, 2018. It is now read-only.

Commit 11c0554

Browse files
committed
Add support for environment variable substition for ADD and COPY resources
Supports the case where the name of the file is supposed to be resolved using an environment variable. ENV VAR sub ADD files/$VAR.dat /$VAR.dat ADD files/${VAR}.dat /${VAR}.dat In both cases the VAR is substituted for sub. This allows the tar file creation process to capture the real source file.
1 parent d47eaf6 commit 11c0554

1 file changed

Lines changed: 62 additions & 5 deletions

File tree

src/main/java/com/github/dockerjava/client/command/BuildImgCmd.java

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import java.net.URI;
77
import java.net.URISyntaxException;
88
import java.util.ArrayList;
9+
import java.util.HashMap;
910
import java.util.List;
11+
import java.util.Map;
1012
import java.util.UUID;
1113
import java.util.regex.Matcher;
1214
import java.util.regex.Pattern;
@@ -35,7 +37,9 @@ public class BuildImgCmd extends AbstrDockerCmd<BuildImgCmd, ClientResponse> {
3537

3638
private static final Logger LOGGER = LoggerFactory.getLogger(BuildImgCmd.class);
3739

38-
private static final Pattern ADD_PATTERN = Pattern.compile("^ADD\\s+(.*)\\s+(.*)$");
40+
private static final Pattern ADD_OR_COPY_PATTERN = Pattern.compile("^(ADD|COPY)\\s+(.*)\\s+(.*)$");
41+
42+
private static final Pattern ENV_PATTERN = Pattern.compile("^ENV\\s+(.*)\\s+(.*)$");
3943

4044
private File dockerFolder = null;
4145
private InputStream tarInputStream = null;
@@ -133,14 +137,40 @@ protected File buildDockerFolderTar() {
133137
List<File> filesToAdd = new ArrayList<File>();
134138
filesToAdd.add(dockerFile);
135139

140+
Map<String, String>environmentMap = new HashMap<String, String>();
141+
142+
int lineNumber = 0;
143+
136144
for (String cmd : dockerFileContent) {
137-
final Matcher matcher = ADD_PATTERN.matcher(cmd.trim());
145+
146+
lineNumber++;
147+
148+
if (cmd.trim().isEmpty() || cmd.startsWith("#"))
149+
continue; // skip emtpy and commend lines
150+
151+
final Matcher envMatcher = ENV_PATTERN.matcher(cmd.trim());
152+
153+
if (envMatcher.find()) {
154+
if (envMatcher.groupCount() != 2)
155+
throw new DockerException(String.format("Wrong ENV format on line [%d]", lineNumber));
156+
157+
String variable = envMatcher.group(1).trim();
158+
159+
String value = envMatcher.group(2).trim();
160+
161+
environmentMap.put(variable, value);
162+
}
163+
164+
165+
final Matcher matcher = ADD_OR_COPY_PATTERN.matcher(cmd.trim());
138166
if (matcher.find()) {
139-
if (matcher.groupCount() != 2) {
140-
throw new DockerException(String.format("Wrong format on line [%s]", cmd));
167+
if (matcher.groupCount() != 3) {
168+
throw new DockerException(String.format("Wrong ADD or COPY format on line [%d]", lineNumber));
141169
}
142170

143-
String resource = matcher.group(1).trim();
171+
String extractedResource = matcher.group(2);
172+
173+
String resource = filterForEnvironmentVars(extractedResource, environmentMap);
144174

145175
if(isFileResource(resource)) {
146176
File src = new File(resource);
@@ -170,6 +200,33 @@ protected File buildDockerFolderTar() {
170200
}
171201
}
172202

203+
private String filterForEnvironmentVars(String extractedResource,
204+
Map<String, String> environmentMap) {
205+
206+
if (environmentMap.size() > 0) {
207+
208+
String currentResourceContent = extractedResource;
209+
210+
for (Map.Entry<String, String> entry : environmentMap.entrySet()) {
211+
212+
String variable = entry.getKey();
213+
214+
String replacementValue = entry.getValue();
215+
216+
// handle: $VARIABLE case
217+
currentResourceContent = currentResourceContent.replaceAll("\\$" + variable, replacementValue);
218+
219+
// handle ${VARIABLE} case
220+
currentResourceContent = currentResourceContent.replaceAll("\\$\\{" + variable + "\\}", replacementValue);
221+
222+
}
223+
224+
return currentResourceContent;
225+
}
226+
else
227+
return extractedResource;
228+
}
229+
173230
private static boolean isFileResource(String resource) {
174231
URI uri;
175232
try {

0 commit comments

Comments
 (0)