Skip to content

Commit 55f09dc

Browse files
native build modified with OCI DevOps
1 parent d89dca4 commit 55f09dc

File tree

5 files changed

+68
-55
lines changed

5 files changed

+68
-55
lines changed

app-dev/devops-and-containers/devops/oci-devops-terraform-function-java-graalvm/files/build_pipeline_specs/build_spec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ steps:
1414
command: |
1515
docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME} .
1616
docker tag ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:latest ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:$buildId
17+
1718
outputArtifacts:
1819
- name: image-jvm
1920
type: DOCKER_IMAGE

app-dev/devops-and-containers/devops/oci-devops-terraform-function-java-graalvm/files/build_pipeline_specs/build_spec_native.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,45 @@ component: build
33
timeoutInSeconds: 5000
44
shell: bash
55
env:
6+
variables:
7+
"JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java23"
68
exportedVariables:
79
- buildId
10+
# https://docs.oracle.com/en/graalvm/jdk/23/docs/getting-started/oci/devops-build-pipeline/#prerequisites
811
steps:
912
- type: Command
1013
command: |
1114
buildId=`echo ${OCI_BUILD_RUN_ID} | rev | cut -c 1-6 | rev`
1215
echo "Build ID: $buildId"
16+
- type: Command
17+
command: |
18+
yum -y install graalvm-23-native-image
19+
- type: Command
20+
command: |
21+
export PATH=$JAVA_HOME/bin:$PATH
22+
- type: Command
23+
command: |
24+
mvn package
25+
- type: Command
26+
command: |
27+
native-image \
28+
-H:ReflectionConfigurationFiles=reflection.json \
29+
-Ob \
30+
-H:Name=Hello \
31+
-cp "target/Hellofunc-1.0-SNAPSHOT-jar-with-dependencies.jar" \
32+
com.fnproject.fn.runtime.EntryPoint
1333
- type: Command
1434
command: |
1535
docker build -f Dockerfile.native -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME} .
1636
docker tag ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:latest ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:$buildId
37+
# Alternate (faster) method of pushing if don't want to wait outputArtifacts getting executed (stage can be removed from build pipeline)
38+
# This does not require personal Docker credentials (!)
39+
- type: Command
40+
command: |
41+
yum install jq -y
42+
oci raw-request --region eu-frankfurt-1 --http-method GET --target-uri 'https://fra.ocir.io/20180419/docker/token' --auth=resource_principal | jq -r .data.token | docker login -u BEARER_TOKEN --password-stdin fra.ocir.io
43+
docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:$buildId
44+
1745
outputArtifacts:
1846
- name: image-native
1947
type: DOCKER_IMAGE

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/README.md

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,18 @@ GraalVM compiles your Java functions ahead of time into standalone binaries that
116116

117117
<p>
118118

119-
To do this a Docker multi-stage build is used.
120-
119+
To do this a <a href="https://github.com/oracle-devrel/technology-engineering/tree/main/app-dev/devops-and-containers/devops/oci-devops-terraform-function-java-graalvm">create OCI DevOps project for a Function CI/CD</a> and then clone the repo and copy the following files with the Function source and commit and push them to the repo:
121120
<p>
122-
123-
Before building the native image let's do a full maven build for the project to create the necessary libraries under <code>target/lib</code>:
124-
125-
<pre>
126-
mvn clean install
127-
</pre>
128-
129-
Then build the Docker container using <a href="./files/Dockerfile.native">multi-stage Docker file</a> including the GraalVM native image compilation:
130-
131-
<pre>
132-
docker build -f Dockerfile.native -t fra.ocir.io/&lt;YOUR OCI TENANCY NAMESPACE&gt;/helloworldai-java:2 .
133-
</pre>
134-
135-
The GraalVM compilation stage requires quite a bit resources from your localhost so in case for example using Rancher desktop
136-
think of increasing the CPU and memory for it to make the build faster.
137-
121+
<ul>
122+
<li>Dockerfile.native</li>
123+
<li>build_spec_native.yaml</li>
124+
<li>reflection.json</li>
125+
<li>pom.xml</li>
126+
</ul>
138127
<p>
139-
140-
In the <a href="./files/Dockerfile.native">Dockerfile.native</a> two things are important: Including the <a href="./files/reflection.json">reflection.json</a> with the proper function class name and passing Fn FDK libraries with <code>"-Djava.library.path=/lib"</code> in the container CMD along with the <code>"com.example.HelloAIFunction::handleRequest"</code> function handler.
141-
128+
After pushing run the native build pipeline and test the Function in cloud shell.
142129
<p>
143130

144-
After the build push the container to OCIR repo:
145-
146-
<pre>
147-
docker push fra.ocir.io/&lt;YOUR OCI TENANCY NAMESPACE&gt;/helloworldai-java:2
148-
</pre>
149-
150-
Finally deploy the container to your OCI Function by replacing the container using the Cloud UI by editing the function and changing the container from <code>helloworldai-java:1</code> to <code>helloworldai-java:2</code>. Then test it.
151-
152131

153132
# Useful Links
154133

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
1-
FROM fnproject/fn-java-fdk-build:jdk17-1.0-latest as build-stage
2-
WORKDIR /function
3-
ENV MAVEN_OPTS -Dhttp.proxyHost= -Dhttp.proxyPort= -Dhttps.proxyHost= -Dhttps.proxyPort= -Dhttp.nonProxyHosts= -Dmaven.repo.local=/usr/share/maven/ref/repository
4-
ADD pom.xml /function/pom.xml
5-
RUN ["mvn", "package", "dependency:copy-dependencies", "-DincludeScope=runtime", "-DskipTests=true", "-Dmdep.prependGroupId=true", "-DoutputDirectory=target", "--fail-never"]
6-
ADD src /function/src
7-
RUN ["mvn", "package"]
8-
9-
FROM container-registry.oracle.com/graalvm/native-image:23-ol8 AS native
10-
WORKDIR /app
11-
COPY --from=build-stage /function/target .
12-
ADD reflection.json .
13-
14-
RUN native-image \
15-
-H:ReflectionConfigurationFiles=/app/reflection.json \
16-
-Ob \
17-
-H:Name=Hello \
18-
-cp "/app/Hellofunc-1.0-SNAPSHOT.jar:/app/*" \
19-
com.fnproject.fn.runtime.EntryPoint
20-
21-
FROM fnproject/fn-java-fdk:jre17-latest as fdk
22-
23-
FROM container-registry.oracle.com/os/oraclelinux:8-slim
24-
COPY --from=native /app/Hello .
25-
COPY --from=fdk /function/runtime/* ./
1+
FROM fnproject/fn-java-fdk:jre17-latest
2+
COPY Hello ./
263
ENTRYPOINT [ "./Hello" ]
27-
CMD [ "com.example.fn.HelloFunction::handleRequest", "-Djava.library.path=/lib"]
4+
CMD [ "com.example.fn.HelloFunction::handleRequest", "-Djava.library.path=/function/runtime/lib"]

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/files/build_spec_native.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,45 @@ component: build
33
timeoutInSeconds: 5000
44
shell: bash
55
env:
6+
variables:
7+
"JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java23"
68
exportedVariables:
79
- buildId
10+
# https://docs.oracle.com/en/graalvm/jdk/23/docs/getting-started/oci/devops-build-pipeline/#prerequisites
811
steps:
912
- type: Command
1013
command: |
1114
buildId=`echo ${OCI_BUILD_RUN_ID} | rev | cut -c 1-6 | rev`
1215
echo "Build ID: $buildId"
16+
- type: Command
17+
command: |
18+
yum -y install graalvm-23-native-image
19+
- type: Command
20+
command: |
21+
export PATH=$JAVA_HOME/bin:$PATH
22+
- type: Command
23+
command: |
24+
mvn package
25+
- type: Command
26+
command: |
27+
native-image \
28+
-H:ReflectionConfigurationFiles=reflection.json \
29+
-Ob \
30+
-H:Name=Hello \
31+
-cp "target/Hellofunc-1.0-SNAPSHOT-jar-with-dependencies.jar" \
32+
com.fnproject.fn.runtime.EntryPoint
1333
- type: Command
1434
command: |
1535
docker build -f Dockerfile.native -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME} .
1636
docker tag ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:latest ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:$buildId
37+
# Alternate (faster) method of pushing if don't want to wait outputArtifacts getting executed (stage can be removed from build pipeline)
38+
# This does not require personal Docker credentials (!)
39+
- type: Command
40+
command: |
41+
yum install jq -y
42+
oci raw-request --region eu-frankfurt-1 --http-method GET --target-uri 'https://fra.ocir.io/20180419/docker/token' --auth=resource_principal | jq -r .data.token | docker login -u BEARER_TOKEN --password-stdin fra.ocir.io
43+
docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:$buildId
44+
1745
outputArtifacts:
1846
- name: image-native
1947
type: DOCKER_IMAGE

0 commit comments

Comments
 (0)