DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > Setting Up a Jenkins Instance With Configuration as Code (Using Yaml Configuration)

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

This article walks through how to set up a Jenkins instance with the Configuration as Code plugin using yaml.

Rodrigo Prates user avatar by
Rodrigo Prates
·
Jul. 10, 22 · DevOps Zone · Tutorial
Like (2)
Save
Tweet
6.06K Views

Join the DZone community and get the full member experience.

Join For Free

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: 

Shell
 
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.

YAML
 
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
Groovy
 

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.

Run a Pipeline

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


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


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.

Jenkins (software)

Published at DZone with permission of Rodrigo Prates. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Highlights from the New NIST SSDF
  • The Best Infrastructure as Code Tools for 2022
  • Getting Started With Nose in Python
  • How To Build a Multi-Zone Java App in Days With Vaadin, YugabyteDB, and Heroku

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo