Search code examples
jenkinsgroovyjenkins-pipelinejenkins-clijenkins-job-dsl

How to add "single conditional steps" under build section using dsl script


I'm currently trying to develop a DSL script that can create a jenkins job with all required plugins and options. I think I've almost completed all the section. But, I stuck up under build section where I've to include "conditional steps (single)" under Build.

Actually what I wanted is this

But, what I get is this

Here's the code that I used,

job('Sample_dev') {
    steps {
        conditionalSteps {
            condition {
                alwaysRun()
            }
        }
        maven {
            goals('install')
        }
    }
}

Solution

  • You have done few mistakes there:

    • Using multi-step DSL for achieving single step.
    • Pushed maven outside context like individual step.
    • Wrong DSL for Maven Step declaration.

    Try following

    job('Sample_dev')
    {
      steps{
          singleConditionalBuilder{
            condition{
                alwaysRun()
            }
            buildStep {
              maven{
                targets('install')
                name('')
                pom('')
                properties('')
                jvmOptions('')
                usePrivateRepository(false)
                settings {
                    standard()
                }
                globalSettings {
                    standard()
                }
                injectBuildVariables(false)
              }
            }
            runner {
              fail()
            }
          }
       }
    }
    

    The creator has deployed most on this url https://jenkinsci.github.io/job-dsl-plugin. But I would suggest you install in you local instance and access it via http://<your-jenkins-host>:<port> /plugin/job-dsl/api-viewer/index.html as Job DSL support auto generation so there is bright chance that plugin not listed above still has DSL support.