VOOZH about

URL: https://devops.com/using-jenkins-configuration-as-code/

⇱ Using Jenkins Configuration as Code - DevOps.com


Sign up for our newsletter!
Stay informed on the latest DevOps news

Using the following code, we can configure Jenkins Master to integrate Sonar, Nexus and Jira and manage them as a code. Code as configuration is a plugin in Jenkins, which is very popular and simple to use.

First, we need to install plugins from Jenkins Market place.

Manage Jenkins > Manage Plugins > Advance

πŸ‘ Image

After installing, got to Manage Jenkins and you will find Configuration as Code settings.

πŸ‘ Image

After opening this link, it will give us the Jenkins Configuration as Code screen.

πŸ‘ Image
Here, you have two steps:

  1. URL or Path.
  2. Export the Existing configuration.

Provide URL or Path

We have two options for storing Jenkins.yaml file:

  1. Any directory Jenkins is running
  2. Version Control (such as Git)

If we go with option one, then we need to place Jenkins.yaml file in JENKINS_HOME directory, place that directory path and click on β€œApply new configuration.”

πŸ‘ Image

If we go for version controlling, we need a place for the Jenkins.yaml file and then we need to open the file in a raw format in GitLab, as shown below:

πŸ‘ Image

Now, place that link and click on Apply new Configuration.

πŸ‘ Image

Export the Existing Configuration

Export the current configuration into Jenkins.yaml, click the Export Configuration button to download the Jenkins.yaml file.

πŸ‘ Image

After opening this file, you may see some errors. Keep in mind, this is still in under development so the export function is now working properly.

πŸ‘ Image

We can write Jenkins.yaml file in simple YAML format.

Plugins

This is what the YAML format looks like:

πŸ‘ Image

Let’s move to next step tool’s installation.

Tool installations

This is what it would look like during the tool installation:

πŸ‘ Image

Tools Integration

πŸ‘ Image

The next step is the pipeline job configuration.

Setting Up Job with JCasC

πŸ‘ Image

Now, let’s bring everything together and see what happens in Jenkins:

πŸ‘ Image

After applying the new configuration, it will show the date, time and path of the configuration.

πŸ‘ Image

If there is an error in syntax format, it will throw the error as shown below:

πŸ‘ Image

Rest API Method to Submit the Configuration

Here, officially we have JCasC. There is a way to apply changes through CURL.

Here we have two CURL commands:

  1. Validation
  2. Applying the new

Validation

Here, I am using Jenkins YAML to store in version controlling tool with public repo:

πŸ‘ Image

Response will be as shown below:

πŸ‘ Image

Applying new Configuration

πŸ‘ Image

β€” Santosh Rahul Goru