Index

Jenkins configuration as code

environment variables

ENV CASC_JENKINS_CONFIG=/path/to/configuration.yaml

config.yaml

setting global jenkins authorization strategy
jenkins:
  authorizationStrategy:
    globalMatrix:
      grantedPermissions:
        - "Overall/Administer:admin"
        - "Overall/Read:anonymous"
        - "View/Read:anonymous"
        - "Job/Read:anonymous"
        - "Job/Build:anonymous"
        - "Job/Cancel:authenticated"
configuring seed job
jobs:
  - file: "/data/jenkins/work/seed_job.groovy"
prevent jenkins from asking for approval for scripts
security:
  apiToken:
    creationOfLegacyTokenEnabled: false
    tokenGenerationOnCreationEnabled: false
    usageStatisticsEnabled: true
  globalJobDslSecurityConfiguration:
    useScriptSecurity: false
setting up an environment variable
globalNodeProperties:
    - envVars:
        env:
        - key: "[key]"
          value: "[value]"
config git
jenkins:
  scmCheckoutRetryCount: 5
tool:
  git:
    installations:
    - home: "/usr/bin/git"
      name: "git"

documentation

https://github.com/jenkinsci/configuration-as-code-plugin/blob/master/README.md

job-dsl

hello world job
#!/usr/bin/env groovy
job('hello-job') {
    displayName('Hello Job')
    steps {
        shell('echo "hello from \$PWD"')
    }   
}
now to make the seed job actually load worker jobs
#!/usr/bin/env groovy
job('seed-job') {
    displayName('Seed Job')
    steps {
        shell("cp -r /data/jenkins/jobs \$(pwd)")
        dsl {
            external('jobs/*.groovy')
            ignoreExisting(false)
            removeAction('DELETE')
            removeViewAction('DELETE')
            lookupStrategy('SEED_JOB')
        }
    }   
}
runnning a shell script from workspace
steps {
        shell(readFileFromWorkspace('shell/script.sh'))
    }
adding parameters to a job
parameters {
        stringParam('foo', 'bar', 'parameter description')
        choiceParam('choice', ['502', '508'], 'my first choice')
    }   
    steps {
        shell('echo "$foo = $choice"')
    }

cron jobs

every hour
triggers {
        cron("@hourly")
    }
cron syntax: {Minute} {Hour 0-23} {DayOfMonth} {Month} {DayofWeek}
// every minute
    triggers {
        cron("* * * * *")
    }
documentation jenkins cron syntax
https://jenkins.io/doc/book/pipeline/syntax/#triggers

scm

store jobs in git repository and automatically update jenkins
repo="https://example.com/jobs.git"
job('seed-job') {
        triggers {
                scm('@daily')
        }
        scm {
                git {
                        branch('master')
                        remote {
                                url(repo)
                        }
                        extensions {
                                cleanAfterCheckout()
                        }
                }
        }
        steps {
                dsl {
                        external('jobs/*.groovy')
                        ignoreExisting(false)
                        removeAction('DELETE')
                        removeViewAction('DELETE')
                        lookupStrategy('SEED_JOB')
                }
        }
}

documentation

jenkins dsl api
https://jenkinsci.github.io/job-dsl-plugin/
plugin
https://plugins.jenkins.io/job-dsl
https://github.com/jenkinsci/configuration-as-code-plugin/blob/master/README.md
groovy
https://groovy-lang.org/