Skip to content

Commit baf85eb

Browse files
committed
Merge branch 'buildargs-option' into 'main'
Added buildArgs option to CLI so that users can pass ARGs into their custom build commands See merge request weblogic-cloud/weblogic-image-tool!450
2 parents 11a7170 + 5b2b175 commit baf85eb

File tree

10 files changed

+69
-2
lines changed

10 files changed

+69
-2
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/builder/BuildCommand.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.nio.file.Paths;
1616
import java.util.ArrayList;
1717
import java.util.List;
18+
import java.util.Map;
1819
import java.util.Objects;
1920
import java.util.stream.Collectors;
2021
import java.util.stream.Stream;
@@ -98,6 +99,21 @@ public BuildCommand buildArg(String key, String value, boolean conceal) {
9899
return this;
99100
}
100101

102+
/**
103+
* Add multiple --build-arg key value pairs to the Docker build command.
104+
* @param keyValuePairs A map of key-value pairs to be used directly with the container image builder
105+
*/
106+
public BuildCommand buildArg(Map<String,String> keyValuePairs) {
107+
if (keyValuePairs == null || keyValuePairs.isEmpty()) {
108+
return this;
109+
}
110+
111+
for (Map.Entry<String,String> entry : keyValuePairs.entrySet()) {
112+
buildArg(entry.getKey(), entry.getValue());
113+
}
114+
return this;
115+
}
116+
101117
/**
102118
* Add a --network to the Docker build command.
103119
* @param value the Docker network to use

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.time.Duration;
1212
import java.time.Instant;
1313
import java.util.List;
14+
import java.util.Map;
1415
import java.util.Properties;
1516
import java.util.UUID;
1617
import java.util.regex.Matcher;
@@ -123,6 +124,7 @@ BuildCommand getInitialBuildCmd(String contextFolder) {
123124
.tag(imageTag)
124125
.network(buildNetwork)
125126
.pull(buildPull)
127+
.buildArg(buildArgs)
126128
.buildArg("http_proxy", httpProxyUrl, httpProxyUrl != null && httpProxyUrl.contains("@"))
127129
.buildArg("https_proxy", httpsProxyUrl, httpsProxyUrl != null && httpsProxyUrl.contains("@"))
128130
.buildArg("no_proxy", nonProxyHosts);
@@ -175,6 +177,12 @@ void initializeOptions() throws InvalidCredentialException, IOException, Invalid
175177
handleChown();
176178
handleAdditionalBuildCommands();
177179

180+
if (buildArgs != null) {
181+
for (String arg : buildArgs.keySet()) {
182+
dockerfileOptions.buildArgs(arg);
183+
}
184+
}
185+
178186
if (kubernetesTarget == KubernetesTarget.OPENSHIFT) {
179187
dockerfileOptions.setDomainGroupAsUser(true);
180188
// if the user did not set the OS user:group, make the default oracle:root, instead of oracle:oracle
@@ -408,6 +416,12 @@ public String buildId() {
408416
)
409417
KubernetesTarget kubernetesTarget = KubernetesTarget.DEFAULT;
410418

419+
@Option(
420+
names = {"--buildArgs"},
421+
description = "Additional argument passed directly to the build engine."
422+
)
423+
Map<String,String> buildArgs;
424+
411425
@SuppressWarnings("unused")
412426
@Unmatched
413427
List<String> unmatchedOptions;

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/DockerfileOptions.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class DockerfileOptions {
6666
private MiddlewareInstall mwInstallers;
6767
private boolean domainGroupAsUser;
6868
private boolean usingBusybox;
69+
private List<String> buildArgs;
6970

7071
// WDT values
7172
private String wdtHome;
@@ -97,6 +98,7 @@ public DockerfileOptions(String buildId) {
9798
skipMiddlewareInstall = false;
9899
domainGroupAsUser = false;
99100
usingBusybox = false;
101+
buildArgs = new ArrayList<>();
100102

101103
javaHome = DEFAULT_JAVA_HOME;
102104
oracleHome = DEFAULT_ORACLE_HOME;
@@ -1119,4 +1121,23 @@ public DockerfileOptions usingBusybox(boolean value) {
11191121
usingBusybox = value;
11201122
return this;
11211123
}
1124+
1125+
/**
1126+
* Used by mustache template to retrieve variable names for ARG in Dockerfile.
1127+
* @return list of variable names
1128+
*/
1129+
@SuppressWarnings("unused")
1130+
public List<String> buildArgs() {
1131+
return buildArgs;
1132+
}
1133+
1134+
/**
1135+
* Add ARG statements to the Dockerfile.
1136+
* @param variableName an ARG name.
1137+
* @return this
1138+
*/
1139+
public DockerfileOptions buildArgs(String variableName) {
1140+
buildArgs.add(variableName);
1141+
return this;
1142+
}
11221143
}

imagetool/src/main/resources/docker-files/Create_Image.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44

55
FROM {{baseImage}} as os_update
6+
{{#buildArgs}}ARG {{{.}}}
7+
{{/buildArgs}}
68
LABEL com.oracle.weblogic.imagetool.buildid="{{buildId}}"
79
USER root
810
{{#initialBuildCommands}}
@@ -29,7 +31,8 @@ USER root
2931
{{/isWdtEnabled}}
3032

3133
FROM os_update as final_build
32-
34+
{{#buildArgs}}ARG {{{.}}}
35+
{{/buildArgs}}
3336
ENV ORACLE_HOME={{{oracle_home}}} \
3437
LD_LIBRARY_PATH={{{oracle_home}}}/oracle_common/adr:$LD_LIBRARY_PATH \
3538
{{#installJava}}

imagetool/src/main/resources/docker-files/Rebase_Image.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
{{#isRebaseToTarget}}
66
FROM {{sourceImage}} as source_image
77
FROM {{targetImage}} as final_build
8+
{{#buildArgs}}ARG {{{.}}}
9+
{{/buildArgs}}
810

911
ENV DOMAIN_HOME={{{domain_home}}}
1012
LABEL com.oracle.weblogic.imagetool.buildid="{{buildId}}"
@@ -38,7 +40,8 @@ LABEL com.oracle.weblogic.imagetool.buildid="{{buildId}}"
3840
{{/installMiddleware}}
3941

4042
FROM os_update as final_build
41-
43+
{{#buildArgs}}ARG {{{.}}}
44+
{{/buildArgs}}
4245
ENV ORACLE_HOME={{{oracle_home}}} \
4346
LD_LIBRARY_PATH={{{oracle_home}}}/oracle_common/adr:$LD_LIBRARY_PATH \
4447
{{#installJava}}

imagetool/src/main/resources/docker-files/Update_Image.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
{{/isWdtEnabled}}
88

99
FROM {{baseImage}} as final_build
10+
{{#buildArgs}}ARG {{{.}}}
11+
{{/buildArgs}}
1012
USER root
1113

1214
ENV OPATCH_NO_FUSER=true

imagetool/src/main/resources/docker-files/aux-image.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Create Auxiliary Image for WebLogic Kubernetes Operator
77

88
FROM {{baseImage}} as os_update
9+
{{#buildArgs}}ARG {{{.}}}
10+
{{/buildArgs}}
911
LABEL com.oracle.weblogic.imagetool.buildid="{{buildId}}"
1012

1113
USER root

imagetool/src/main/resources/docker-files/install-java.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Installing Java
55

66
FROM os_update as jdk_build
7+
{{#buildArgs}}ARG {{{.}}}
8+
{{/buildArgs}}
79
LABEL com.oracle.weblogic.imagetool.buildid="{{buildId}}"
810

911
ENV JAVA_HOME={{{java_home}}}

imagetool/src/main/resources/docker-files/install-middleware.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Installing Middleware
55

66
FROM os_update as wls_build
7+
{{#buildArgs}}ARG {{{.}}}
8+
{{/buildArgs}}
79
LABEL com.oracle.weblogic.imagetool.buildid="{{buildId}}"
810

911
ENV JAVA_HOME={{{java_home}}} \

imagetool/src/main/resources/docker-files/run-wdt.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Create WLS domain (or model)
55

66
FROM {{{wdtBase}}} as wdt_build
7+
{{#buildArgs}}ARG {{{.}}}
8+
{{/buildArgs}}
79
LABEL com.oracle.weblogic.imagetool.buildid="{{buildId}}"
810

911
ENV WLSDEPLOY_PROPERTIES="{{{wlsdeploy_properties}}} -Djava.security.egd=file:/dev/./urandom" \

0 commit comments

Comments
 (0)