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.
Join the DZone community and get the full member experience.
Join For FreeSometimes 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.
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:
- Print the variables we defined
- 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:
- Update your yaml file with the changes
- 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.
Published at DZone with permission of Rodrigo Prates. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments