Skip to content

Commit d7d9525

Browse files
committed
Pass JAVA_VERSION to prebuild. Move more scripts to preprocessor
1 parent 7559dd6 commit d7d9525

File tree

10 files changed

+313
-155
lines changed

10 files changed

+313
-155
lines changed

deploy/tasks/pre-build.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ spec:
4747
description: The build tool to use.
4848
- name: BUILD_TOOL_VERSION
4949
description: The build tool version to use.
50-
- name: JAVA_HOME
51-
description: Java home directory to use.
50+
- name: JAVA_VERSION
51+
description: Java version to use (7, 8, 9, 11, 17, 21, 22)
5252
- name: BUILD_PLUGINS
5353
description: Comma separated list of build plugins that should be disabled.
5454
default: ""
@@ -81,8 +81,7 @@ spec:
8181
cpu: 10m
8282
memory: 512Mi
8383
script: |
84-
$(params.BUILD_SCRIPT)
85-
/opt/jboss/container/java/run/run-java.sh $(params.BUILD_TOOL)-prepare --java-home=$(params.JAVA_HOME) --build-tool-version=$(params.BUILD_TOOL_VERSION) --recipe-image=$(params.RECIPE_IMAGE) --request-processor-image=$(params.JVM_BUILD_SERVICE_REQPROCESSOR_IMAGE) --disabled-plugins=$(params.BUILD_PLUGINS) $(workspaces.source.path)/source
84+
/opt/jboss/container/java/run/run-java.sh $(params.BUILD_TOOL)-prepare --java-version=$(params.JAVA_VERSION) --build-tool-version=$(params.BUILD_TOOL_VERSION) --recipe-image=$(params.RECIPE_IMAGE) --request-processor-image=$(params.JVM_BUILD_SERVICE_REQPROCESSOR_IMAGE) --disabled-plugins=$(params.BUILD_PLUGINS) $(workspaces.source.path)/source
8685
env:
8786
- name: BUILD_SCRIPT
8887
value: $(params.BUILD_SCRIPT)

java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/build/preprocessor/AbstractPreprocessor.java

Lines changed: 177 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.redhat.hacbs.container.build.preprocessor;
22

3+
import static org.apache.commons.lang3.StringUtils.isEmpty;
34
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
45

56
import java.io.IOException;
@@ -32,8 +33,8 @@ public abstract class AbstractPreprocessor implements Runnable {
3233
@CommandLine.Option(names = "--request-processor-image", required = true)
3334
String buildRequestProcessorImage;
3435

35-
@CommandLine.Option(names = "--java-home", required = true)
36-
String javaHome;
36+
@CommandLine.Option(names = "--java-version", required = true)
37+
String javaVersion;
3738

3839
@CommandLine.Option(names = "--build-tool-version", required = true)
3940
String buildToolVersion;
@@ -54,45 +55,66 @@ public String toString() {
5455

5556
@Override
5657
public void run() {
57-
58-
Log.warnf("### Using tool %s with version %s and javaHome %s", type, buildToolVersion, javaHome);
59-
Log.warnf("### ENV %s", System.getenv("jvm-build-service"));
60-
Log.warnf("### BUILD_SCRIPT %s", System.getenv("BUILD_SCRIPT"));
61-
58+
Path workspaceSource = buildRoot.getParent();
6259
Path jbsDirectory = Path.of(buildRoot.toString(), ".jbs");
6360
//noinspection ResultOfMethodCallIgnored
6461
jbsDirectory.toFile().mkdirs();
6562

63+
String buildScript = System.getenv("BUILD_SCRIPT");
64+
Log.warnf("### BUILD_SCRIPT %s", buildScript);
65+
if (isEmpty(buildScript)) {
66+
Log.errorf("Unable to find BUILD_SCRIPT in environment");
67+
throw new RuntimeException("No BUILD_SCRIPT found");
68+
}
69+
70+
Log.warnf("### WorkspaceSource %s", workspaceSource);
71+
Log.warnf("### Using tool %s with version %s and javaHome %s", type, buildToolVersion, javaVersion);
72+
Log.warnf("### ENV %s", System.getenv("jvm-build-service"));
73+
74+
String javaHome = "";
75+
if (javaVersion.equals("7") || javaVersion.equals("8")) {
76+
javaHome = "/lib/jvm/java-1." + javaVersion + ".0";
77+
} else {
78+
javaHome = "/lib/jvm/java-" + javaVersion;
79+
}
80+
81+
82+
// TODO: Rename CACHE_URL to PROXY_URL to cover both Indy and JBS use-cases
6683
String runBuild = """
6784
#!/usr/bin/env bash
6885
set -o verbose
6986
set -eu
7087
set -o pipefail
7188
89+
#fix this when we no longer need to run as root
90+
export HOME=/root
91+
7292
export LANG="en_US.UTF-8"
7393
export LC_ALL="en_US.UTF-8"
7494
export JAVA_HOME=%s
7595
# This might get overridden by the tool home configuration below. This is
7696
# useful if Gradle/Ant also requires Maven configured.
7797
export MAVEN_HOME=/opt/maven/3.8.8
98+
# If we run out of memory we want the JVM to die with error code 134
99+
export MAVEN_OPTS="-XX:+CrashOnOutOfMemoryError"
100+
# If we run out of memory we want the JVM to die with error code 134
101+
export JAVA_OPTS="-XX:+CrashOnOutOfMemoryError"
78102
export %s_HOME=/opt/%s/%s
79103
104+
mkdir -p /var/workdir/workspace/artifacts /var/workdir/workspace/logs /var/workdir/workspace/packages /var/workdir/software/settings ${HOME}/.sbt/1.0
80105
cd %s
81-
mkdir -p ../logs ../packages
82106
83107
if [ ! -z ${JAVA_HOME+x} ]; then
84108
echo "JAVA_HOME:$JAVA_HOME"
85109
PATH="${JAVA_HOME}/bin:$PATH"
86110
fi
87111
88112
if [ ! -z ${MAVEN_HOME+x} ]; then
89-
echo "MAVEN_HOME:$MAVEN_HOME"
90-
PATH="${MAVEN_HOME}/bin:$PATH"
113+
""".formatted(javaHome, type.name(), type, buildToolVersion, buildRoot);
91114

92-
if [ ! -d "${MAVEN_HOME}" ]; then
93-
echo "Maven home directory not found at ${MAVEN_HOME}" >&2
94-
exit 1
95-
fi
115+
runBuild += getMavenSetup();
116+
117+
runBuild += """
96118
fi
97119
98120
if [ ! -z ${GRADLE_HOME+x} ]; then
@@ -113,6 +135,29 @@ public void run() {
113135
echo "Ant home directory not found at ${ANT_HOME}" >&2
114136
exit 1
115137
fi
138+
139+
if [ ! z ${CACHE_URL+x} ]; then
140+
cat > ivysettings.xml << EOF
141+
<ivysettings>
142+
<property name="cache-url" value="${CACHE_URL}"/>
143+
<property name="default-pattern" value="[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
144+
<property name="local-pattern" value="\\${user.home}/.m2/repository/[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
145+
<settings defaultResolver="defaultChain"/>
146+
<resolvers>
147+
<ibiblio name="default" root="\\${cache-url}" pattern="\\${default-pattern}" m2compatible="true"/>
148+
<filesystem name="local" m2compatible="true">
149+
<artifact pattern="\\${local-pattern}"/>
150+
<ivy pattern="\\${local-pattern}"/>
151+
</filesystem>
152+
<chain name="defaultChain">
153+
<resolver ref="local"/>
154+
<resolver ref="default"/>
155+
</chain>
156+
</resolvers>
157+
</ivysettings>
158+
EOF
159+
fi
160+
116161
fi
117162
118163
if [ ! -z ${SBT_DIST+x} ]; then
@@ -123,16 +168,31 @@ public void run() {
123168
echo "SBT home directory not found at ${SBT_DIST}" >&2
124169
exit 1
125170
fi
171+
172+
if [ ! z ${CACHE_URL+x} ]; then
173+
cat > "$HOME/.sbt/repositories" <<EOF
174+
[repositories]
175+
local
176+
my-maven-proxy-releases: ${CACHE_URL}
177+
EOF
178+
fi
179+
# TODO: we may need .allowInsecureProtocols here for minikube based tests that don't have access to SSL
180+
cat >"$HOME/.sbt/1.0/global.sbt" <<EOF
181+
publishTo := Some(("MavenRepo" at s"file:%s/artifacts")),
182+
EOF
183+
184+
126185
fi
127186
echo "PATH:$PATH"
128187
129-
#fix this when we no longer need to run as root
130-
export HOME=/root
188+
""".formatted(workspaceSource);
131189

190+
Log.warnf("### runBuild is %s", runBuild);
191+
192+
193+
runBuild += buildScript;
132194

133-
""".formatted(javaHome, type.name(), type, buildToolVersion, buildRoot);
134195

135-
Log.warnf("### runBuild is %s", runBuild);
136196

137197
String containerFile = """
138198
FROM %s
@@ -177,11 +237,110 @@ public void run() {
177237
COPY --from=0 /var/workdir/workspace/artifacts /
178238
""";
179239
}
240+
241+
242+
180243
try {
244+
Files.writeString(Paths.get(jbsDirectory.toString(), "run-build.sh"), runBuild);
181245
Files.writeString(Paths.get(jbsDirectory.toString(), "Containerfile"), containerFile);
182246
} catch (IOException e) {
183247
Log.errorf("Unable to write Containerfile", e);
184248
throw new RuntimeException(e);
185249
}
186250
}
251+
252+
253+
private String getMavenSetup() {
254+
return """
255+
echo "MAVEN_HOME:$MAVEN_HOME"
256+
PATH="${MAVEN_HOME}/bin:$PATH"
257+
258+
if [ ! -d "${MAVEN_HOME}" ]; then
259+
echo "Maven home directory not found at ${MAVEN_HOME}" >&2
260+
exit 1
261+
fi
262+
263+
if [ ! z ${CACHE_URL+x} ]; then
264+
cat >"/var/workdir/software/settings"/settings.xml <<EOF
265+
<settings>
266+
<mirrors>
267+
<mirror>
268+
<id>mirror.default</id>
269+
<url>${CACHE_URL}</url>
270+
<mirrorOf>*</mirrorOf>
271+
</mirror>
272+
</mirrors>
273+
EOF
274+
else
275+
cat >"/var/workdir/software/settings"/settings.xml <<EOF
276+
<settings>
277+
EOF
278+
fi
279+
cat >>"/var/workdir/software/settings"/settings.xml <<EOF
280+
<!-- Off by default, but allows a secondary Maven build to use results of prior (e.g. Gradle) deployment -->
281+
<profiles>
282+
<profile>
283+
<id>secondary</id>
284+
<activeByDefault>true</activeByDefault>
285+
</activation>
286+
<repositories>
287+
<repository>
288+
<id>artifacts</id>
289+
<url>file:///var/workdir/workspace/artifacts</url>
290+
<releases>
291+
<enabled>true</enabled>
292+
<checksumPolicy>ignore</checksumPolicy>
293+
</releases>
294+
</repository>
295+
</repositories>
296+
<pluginRepositories>
297+
<pluginRepository>
298+
<id>artifacts</id>
299+
<url>file:///var/workdir/workspace/artifacts</url>
300+
<releases>
301+
<enabled>true</enabled>
302+
<checksumPolicy>ignore</checksumPolicy>
303+
</releases>
304+
</pluginRepository>
305+
</pluginRepositories>
306+
</profile>
307+
</profiles>
308+
</settings>
309+
EOF
310+
311+
312+
TOOLCHAINS_XML="/var/workdir/software/settings"/toolchains.xml
313+
314+
cat >"$TOOLCHAINS_XML" <<EOF
315+
<?xml version="1.0" encoding="UTF-8"?>
316+
<toolchains>
317+
EOF
318+
319+
if [ "%s" = "7" ]; then
320+
JAVA_VERSIONS="7:1.7.0 8:1.8.0 11:11"
321+
else
322+
JAVA_VERSIONS="8:1.8.0 9:11 11:11 17:17 21:21 22:22"
323+
fi
324+
325+
for i in $JAVA_VERSIONS; do
326+
version=$(echo $i | cut -d : -f 1)
327+
home=$(echo $i | cut -d : -f 2)
328+
cat >>"$TOOLCHAINS_XML" <<EOF
329+
<toolchain>
330+
<type>jdk</type>
331+
<provides>
332+
<version>$version</version>
333+
</provides>
334+
<configuration>
335+
<jdkHome>/usr/lib/jvm/java-$home-openjdk</jdkHome>
336+
</configuration>
337+
</toolchain>
338+
EOF
339+
done
340+
341+
cat >>"$TOOLCHAINS_XML" <<EOF
342+
</toolchains>
343+
EOF
344+
""".formatted(javaVersion);
345+
}
187346
}

pkg/reconciler/dependencybuild/buildrecipeyaml.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func createPipelineSpec(log logr.Logger, tool string, commitTime int64, jbsConfi
151151
tlsVerify = "false"
152152
}
153153

154+
// TODO: ### Remove javaHome?
154155
var javaHome string
155156
if recipe.JavaVersion == "7" || recipe.JavaVersion == "8" {
156157
javaHome = "/lib/jvm/java-1." + recipe.JavaVersion + ".0"
@@ -280,7 +281,7 @@ func createPipelineSpec(log logr.Logger, tool string, commitTime int64, jbsConfi
280281
{Name: PipelineParamChainsGitUrl, Type: tektonpipeline.ParamTypeString},
281282
{Name: PipelineParamChainsGitCommit, Type: tektonpipeline.ParamTypeString},
282283
{Name: PipelineParamGoals, Type: tektonpipeline.ParamTypeArray},
283-
{Name: PipelineParamJavaVersion, Type: tektonpipeline.ParamTypeString},
284+
//{Name: PipelineParamJavaVersion, Type: tektonpipeline.ParamTypeString},
284285
// {Name: PipelineParamToolVersion, Type: tektonpipeline.ParamTypeString},
285286
{Name: PipelineParamPath, Type: tektonpipeline.ParamTypeString},
286287
{Name: PipelineParamCacheUrl, Type: tektonpipeline.ParamTypeString, Default: &tektonpipeline.ResultValue{Type: tektonpipeline.ParamTypeString, StringVal: cacheUrl}},
@@ -452,17 +453,17 @@ func createPipelineSpec(log logr.Logger, tool string, commitTime int64, jbsConfi
452453
},
453454
},
454455
{
455-
Name: "JAVA_HOME",
456+
Name: "JAVA_VERSION",
456457
Value: tektonpipeline.ParamValue{
457458
Type: tektonpipeline.ParamTypeString,
458-
StringVal: javaHome,
459+
StringVal: recipe.JavaVersion,
459460
},
460461
},
461462
{
462463
Name: "BUILD_SCRIPT",
463464
Value: tektonpipeline.ParamValue{
464465
Type: tektonpipeline.ParamTypeString,
465-
StringVal: createKonfluxScripts(konfluxScript),
466+
StringVal: konfluxScript,
466467
},
467468
},
468469
{

pkg/reconciler/dependencybuild/dependencybuild.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ func (r *ReconcileDependencyBuild) handleStateBuilding(ctx context.Context, db *
590590
{Name: PipelineParamChainsGitCommit, Value: tektonpipeline.ResultValue{Type: tektonpipeline.ParamTypeString, StringVal: db.Spec.ScmInfo.CommitHash}},
591591
{Name: PipelineParamPath, Value: tektonpipeline.ResultValue{Type: tektonpipeline.ParamTypeString, StringVal: contextDir}},
592592
{Name: PipelineParamGoals, Value: tektonpipeline.ResultValue{Type: tektonpipeline.ParamTypeArray, ArrayVal: attempt.Recipe.CommandLine}},
593-
{Name: PipelineParamJavaVersion, Value: tektonpipeline.ResultValue{Type: tektonpipeline.ParamTypeString, StringVal: attempt.Recipe.JavaVersion}},
593+
//{Name: PipelineParamJavaVersion, Value: tektonpipeline.ResultValue{Type: tektonpipeline.ParamTypeString, StringVal: attempt.Recipe.JavaVersion}},
594594
}
595595

596596
orasOptions := ""

pkg/reconciler/dependencybuild/scripts/ant-build.sh

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
# exit 1
66
#fi
77

8-
if [ -z ${JBS_DISABLE_CACHE+x} ]; then
9-
# XXX: It's possible that build.xml is not in the root directory
10-
cat > ivysettings.xml << EOF
11-
<ivysettings>
12-
<property name="cache-url" value="${CACHE_URL}"/>
13-
<property name="default-pattern" value="[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
14-
<property name="local-pattern" value="\${user.home}/.m2/repository/[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
15-
<settings defaultResolver="defaultChain"/>
16-
<resolvers>
17-
<ibiblio name="default" root="\${cache-url}" pattern="\${default-pattern}" m2compatible="true"/>
18-
<filesystem name="local" m2compatible="true">
19-
<artifact pattern="\${local-pattern}"/>
20-
<ivy pattern="\${local-pattern}"/>
21-
</filesystem>
22-
<chain name="defaultChain">
23-
<resolver ref="local"/>
24-
<resolver ref="default"/>
25-
</chain>
26-
</resolvers>
27-
</ivysettings>
28-
EOF
29-
fi
8+
#if [ -z ${JBS_DISABLE_CACHE+x} ]; then
9+
# # XXX: It's possible that build.xml is not in the root directory
10+
# cat > ivysettings.xml << EOF
11+
#<ivysettings>
12+
# <property name="cache-url" value="${CACHE_URL}"/>
13+
# <property name="default-pattern" value="[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
14+
# <property name="local-pattern" value="\${user.home}/.m2/repository/[organisation]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"/>
15+
# <settings defaultResolver="defaultChain"/>
16+
# <resolvers>
17+
# <ibiblio name="default" root="\${cache-url}" pattern="\${default-pattern}" m2compatible="true"/>
18+
# <filesystem name="local" m2compatible="true">
19+
# <artifact pattern="\${local-pattern}"/>
20+
# <ivy pattern="\${local-pattern}"/>
21+
# </filesystem>
22+
# <chain name="defaultChain">
23+
# <resolver ref="local"/>
24+
# <resolver ref="default"/>
25+
# </chain>
26+
# </resolvers>
27+
#</ivysettings>
28+
#EOF
29+
#fi
3030

3131
echo "Running $(which ant) with arguments: $@"
3232
eval "ant $@" | tee $(workspaces.source.path)/logs/ant.log

0 commit comments

Comments
 (0)