Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Jenkins Docker Image with pre configured jobs

I have created a bunch of Local deployment pipeline jobs, these jobs do things like remove an existing container, build a service locally, build a docker image, run the container - etc. These are not CI/CD jobs, just small pipelines for deploying locally during dev.

What I want to do now is make this available to all our devs, so they can just simply spin up a local instance of jenkins that already contains the jobs.

My docker file is reasonably straight forward...

FROM jenkins:latest

USER root

RUN apt-get update
RUN apt-get install -y sudo

RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers

# Docker
RUN apt-get update
RUN apt-get dist-upgrade -y
RUN apt-get install apt-transport-https ca-certificates -y
RUN sh -c "echo deb https://apt.dockerproject.org/repo debian-jessie main > /etc/apt/sources.list.d/docker.list"
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
RUN apt-get update
RUN apt-cache policy docker-engine
RUN apt-get install docker-engine -y

# .NET Core CLI dependencies
RUN echo "deb [arch=amd64] http://llvm.org/apt/jessie/ llvm-toolchain-jessie-3.6 main" > /etc/apt/sources.list.d/llvm.list \
    && wget -q -O - http://llvm.org/apt/llvm-snapshot.gpg.key|apt-key add - \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        clang-3.5 \
        libc6 \
        libcurl3 \
        libgcc1 \
        libicu52 \
        liblldb-3.6 \
        liblttng-ust0 \
        libssl1.0.0 \
        libstdc++6 \
        libtinfo5 \
        libunwind8 \
        libuuid1 \
        zlib1g \
    && rm -rf /var/lib/apt/lists/*

#DotNetCore
RUN curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?linkid=847105
RUN mkdir -p /opt/dotnet && tar zxf dotnet.tar.gz -C /opt/dotnet
RUN ln -s /opt/dotnet/dotnet /usr/local/bin

# Minimal Jenkins Plugins
RUN /usr/local/bin/install-plugins.sh git matrix-auth workflow-aggregator docker-workflow blueocean credentials-binding

# Skip initial setup
ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false

COPY LocallyDeployIdentityConfig.xml /var/jenkins_home/jobs/identity/config.xml

USER jenkins

What I thought I could do is simply copy a job config file into the /jobs/jobname folder and the job would appear, but not only does this not appear, but now I cannot manually create jobs either. I now get a java.io.IOException "No such file or directory" - Note when I exec into the running container, the job and jobname directories exist and my config file is in there.

Any ideas?

like image 435
Tim Jarvis Avatar asked Apr 29 '17 03:04

Tim Jarvis


People also ask

How do I build a dockerfile using Jenkins?

This Dockerfile is built from Jenkins official image, install docker and give access to user jenkins build dockers. To get the jenkins docker build, let's run the following command: $ docker build -t jenkins-with-docker .

How do I build an image from a Jenkins job?

To build it, press Build Now. After a few minutes you should see an image appear in your Docker Hub repository, and something like this on the page of your new Jenkins job: We have successfully containerised an application, and set up a Jenkins job to build and publish the image on every change to a repository.

How do I build an image from a dockerfile?

To build the image on your own computer, navigate to the project directory (the one with your application code and the Dockerfile), and run docker build: docker build . -t getintodevops-hellonode:1 This instructs Docker to build the Dockerfile in the current directory with the tag getintodevops-hellonode:1.

How does the Jenkins cloud plugin work?

The plugin is based on a Jenkins Cloud plugin. When a build requires Docker, it will create a "Cloud Agent" via the plugin. The agent will be a Docker Container configured to talk to our Docker Daemon. The Jenkins build job will use this container to execute the build and create the image before being stopped.


3 Answers

I maintain the jobs in a bootstrap folder together with configs etc.

To add a job (i.e. seedjob) I need to add the following to the Dockerfile:

# copy seedjob
COPY bootstrap/seedjob.xml /usr/share/jenkins/ref/jobs/seedjob/config.xml
like image 77
moin moin Avatar answered Oct 17 '22 09:10

moin moin


For anyone who is interested - I found a better solution. I simply map the jobs folder to a folder on my host, that way I can put the created jobs into source control and edit then add them without having to build a new docker image.

Sorted.

like image 13
Tim Jarvis Avatar answered Oct 17 '22 10:10

Tim Jarvis


Jobs need to bootstrapped while the Jenkins starts can be copied to /usr/share/jenkins/ref/jobs/ folder.

But keep in mind that if the jobs(or any) already exist in Jenkins home folder, updates from /usr/share/jenkins/ref/jobs/ folder won't have any effect unless you end the files with *.override name. https://github.com/jenkinsci/docker/blob/master/jenkins-support#L110

Dockerfile

# First time building of jenkins with the preconfigured job
COPY job_name/config.xml /usr/share/jenkins/ref/jobs/job_name/config.xml

# But if jobs need to be updated, suffix the file names with '.override'.
COPY job_name/config.xml.override /usr/share/jenkins/ref/jobs/job_name/config.xml.override
like image 7
Vaisakh Rajagopal Avatar answered Oct 17 '22 09:10

Vaisakh Rajagopal