Jenkins JCaC

In this article, we will automate no touch Jenkins setup wizard on docker (jcac). We will use the Jenkins Code As Configuration (JCAC) to achieve the automation. When we install the Jenkins first time, after that through GUI (Graphical User Interface) we install plugins, set Jenkins admin credentials etc. So we will automate these steps.

With the help of Jenkins As Code Configuration, we can extend the automation in Jenkins. As per our current article agenda, we will do no touch first time Jenkins setup.

Prerequisites

Docker server should be up and running. If it is not available then install Docker server.

Steps to automate Jenkins setup

In this section follow the given below steps to setup the automation system. Here is some high level overview.

  1. Create JCAC (Jenkins Code As Configuration) file.
  2. Create Plugins list file which you want to install.
  3. Create Dockerfile.
  4. Create Docker Image.
  5. Run the Jenkins container.

Create directories for setup

Create directory called jenkins-jcac. Then change to this directory. Here in this directory, we will create all the files for creating a new Jenkins docker image.

sharad@server01:~$ mkdir jenkins-jcac
sharad@server01:~$ cd jenkins-jcac/
sharad@server01:~/jenkins-jcac$

Create JCAC (Jenkins Code As Configuration) file

Create a JCaC file. In casc.yaml file for creating the Jenkins admin credentials.

vi casc.yaml

Write the given below content in casc.yaml file and save it.

jenkins:
  securityRealm:
    local:
      allowsSignup: false
      users:
        - id: ${JENKINS_ADMIN_ID}
          password: ${JENKINS_ADMIN_PASSWORD}
  authorizationStrategy:
    globalMatrix:
      permissions:
        - "Overall/Administer:admin"
        - "Overall/Read:authenticated"

Create Plugins list file

Create a file for keep the list of plugin which we will install in the Jenkins container.

vi plugins.txt

We have added the Jenkins plugins name and their version (latest). In this list, important plugin for our automation is configuration-as-code. This plugin is important

configuration-as-code:latest
matrix-auth:latest
ant:latest
antisamy-markup-formatter:latest
build-timeout:latest
cloudbees-folder:latest
credentials-binding:latest
email-ext:latest
git:latest
github-branch-source:latest
gradle:latest
ldap:latest
mailer:latest
matrix-auth:latest
pam-auth:latest
pipeline-github-lib:latest
pipeline-stage-view:latest
role-strategy:latest
ssh-slaves:latest
timestamper:latest
workflow-aggregator:latest
ws-cleanup:latest

Create Dockerfile

Now create the Docker file. Use any text editor to create a Dockerfile inside this directory. On terminal we prefer to use vi/vim commands for creating file. Hence, in this example it is the same.

vi Dockerfile

Write the given below content in the Dockerfile and save it.

FROM jenkins/jenkins:lts-jdk17

# set environment
ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false"
ENV JENKINS_HOME="/var/jenkins_home"
ENV JENKINS_OPTS="--prefix=/jenkins"
ENV CASC_JENKINS_CONFIG="/var/jenkins_home/casc.yaml"

COPY --chown=jenkins:jenkins plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt

COPY casc.yaml /var/jenkins_home/casc.yaml

FROM jenkins/jenkins:lts-jdk17 : This line tells that we are using Jenkins docker image version jenkins-lts-jdk17 from jenkins docker repo.

ENV JAVA_OPTS=”-Djenkins.install.runSetupWizard=false” : This line is for setting Environment JAVA_OPTS value.It is important line for our setup. As per the JAVA_OPTS value install the Jenkins without setup wizard.

ENV JENKINS_HOME=”/var/jenkins_home: This is for setting Environment called JENKINS_HOME. Its value will help Jenkins to setup its home directory path.

ENV JENKINS_OPTS=”–prefix=/jenkins: Runs Jenkins to include the $PREFIX at the end of the URL. As per this configuration, in Jenkins URL at the end you will find /jenkins .

ENV CASC_JENKINS_CONFIG=”/var/jenkins_home/casc.yaml”: This ENV settings value refer file path of Jenkins Code As Configuration (JCaC) file.

COPY –chown=jenkins:jenkins plugins.txt /usr/share/jenkins/ref/plugins.txt : This will copy the plugins.txt file to Docker image.

RUN jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt: Run this command to install plugins as per listed in plugins.txt file.

COPY casc.yaml /var/jenkins_home/casc.yaml : Copy the JCaC file to Docker Image.

Before proceeding, just check all the files are created.

Create new Jenkins docker Image

Be there in the directory where we have created all the files. Now, execute the docker build command.

docker build -t jenkinsjcac:1.0 .

Check and validate the docker image is created.

docker images|grep jenkinsjcac

Run the Jenkins container

Now run the Jenkins container. Type the username and password for admin user. Here, in given below command the user name is admin and its password we have set as adminPassword .

We are running the Docker container in background (we have used the -d parameter in command line)

docker run -d --name jenkins -p 8080:8080 --env JENKINS_ADMIN_ID=admin --env JENKINS_ADMIN_PASSWORD=adminPassword jenkinsjcac:1.0

Login to Jenkins Dashboard

Now open the web browser and type the Jenkins server address with port number and prefix /jenkins. The Jenkins server is ready to use now.

For example:

http://192.168.123.92:8080/jenkins/

You will find the Jenkins dashboard, type admin credentials which you have provided during docker run command.

You will get your first view of dashboard after login.

Now let’s explore the plugin which we have provided the list prior creating the new Jenkins Docker image.

Click on ‘Manage jenkins’ >> ‘Plugins’ >> ‘Installed Plugins’

You will find all the plugins installed in this section.

You can start using the Jenkins and do further settings.

Tips

To know about the Jenkins Code As Confuguration what to write then check the reference from your own Jenkins link. The address format is given below. Replace the IP address with your Jenkins server ip address.

<ServerIP_address>:8080/configuration-as-code/reference

Conclusion

With the help of Jenkins Code As Configuration (JCaC), now it is easy to automate the Jenkins setup. We can easily integrate this setup with other automation tool like Ansible, Puppet, Chef etc. Even the one can also explore the possibility to automate the Jenkins entire configuration in advanced level. As we have Docker Image ready, we can apply the same in Kubernetes with some configuration. Truly it is a game changer feature in Jenkins for automation perspective.

Read Some More Articles