Skip to content

Commit

Permalink
SpringBoot S2I Builder image
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemoralespou committed Mar 7, 2016
0 parents commit c6efaf4
Show file tree
Hide file tree
Showing 16 changed files with 1,124 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.git/
140 changes: 140 additions & 0 deletions .sti/bin/assemble
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash -e
#
# S2I assemble script for the 'springboot-sti' image.
# The 'assemble' script builds your application source ready to run.
#
# For more information refer to the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#

if [ "$1" = "-h" ]; then
# If the 'springboot-sti' assemble script is executed with '-h' flag,
# print the usage.
exec /usr/local/sti/usage
fi

# # Restore artifacts from the previous build (if they exist).
# #
# if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then
# echo "---> Restoring build artifacts"
# mv /tmp/artifacts/. ./
# fi
#
# echo "---> Installing application source"
# cp -Rf /tmp/src/. ./
#
# echo "---> Building application from source"
# # TODO: Add build steps for your application, eg npm install, bundle install

##############################################################################

# Source code provided to STI is at ${HOME}/source
HOME=/opt/app-root/src
LOCAL_SOURCE_DIR=${HOME}/source
mkdir -p $LOCAL_SOURCE_DIR

DEPLOY_DIR=/opt/openshift

# the subdirectory within LOCAL_SOURCE_DIR from where we should copy build artifacts
ARTIFACT_DIR=${ARTIFACT_DIR-target}

function copy_artifacts() {
if [ -d $LOCAL_SOURCE_DIR/$1 ]; then
echo "Copying all JAR artifacts from $LOCAL_SOURCE_DIR/$1 directory into $DEPLOY_DIR for later deployment..."
cp -v $LOCAL_SOURCE_DIR/$1/*.jar $DEPLOY_DIR 2> /dev/null
fi
}

# Copy the source for compilation
cp -ad /tmp/src/* $LOCAL_SOURCE_DIR

# If a pom.xml is present, this is a normal build scenario
# so run maven.
if [ -f "$LOCAL_SOURCE_DIR/pom.xml" ]; then
echo "Building with maven. $LOCAL_SOURCE_DIR/pom.xml found."
pushd $LOCAL_SOURCE_DIR &> /dev/null

if [ -z "$BUILDER_ARGS" ]; then
export BUILDER_ARGS="package -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga"
fi

echo "Found pom.xml... attempting to build with 'mvn -e ${BUILDER_ARGS}'"

echo "Maven version:"
mvn --version

# Execute the actual build
mvn -e $BUILDER_ARGS

ERR=$?
if [ $ERR -ne 0 ]; then
echo "Aborting due to error code $ERR from Maven build"
exit $ERR
fi

# Copy built artifacts (if any!) from the target/ directory
# to the $DEPLOY_DIR directory for later deployment
copy_artifacts target

# clean up after maven
mvn clean
if [ -d "$HOME/.m2/repository" ]; then
rm -r "$HOME/.m2/repository"
fi

popd &> /dev/null
elif [ -f "$LOCAL_SOURCE_DIR/build.gradle" ]; then
echo "Building with gradle. $LOCAL_SOURCE_DIR/build.gradle found."

pushd $LOCAL_SOURCE_DIR &> /dev/null

if [ -z "$BUILDER_ARGS" ]; then
export BUILDER_ARGS="build -x test"
# TODO: Specify setting file with -c sss
fi

echo "Found gradle.build ... attempting to build with 'gradle -s ${BUILDER_ARGS}'"

echo "Gradle version:"
gradle --version

# Execute the actual build
gradle -s $BUILDER_ARGS

ERR=$?
if [ $ERR -ne 0 ]; then
echo "Aborting due to error code $ERR from Gradle build"
exit $ERR
fi

# Copy built artifacts (if any!) from the target/ directory
# to the $DEPLOY_DIR directory for later deployment
copy_artifacts build/libs


# clean up after maven
gradle clean
# if [ -d "$HOME/.m2/repository" ]; then
# rm -r "$HOME/.m2/repository"
# fi

popd &> /dev/null
fi

# Copy (probably binary) artifacts from the deployments/
# directory to the $DEPLOY_DIR directory for later deployment
# copy_artifacts "deployments"

# if [ -d $LOCAL_SOURCE_DIR/configuration ]; then
# echo "Copying config files from project..."
# cp -v $LOCAL_SOURCE_DIR/configuration/* $ARTIFACTS_HOME
# fi

# As SpringBoot you should only have 1 fat jar
if [ $(ls /opt/openshift/*.jar | wc -l) -eq 1 ]; then
mv /opt/openshift/*.jar /opt/openshift/app.jar
[ ! -f /opt/openshift/app.jar ] && echo "Application could not be properly built." && exit 1
echo "Application jar file is located in /opt/openshift/app.jar"
else
exit 1
fi
10 changes: 10 additions & 0 deletions .sti/bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash -e
#
# S2I run script for the 'springboot-sti' image.
# The run script executes the server that runs your application.
#
# For more information see the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#

exec java -Djava.security.egd=file:/dev/./urandom -jar /opt/openshift/app.jar $APP_OPTIONS
10 changes: 10 additions & 0 deletions .sti/bin/save-artifacts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh -e
#
# S2I save-artifacts script for the 'springboot-sti' image.
# The save-artifacts script streams a tar archive to standard output.
# The archive contains the files and folders you want to re-use in the next build.
#
# For more information see the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
# tar cf - <list of files and folders>
12 changes: 12 additions & 0 deletions .sti/bin/usage
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash -e
cat <<EOF
This is the springboot-sti S2I image:
To use it, install S2I: https://github.com/openshift/source-to-image
Sample invocation:
sti build git://<source code> springboot-sti <application image>
You can then run the resulting image via:
docker run <application image>
EOF
54 changes: 54 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# springboot-sti
FROM openshift/base-centos7
MAINTAINER Jorge Morales <[email protected]>

# Install build tools on top of base image
# Java jdk 8, Maven 3.3, Gradle 2.6
RUN yum install -y --enablerepo=centosplus \
tar unzip bc which lsof java-1.8.0-openjdk java-1.8.0-openjdk-devel && \
yum clean all -y && \
mkdir -p /opt/openshift && \
mkdir -p /opt/app-root/source && chmod -R a+rwX /opt/app-root/source && \
mkdir -p /opt/s2i/destination && chmod -R a+rwX /opt/s2i/destination && \
mkdir -p /opt/app-root/src && chmod -R a+rwX /opt/app-root/src

ENV MAVEN_VERSION 3.3.9
RUN (curl -0 http://www.eu.apache.org/dist/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz | \
tar -zx -C /usr/local) && \
mv /usr/local/apache-maven-$MAVEN_VERSION /usr/local/maven && \
ln -sf /usr/local/maven/bin/mvn /usr/local/bin/mvn

ENV GRADLE_VERSION 2.6
RUN curl -sL -0 https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip -o /tmp/gradle-${GRADLE_VERSION}-bin.zip && \
unzip /tmp/gradle-${GRADLE_VERSION}-bin.zip -d /usr/local/ && \
rm /tmp/gradle-${GRADLE_VERSION}-bin.zip && \
mv /usr/local/gradle-${GRADLE_VERSION} /usr/local/gradle && \
ln -sf /usr/local/gradle/bin/gradle /usr/local/bin/gradle

ENV PATH=/opt/maven/bin/:/opt/gradle/bin/:$PATH

ENV BUILDER_VERSION 1.0

LABEL io.k8s.description="Platform for building Spring Boot applications with maven or gradle" \
io.k8s.display-name="Spring Boot builder 1.0" \
io.openshift.expose-services="8080:http" \
io.openshift.tags="builder,maven-3,gradle-2.6,springboot"

# TODO (optional): Copy the builder files into /opt/openshift
# COPY ./<builder_folder>/ /opt/openshift/
# COPY Additional files,configurations that we want to ship by default, like a default setting.xml

LABEL io.openshift.s2i.scripts-url=image:///usr/local/sti
COPY ./.sti/bin/ /usr/local/sti

RUN chown -R 1001:1001 /opt/openshift

# This default user is created in the openshift/base-centos7 image
USER 1001

# Set the default port for applications built using this image
EXPOSE 8080

# Set the default CMD for the image
# CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/opt/openshift/app.jar"]
CMD ["usage"]
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

IMAGE_NAME = springboot-sti

build:
docker build -t $(IMAGE_NAME) .

.PHONY: test
test:
docker build -t $(IMAGE_NAME)-candidate .
IMAGE_NAME=$(IMAGE_NAME)-candidate BUILDER=maven test/run
IMAGE_NAME=$(IMAGE_NAME)-candidate BUILDER=gradle test/run
43 changes: 43 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
== OpenShift S2I Builder for Spring Boot
This Source-to-Image Builder let's you create projects targetting Spring Boot and built with:

* maven
* gradle
NOTE: If a project has a pom.xml and a build.gradle, maven will take precedence

== ENV Options

* *BUILDER_ARGS*: Allows you to specify options to pass to maven or gradle


== Defaults
If you do not specify any BUILDER_ARGS, by default the s2i image will use the following:

* Maven

----
BUILDER_ARGS="package -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga"
----

* Gradle

----
BUILDER_ARGS="build -x test"
----

== Test in OpenShift

* First load all the needed resources in a project.

----
oc create -f https://raw.githubusercontent.com/jorgemoralespou/osev3-examples/master/spring-boot/springboot-sti/springboot-sti-all.json
----

* Once the builder springboot-sti has finished building, you can create an app with:

** Instant app already provided as template
** Using the springboot-sti builder image using a regular Git repository

== Samples
There is a lot of example SpringBoot applications https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples[here]
Loading

0 comments on commit c6efaf4

Please sign in to comment.