|
| 1 | +title: Jenkins X - Create Builder Image |
| 2 | +description: How to create a Jenkins X builder Image |
| 3 | + |
| 4 | +# Create Jenkins X Builder Image |
| 5 | + |
| 6 | +Jenkins X leverages [Tekton pipelines](https://github.com/tektoncd/pipeline) to create a Kubernetes native Pipeline experience. Every step is run in its own container. |
| 7 | + |
| 8 | +People comonly start using Jenkins X via its pre-defined build packs. |
| 9 | +These build packs already have a default Container Image defined, and use some specific containers for certain specific tasks - such as Kaniko for building Container Images. We call these Container Images: ***Builders***. |
| 10 | + |
| 11 | +## Create Custom Builder |
| 12 | + |
| 13 | +Sometimes you need to use a different container for a specific step. First, [look at the available builder](https://github.com/jenkins-x/jenkins-x-builders). If what you need does not exist yet, you will have to create one yourself. |
| 14 | + |
| 15 | +Jenkins X [has a guide on how to create a custom Builder](https://jenkins-x.io/docs/guides/managing-jx/common-tasks/create-custom-builder/). |
| 16 | + |
| 17 | +In essence, you create a Container Image that extends from `gcr.io/jenkinsxio/builder-base:0.0.81`, includes your tools and packages of choice, and may or may not include the `jx` binary. |
| 18 | + |
| 19 | +What comes after, is your choice. You can add [your Builder to Jenkins X's list of Builders](https://jenkins-x.io/docs/guides/managing-jx/common-tasks/create-custom-builder/#install-the-builder), or directly use it in your Jenkins X Pipeline by FQN. |
| 20 | + |
| 21 | +The main difference, is that when you add the Builder to Jenkins X you can include default configuration for the entire Pod. Otherwise, you have to specify any unique configuration in the Jenkins X Pipeline where you use the image. |
| 22 | + |
| 23 | +## Dockerfile Example |
| 24 | + |
| 25 | +!!! example "Dockerfile" |
| 26 | + |
| 27 | + ```Dockerfile |
| 28 | + FROM gcr.io/jenkinsxio/builder-base:0.0.81 |
| 29 | + |
| 30 | + RUN yum install -y java-11-openjdk-devel && yum update -y && yum clean all |
| 31 | + |
| 32 | + # Maven |
| 33 | + ENV MAVEN_VERSION 3.6.3 |
| 34 | + RUN curl -f -L https://repo1.maven.org/maven2/org/apache/maven/apache-maven/$MAVEN_VERSION/apache-maven-$MAVEN_VERSION-bin.tar.gz | tar -C /opt -xzv |
| 35 | + ENV M2_HOME /opt/apache-maven-$MAVEN_VERSION |
| 36 | + ENV maven.home $M2_HOME |
| 37 | + ENV M2 $M2_HOME/bin |
| 38 | + ENV PATH $M2:$PATH |
| 39 | + |
| 40 | + # GraalVM |
| 41 | + ARG GRAAL_VERSION=20.0.0 |
| 42 | + ENV GRAAL_CE_URL=https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${GRAAL_VERSION}/graalvm-ce-java11-linux-amd64-${GRAAL_VERSION}.tar.gz |
| 43 | + ARG INSTALL_PKGS="gzip" |
| 44 | + |
| 45 | + ENV GRAALVM_HOME /opt/graalvm |
| 46 | + ENV JAVA_HOME /opt/graalvm |
| 47 | + |
| 48 | + RUN yum install -y ${INSTALL_PKGS} && \ |
| 49 | + ### Installation of GraalVM |
| 50 | + mkdir -p ${GRAALVM_HOME} && \ |
| 51 | + cd ${GRAALVM_HOME} && \ |
| 52 | + curl -fsSL ${GRAAL_CE_URL} | tar -xzC ${GRAALVM_HOME} --strip-components=1 && \ |
| 53 | + ### Cleanup |
| 54 | + yum clean all && \ |
| 55 | + rm -f /tmp/graalvm-ce-amd64.tar.gz && \ |
| 56 | + rm -rf /var/cache/yum |
| 57 | + ### |
| 58 | + |
| 59 | + ENV PATH $GRAALVM_HOME/bin:$PATH |
| 60 | + RUN gu install native-image |
| 61 | + |
| 62 | + # jx |
| 63 | + ENV JX_VERSION 2.1.30 |
| 64 | + RUN curl -f -L https://github.com/jenkins-x/jx/releases/download/v${JX_VERSION}/jx-linux-amd64.tar.gz | tar xzv && \ |
| 65 | + mv jx /usr/bin/ |
| 66 | + |
| 67 | + CMD ["mvn","-version"] |
| 68 | + ``` |
| 69 | + |
| 70 | +## Usage |
| 71 | + |
| 72 | +The example above is my [Jenkins X Builder for Maven + JDK 11 + GraalVM](https://github.com/joostvdg/jenkins-x-builders/tree/master/graalvm-maven-jdk11). My Dockerhub ID is `caladreas`, and the image is `jx-builder-graalvm-maven-jdk11`. |
| 73 | + |
| 74 | +### Override Default Container |
| 75 | + |
| 76 | +To use this Container Image as the default container: |
| 77 | + |
| 78 | +!!! example "jenkins-x.yml" |
| 79 | + |
| 80 | + ```yaml |
| 81 | + buildPack: maven-java11 |
| 82 | + pipelineConfig: |
| 83 | + agent: |
| 84 | + image: caladreas/jx-builder-graalvm-maven-jdk11:v0.7.0 |
| 85 | + ``` |
| 86 | + |
| 87 | +### Override Step Container |
| 88 | + |
| 89 | +!!! example "jenkins-x.yml" |
| 90 | + |
| 91 | + ```yaml hl_lines="10" |
| 92 | + buildPack: maven-java11 |
| 93 | + pipelineConfig: |
| 94 | + pipelines: |
| 95 | + overrides: |
| 96 | + - pipeline: pullRequest |
| 97 | + stage: build |
| 98 | + name: mvn-install |
| 99 | + steps: |
| 100 | + - name: mvn-deploy |
| 101 | + image: caladreas/jx-builder-graalvm-maven-jdk11:v0.9.0 |
| 102 | + ``` |
0 commit comments