Parameterized Build Syntax for Jenkins Configuration as Code Plug-in (JCasC)

2.4k Views Asked by At

I'm trying to use the configuration as code (JCasC) plug-in to create a pipeline job that takes in build parameters but I can't find the syntax for this anywhere online. I'm writing the configuration in YAML.

On the GUI, the field is called "This build is paramertized" and it is under the 'General' heading. I need to define two string parameters: CLUSTER_ID=cluster_id and OPENSHIFT_ADMINSTRATION_BRANCH=develop.

This is the yaml file that I am trying to edit:

jobs:
  - script: >
      folder('test1'){
        pipelineJob('test1/seedJobTest') {
          description 'seedJobTest'
          logRotator {
            daysToKeep 10
          }
          definition {
            cpsScm {
              scm {
                git {
                  remote {
                    credentials "xxx"
                    url 'xxx'
                  }
                  branches 'refs/head/master'
                  scriptPath 'Jenkinsfile'
                  extensions { }
                }
              }
            }
          }
          configure { project ->
            project / 'properties' / 'EnvInjectJobProperty' {
              'on'('true')
            }
            project / 'properties' / 'org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty' {}
          }
        }
      }

Thanks for your help!

1

There are 1 best solutions below

0
Jamie Jackson On

Solution


jobs:
  - script: >
      folder('test1'){
        pipelineJob('test1/seedJobTest') {
          description 'seedJobTest'
          logRotator {
            daysToKeep 10
          }
          parameters {
            stringParam("CLUSTER_ID", "cluster_id", "your description here")
            stringParam("OPENSHIFT_ADMINSTRATION_BRANCH", "develop", "your description here")
          }
          definition {
            cpsScm {
              scm {
                git {
                  remote {
                    credentials "xxx"
                    url 'xxx'
                  }
                  branches 'refs/head/master'
                  scriptPath 'Jenkinsfile'
                  extensions { }
                }
              }
            }
          }
          configure { project ->
            project / 'properties' / 'EnvInjectJobProperty' {
              'on'('true')
            }
            project / 'properties' / 'org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty' {}
          }
        }
      }

How to Figure this Stuff Out in the Future - XML Job To DSL (Jenkins Plugin)

Here's how I would go about figuring this kind of thing:

  • Manually create a temporary pipeline job with the things you want in your seed job (the one you want to automate).
  • Install (if only temporarily) the "XML Job To DSL" Jenkins plugin.
  • Go to the main Jenkins Dashboard
  • In the left navigation, you'll find "XML Job To DSL." Click it.
  • Select the temporary job you created and click "Convert selected to DSL"

When I went about getting the params snippet for this answer, I did as I described above, but simply created two parameters. I ended up with this:

pipelineJob("test") {
    description()
    keepDependencies(false)
    parameters {
        stringParam("CLUSTER_ID", "cluster_id", "your description here")
        stringParam("OPENSHIFT_ADMINSTRATION_BRANCH", "develop", "your description here")
    }
    definition {
        cpsScm {
""      }
    }
    disabled(false)
}

Read-Only Parameter Option

One more thing, in case it's useful to you (as it was to me). If you want to create a parameterized seed job but you don't want those to be editable at build time, you can install the "Readonly Parameter" Jenkins plugin; then, you'll be able to do this kind of thing:

jobs:
  - script: >
      pipelineJob("Param Example") {
        description()
        keepDependencies(false)
        parameters {
          wHideParameterDefinition {
            name('AGENT')
            defaultValue('docker-host')
            description('Node on which to run.')
          }
          wHideParameterDefinition {
            name('ENV_FILE_DIR')
            defaultValue('local2')
            description('Name of environment directory which houses .env')
          }
          booleanParam("include_search_stack", false, "Build/run the local Fess, Elasticsearch, and Kibana containers.")
          booleanParam("SKIP_404_GENERATION", false, "Helpful sometimes during local development.")
        }
        definition {
          cpsScm {
            scm {
              git {
                remote {
                  url("https://myrepo/blah.git")
                  credentials("scm")
                }
                branch("master")
              }
            }
            scriptPath("pipeline/main/Jenkinsfile")
          }
        }
        disabled(false)
      }

In this example, the top two params, AGENT and ENV_FILE_DIR are sort of "hard-coded" from CasC, because the those parameters are not editable at build-time. However, the include_search_stack and SKIP_404_GENERATION parameters are editable. I used this mixed example to show that either/both are usable in the same job.

Read-only parameters have been useful in some of my use cases.