Advanced Jenkins

Backup and Configuration Management

Use JCasC create jobs

In this lesson, we’ll explore how to leverage the Jenkins Configuration as Code (JCasC) plugin to define your Jenkins tools and pipeline jobs declaratively. You’ll learn:

  • Where to store your YAML configurations
  • How to configure authorization, clouds, and tools
  • How to automate pipeline job creation with the Job DSL plugin

The image shows a GitHub repository page for the "configuration-as-code-plugin" with a list of folders and files in the "demos" directory, along with commit messages and dates.

Examples in the demos Directory

Inside the plugin’s demos folder, you’ll find YAML samples for common Jenkins configurations.

1. Global Matrix Authorization Strategy

jenkins:
  authorizationStrategy:
    globalMatrix:
      permissions:
        - "USER:Overall/Read:anonymous"
        - "GROUP:Overall/Administer:authenticated"
        - "USER:Overall/Administer:admin"

2. Kubernetes Cloud Integration

The image shows a GitHub repository page for a Jenkins configuration-as-code plugin, specifically in the "kubernetes" directory, with a list of YAML files and a README.md file. The README section provides instructions for configuring a Kubernetes plugin.

jenkins:
  clouds:
    - kubernetes:
        name: "advanced-k8s-config"
        serverUrl: "https://advanced-k8s-config:443"
        serverCertificate: "serverCertificate"
        skipTlsVerify: true
        credentialsId: "advanced-k8s-credentials"
        namespace: "default"
        jenkinsUrl: "http://jenkins/"
        jenkinsTunnel: "jenkinsTunnel"
        containerCapStr: 42
        maxRequestsPerHostStr: 64
        retentionTimeout: 5
        connectTimeout: 10

3. Node.js Tool Installation

tool:
  nodejs:
    installations:
      - name: "NodeJS Latest"
        home: "" # required until nodejs-1.3.4 release (JENKINS-57508)
        properties:
          - installSource:
              installers:
                - nodeJSInstaller:
                    id: "12.11.1"
    npmPackagesRefreshHours: 48 # default is 72

4. Git and Maven Tool Installation

git:
  installations:
    - home: "git"
      name: "Default"

maven:
  installations:
    - name: "M398"
      properties:
        - installSource:
            installers:
              - maven:
                  id: "3.9.8"
mavenGlobalConfig:
  globalSettingsProvider: "standard"
  settingsProvider: "standard"

Save your consolidated YAML (e.g., /var/lib/jenkins/JENKINS_BACKUP/jenkins.yaml) and apply it via Manage Jenkins > Configuration as Code. After reloading, you should see your Kubernetes cloud and tools under Global Tool Configuration.


Defining Pipeline Jobs

The demos/jobs folder demonstrates how to create folders and pipeline jobs using the Job DSL plugin.

The image shows a GitHub repository page for the "configuration-as-code-plugin" with a focus on the "jobs" directory, displaying several YAML files and a README file. The README section below provides instructions on configuring seed jobs using the Job DSL plugin.

jobs:
  - script: >
      folder('testjobs')
  - script: >
      pipelineJob('testjobs/default-agent') {
        definition {
          cps {
            script("""
              pipeline {
                agent any
                stages {
                  stage('test') {
                    steps {
                      echo "hello"
                    }
                  }
                }
              }
            """.stripIndent())
          }
        }
      }

Warning

If the Job DSL plugin is not installed, you will see this exception:

The image shows a webpage with documentation related to Jenkins configuration, specifically focusing on Kubernetes integration settings. Various configuration options and descriptions are listed, such as server certificates and proxy settings.

io.jenkins.plugins.casc.UnknownConfiguratorException: No configurator for the following root elements: jobs

Plugin Requirements

PluginPurposeInstall Location
Configuration as CodeDeclarative Jenkins configuration via YAMLManage Jenkins > Manage Plugins
Job DSLDefine jobs using Domain Specific LanguageManage Jenkins > Manage Plugins
  1. Go to Manage Jenkins > Manage Plugins.
  2. Search for Job DSL and install it.
  3. Reload your JCasC configuration.

After installation, refresh your Jenkins Dashboard. You will now see a new folder testjobs containing the default-agent pipeline:

The image shows a Jenkins dashboard with a job named "default-agent" under the "testjobs" folder, displaying options for configuration and build history.

Inside default-agent, the pipeline script is:

pipeline {
  agent any
  stages {
    stage('test') {
      steps {
        echo "hello"
      }
    }
  }
}

Verifying Tool Configuration

  1. Navigate to Manage Jenkins > Global Tool Configuration.
  2. Confirm your Node.js installation under NodeJS.

The image shows a Jenkins configuration interface with dropdown menus for managing system settings, plugins, and other configurations. The interface is dark-themed and includes options like "Manage Jenkins" and "Configuration as Code."

  1. Inspect the NodeJS details:

The image shows a Jenkins configuration screen for managing tools, specifically for setting up NodeJS. It includes options for version selection, architecture settings, and global npm package installation.

You’ve now automated both your toolchain and pipeline jobs entirely with JCasC!


Watch Video

Watch video content

Previous
Configure and Explore JCasC