SECRET OF CSS

Setting Up a Jenkins Instance With Configuration as Code (Using Yaml Configuration)


Sometimes we want visibility, and we want to keep track of how our Jenkins instance is configured and parameterized. 

Making these changes from the Jenkins UI is pretty straightforward, but on the other hand, we don’t keep track of history changes, and we need to jump from one menu item to another to get the big picture of how the setup is as a whole.

According to Jenkins Configuration as Code plugin docs:

The Configuration as Code plugin is an opinionated way to configure Jenkins based on human-readable declarative configuration files. Writing such a file should be feasible without being a Jenkins expert, just translating into code a configuration process one is used to executing in the web UI.

In another article, we talked about the importance of using a Jenkinsfile to keep track of all pipeline changes.

Now, we can still have the same approach to set up our Jenkins instance with Configuration as Code, meaning we define and declare all instance parameters and configurations and keep track of any changes made to our CI/CD tool.

Let’s see below how it can be done in practice.

Use Case

Since we want a pretty simple Jenkins setup, let’s:

  • Start a fresh Jenkins installation
  • Define some environment variables
  • Define a JDK tool installation

Then we’ll setup a Jenkins pipeline with a Jenkinsfile that:

  • Prints some environment variables
  • Runs java --version to see the Java 11 version printed

Set Up a Jenkins Instance

To simplify, let’s start our Jenkins using Docker

docker pull jenkins/jenkins:lts-jdk11

docker run -p 8080:8080 -p 50000:50000 --restart=on-failure jenkins/jenkins:lts-jdk11

Now we are going to use the Configuration as Code plugin — and another one. 

Go to Manage Jenkins -> Manage Plugins -> Available and install the following plugins:

  • Configuration as Code
  • AdoptOpenJDK installer

Create Your Configuration Files

Now we need to declare the configurations for our Jenkins instance and also define how our pipeline is going to work.

You can create them in a public repository, like the way I’ve done here.

Create Your Configuration as Code yaml File (Jenkins Configs)

This will define two new global environment vars on Jenkins and set up a JDK11 tool.

jenkins:

  systemMessage: "Jenkins instance using Configuration as Code."

  globalNodeProperties:

  - envVars:

      env:

      - key: SOME_ENV_PATH

        value: "/path/to/somewhere"

      - key: AWS_REGION

        value: us-west-2

tool:

    jdk:

      installations:

        - name: jdk11

          home: "/jdk"

          properties:

            - installSource:

                installers:

                  - adoptOpenJdkInstaller:

                      id: "jdk-11.0.14+9"

Create Your Jenkinsfile (Pipeline Definition)

It will create two stages:

  1. Print the variables we defined
  2. Run the java version command to print its version

pipeline {

  agent any

  environment {

    somePath = "${env.SOME_ENV_PATH}"

    awsRegion = "${env.AWS_REGION}"

  }

  stages {

    stage('Print my stuff') {

      steps {

        echo "somePath environment var is [${somePath}]"

        echo "awsRegion environment var is [${awsRegion}]"

      }

    }

    stage("Check JAVA version") {

      steps {

        sh "java --version"

      }

    }



  }

  post {

    always {

      cleanWs()

    }

  }

}

Using Configuration as Code Plugin

Updating Your Jenkins Configurations

Whenever you want to update your configurations described on your yaml file:

  1. Update your yaml file with the changes
  2. Go to Manager Jenkins -> Configuration as Code:
    • Provide the path/URL to your yaml file (in my case, that’s the GitHub raw URL to the file)
    • Click Apply New Configuration

Then click View Configuration to see the latest configs.

16009705 screenshot from 2022 06 29 14 34 40

Run a Pipeline

Create a Jenkins Pipeline job type pointing to a Jenkinsfile (sample here):

16009707 screenshot from 2022 06 29 14 54 06

When clicking on Build, you will see the respective outputs on the Console log:

16009708 screenshot from 2022 06 29 15 07 16

16009709 screenshot from 2022 06 29 15 07 40

Conclusion

Making use of the Configuration as Code plugin definitely helps to keep track of any changes done to your Jenkins configurations and also gives visibility to everybody in your team/company.

You can still set up more complex things (as you can see some demos here) and also automate the way you update your Jenkins configurations (ironically, with a Jenkins pipeline).

Feel free to check the files used for this exercise on my GitHub repo here.



News Credit

%d bloggers like this: