Skip to content

Commit 5a97607

Browse files
authored
Merge pull request #311 from oracle-devrel/timgraves-devops-native-image
Timgraves devops native image
2 parents 7e2dd86 + 34033b0 commit 5a97607

File tree

3 files changed

+252
-1
lines changed

3 files changed

+252
-1
lines changed

app-dev/cloud-native/devops/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ OCI DevOps, OCI Resource Manager, Visual Builder Studio, Open Source tools
2424
- [OCI DevOps Build Pipeline collection](https://github.com/oracle-devrel/oci-devops-examples)
2525
- [OCI CLI documentation](https://docs.oracle.com/iaas/tools/oci-cli/latest/oci_cli_docs/)
2626
- [Deploying a Helm chart with Provenance liveLab](https://apexapps.oracle.com/pls/apex/r/dbpm/livelabs/view-workshop?wid=3664&clear=RR,180&session=109957900717640)
27-
27+
- [Sample build-spec.yaml](./devops-graalvm-native-image/build-spec.yaml) and [associated Dockerfile](./devops-graalvm-native-image/Dockerfile) to use with OCI Devops. These are very configurable and show the installing of specific versions of software (GraalVM and Maven) into the build environment, creating of a native image executable using the installed software, determining version information dynamically and creating a docker image. The example used is the Quarkus getting-started example, but the build command should be the same for all quarkus examples, for Helidon and others you may need to change it slightly. This also assumes that the projects code base is in a sub directory of the gir repo (callsed getting-started in this case) you will need to modify the PROJECT_PATH_TO_BUILD to reflect a different location. To use this in a devops build pipeline place the Dockerfile and build-spec.yml in your projects code root (where the pom.xml file is). Adjust the PROJECT_PATH_TO_BUILD in the build spec and other variables as needed, add the files to your git repo. Change the output stage names for the image if needed. Configure the git repo as the primary GIT repo for the projects build stage and specify the build-spec.yaml file location.
2828

2929
### Terraform
3030

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (c) 2023 Oracle and/or its affiliates.
2+
#
3+
# The Universal Permissive License (UPL), Version 1.0
4+
#
5+
# Subject to the condition set forth below, permission is hereby granted to any
6+
# person obtaining a copy of this software, associated documentation and/or data
7+
# (collectively the "Software"), free of charge and under any and all copyright
8+
# rights in the Software, and any and all patent rights owned or freely
9+
# licensable by each licensor hereunder covering either (i) the unmodified
10+
# Software as contributed to or provided by such licensor, or (ii) the Larger
11+
# Works (as defined below), to deal in both
12+
#
13+
# (a) the Software, and
14+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
# one is included with the Software (each a "Larger Work" to which the Software
16+
# is contributed by such licensors),
17+
# without restriction, including without limitation the rights to copy, create
18+
# derivative works of, display, perform, and distribute the Software and make,
19+
# use, sell, offer for sale, import, export, have made, and have sold the
20+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
21+
# either these or other terms.
22+
#
23+
# This license is subject to the following condition:
24+
# The above copyright notice and either this complete permission notice or at
25+
# a minimum a reference to the UPL must be included in all copies or
26+
# substantial portions of the Software.
27+
#
28+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34+
# SOFTWARE.
35+
ARG BASE_IMAGE
36+
FROM $BASE_IMAGE
37+
38+
ARG APP_DIR
39+
ARG APP_SOURCE
40+
ARG APPLICATION_NAME
41+
42+
RUN mkdir -p $APP_DIR
43+
COPY $APP_SOURCE $APP_DIR/$APPLICATION_NAME
44+
45+
CMD $APP_DIR/$APPLICATION_NAME
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# Copyright (c) 2023 Oracle and/or its affiliates.
2+
#
3+
# The Universal Permissive License (UPL), Version 1.0
4+
#
5+
# Subject to the condition set forth below, permission is hereby granted to any
6+
# person obtaining a copy of this software, associated documentation and/or data
7+
# (collectively the "Software"), free of charge and under any and all copyright
8+
# rights in the Software, and any and all patent rights owned or freely
9+
# licensable by each licensor hereunder covering either (i) the unmodified
10+
# Software as contributed to or provided by such licensor, or (ii) the Larger
11+
# Works (as defined below), to deal in both
12+
#
13+
# (a) the Software, and
14+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
# one is included with the Software (each a "Larger Work" to which the Software
16+
# is contributed by such licensors),
17+
# without restriction, including without limitation the rights to copy, create
18+
# derivative works of, display, perform, and distribute the Software and make,
19+
# use, sell, offer for sale, import, export, have made, and have sold the
20+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
21+
# either these or other terms.
22+
#
23+
# This license is subject to the following condition:
24+
# The above copyright notice and either this complete permission notice or at
25+
# a minimum a reference to the UPL must be included in all copies or
26+
# substantial portions of the Software.
27+
#
28+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34+
# SOFTWARE.
35+
version: 0.1
36+
component: build
37+
timeoutInSeconds: 6000
38+
runAs: root
39+
shell: bash
40+
41+
env:
42+
# these are local variables to the build config, we need to declare them here so thet will follow on in subsequent build stages
43+
# Note that PATH seems to be automatically added to the list of variables who's value is preserved across commands
44+
variables:
45+
# Software versions to install, this will work, but relies on the
46+
# download sources of the software retaining the same URL formats
47+
GRAALVM_JAVA_VERSION: "17"
48+
MVN_VERSION: "3.9.3"
49+
# some times the actuall code isn't in the root of the git repo
50+
# set this to any sub path or to . if you do want to build in the root of the repo
51+
PROJECT_PATH_TO_BUILD: "getting-started"
52+
# where any downloaded software used by the build will be installed,
53+
# unless you have a specific need there is no benefit to changing this
54+
SW_PATH: "installedsw"
55+
# Settings needed for the docker container
56+
DOCKER_BASE_IMAGE: "oraclelinux:8"
57+
# where in the resulting docker container the application will be placed
58+
# this path will be created is needed
59+
DOCKER_APPLICATION_DIR: "/application"
60+
# the following are determined during the various processing stages
61+
# and used to pass usefull around between the stages
62+
# You don't need to set them
63+
GRAALVM_HOME: "Not yet set"
64+
JAVA_HOME: "Not yet set"
65+
WORKING_PATH: "Not yet set"
66+
EXTRACTED_ARTIFACT_ID: "Not set"
67+
EXTRACTED_VERSION: "Not set"
68+
MVN_HOME: "Not yet set"
69+
APPLICATION_COMPILED_PATH: "Not yet set"
70+
APPLICATION_COMPILED_NAME: "Not yet set"
71+
72+
exportedVariables:
73+
- EXTRACTED_ARTIFACT_ID
74+
- EXTRACTED_VERSION
75+
76+
steps:
77+
- type: Command
78+
name: "Check local environment before starting work"
79+
command: |
80+
echo "Current working directory is "`pwd`
81+
echo "It's contents are "
82+
ls -l .
83+
echo "Source is in ${OCI_PRIMARY_SOURCE_DIR} it's contents are"
84+
cd ${OCI_PRIMARY_SOURCE_DIR}
85+
ls -l .
86+
export WORKING_PATH="/${OCI_PRIMARY_SOURCE_DIR}/${PROJECT_PATH_TO_BUILD}"
87+
echo "Project working path is ${WORKING_PATH}"
88+
cd ${WORKING_PATH}
89+
echo "Top level contents of working path via cwd "
90+
ls -l .
91+
- type: Command
92+
name: "Setup for software install"
93+
command: |
94+
echo "Configuring home directory for the upcomming sw installs"
95+
mkdir -p $HOME/${SW_PATH}
96+
# This uses YUM to install graalvm
97+
#- type: Command
98+
#name: "Install GraalVM Enterprise 22 Native Image for Java 17"
99+
#command: |
100+
#echo "Installing graal vm using yum"
101+
#yum -y install graalvm22-ee-17-native-image
102+
#echo "Java version is "
103+
#java -version
104+
# This used curl to download and then we unpack the graalvm. it give us control over the instalation location
105+
- type: Command
106+
name: "Install GraalVM"
107+
command: |
108+
echo "Installing graal vm for Java ${GRAALVM_JAVA_VERSION} into $HOME/${SW_PATH}"
109+
cd $HOME/${SW_PATH}
110+
echo "Download command is"
111+
echo curl https://download.oracle.com/graalvm/${GRAALVM_JAVA_VERSION}/latest/graalvm-jdk-${GRAALVM_JAVA_VERSION}_linux-x64_bin.tar.gz -o graalvm.tar.gz
112+
echo "Downloading"
113+
curl https://download.oracle.com/graalvm/${GRAALVM_JAVA_VERSION}/latest/graalvm-jdk-${GRAALVM_JAVA_VERSION}_linux-x64_bin.tar.gz -o graalvm.tar.gz
114+
echo "Download file details are"
115+
ls -l graalvm.tar.gz
116+
echo "Unpacking"
117+
tar xf graalvm.tar.gz
118+
echo "Removing download file"
119+
rm -f *tar.gz
120+
echo "Files after removing download are"
121+
ls -l
122+
echo "Relocating"
123+
mv graalvm-jdk-* graalvm
124+
echo "files after relocation"
125+
ls -l
126+
echo "Setting environment variables"
127+
export JAVA_HOME=$HOME/${SW_PATH}/graalvm
128+
export GRAALVM_HOME=${JAVA_HOME}
129+
echo "JAVA_HOME and GRAALVM_HOME both set to ${JAVA_HOME}"
130+
echo "Testing download version"
131+
${GRAALVM_HOME}/bin/java -version
132+
- type: Command
133+
name: "Install Maven"
134+
command: |
135+
echo "Download maven ${MVN_VERSION} and install in $HOME/${SW_PATH}"
136+
cd $HOME/${SW_PATH}
137+
echo "Current $HOME/$SW_PATH before download contents are"
138+
ls -l .
139+
echo "Using curl co download maven, command is "
140+
echo curl https://dlcdn.apache.org/maven/maven-3/${MVN_VERSION}/binaries/apache-maven-${MVN_VERSION}-bin.tar.gz -o maven.tar.gz
141+
curl https://dlcdn.apache.org/maven/maven-3/${MVN_VERSION}/binaries/apache-maven-${MVN_VERSION}-bin.tar.gz -o maven.tar.gz
142+
echo "unpack maven"
143+
tar xf maven.tar.gz
144+
echo "Move maven in to place"
145+
mv apache-maven-* maven
146+
echo "Remove doanload file"
147+
rm *.tar.gz
148+
echo "Configuring MVN_PATH"
149+
export MVN_HOME=$HOME/${SW_PATH}/maven
150+
echo "Installed maven version in ${MVN_PATH} is"
151+
${MVN_HOME}/bin/mvn --version
152+
- type: Command
153+
name: "Configure PATH for installed software"
154+
command: |
155+
echo "Using software in system locations and $SW_PATH"
156+
echo "Before updates PATH is $PATH"
157+
PATH=PATH=${JAVA_HOME}/bin:${MVN_HOME}/bin:$PATH
158+
echo "After updates PATH is $PATH"
159+
- type: Command
160+
name: "Validating software versions"
161+
command: |
162+
echo "Confirming downloaded software versions using PATH"
163+
echo "java -version"
164+
java -version
165+
echo "mvn --version"
166+
mvn --version
167+
- type: Command
168+
name: "Extracting the artifact id and version information"
169+
command: |
170+
echo "Extracting artifact ID and version from the pom.xml in ${WORKING_PATH}"
171+
cd ${WORKING_PATH}
172+
export EXTRACTED_ARTIFACT_ID=`xmllint --xpath '/*[local-name()="project"]/*[local-name()="artifactId"]/text()' pom.xml`
173+
export EXTRACTED_VERSION=`xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml`
174+
echo "Extracted artifact ID is ${EXTRACTED_ARTIFACT_ID}"
175+
echo "Extracted version is ${EXTRACTED_VERSION}"
176+
- type: Command
177+
name: "Compile and build the application"
178+
command: |
179+
echo "Compiling in ${WORKING_PATH}"
180+
cd ${WORKING_PATH}
181+
echo "Starting compile and native imge process, this may take a while"
182+
./mvnw package -Dnative
183+
- type: Command
184+
name: "Locating resulting output"
185+
command: |
186+
cd ${WORKING_PATH}
187+
export APPLICATION_COMPILED_NAME=${EXTRACTED_ARTIFACT_ID}-${EXTRACTED_VERSION}-runner
188+
export APPLICATION_COMPILED_PATH=./target/${APPLICATION_COMPILED_NAME}
189+
echo "Application compiled path = ${APPLICATION_COMPILED_PATH}"
190+
echo "Details are :"
191+
ls -l ${APPLICATION_COMPILED_PATH}
192+
- type: Command
193+
name: "Build docker image"
194+
command: |
195+
echo "Building docker image in ${WORKING_PATH} contents are"
196+
cd ${WORKING_PATH}
197+
ls -l
198+
echo "Using ${APPLICATION_COMPILED_PATH} as the appSource"
199+
echo "Docker command to execute is :"
200+
echo docker build --build-arg BASE_IMAGE=${DOCKER_BASE_IMAGE} --build-arg APP_DIR=${DOCKER_APPLICATION_DIR} --build-arg APP_SOURCE=${APPLICATION_COMPILED_PATH} --build-arg APP_NAME=${APPLICATION_COMPILED_NAME} --file Dockerfile --tag ${EXTRACTED_ARTIFACT_ID}:${EXTRACTED_VERSION} .
201+
docker build --build-arg BASE_IMAGE=${DOCKER_BASE_IMAGE} --build-arg APP_DIR=${DOCKER_APPLICATION_DIR} --build-arg APP_SOURCE=${APPLICATION_COMPILED_PATH} --build-arg APP_NAME=${APPLICATION_COMPILED_NAME} --file Dockerfile --tag ${EXTRACTED_ARTIFACT_ID}:${EXTRACTED_VERSION} .
202+
203+
outputArtifacts:
204+
- name: application-container-image
205+
type: DOCKER_IMAGE
206+
location: ${EXTRACTED_ARTIFACT_ID}:${EXTRACTED_VERSION}

0 commit comments

Comments
 (0)