Job DSL to create "Pipeline" type job

Viewed 39042
5 Answers

First you need to install Job DSL plugin and then create a freestyle project in jenkins and select Process job DSLs from the dropdown in the build section.

Select Use the provided DSL script and provide following script.

pipelineJob('job-name') {
  definition {
    cps {
      script('''
        pipeline {
          agent any
          stages {
            stage('Stage name 1') {
              steps {
                // your logic here
              }
            }
            stage('Stage name 2') {
              steps {
                // your logic here
              }
            }
          }
        }
      }
    ''')   
    }
  }
}

Or you can create your job by pointing the jenkinsfile located in remote git repository.

pipelineJob("job-name") {
  definition {
    cpsScm {
      scm {
        git {
          remote {
            url("<REPO_URL>")
            credentials("<CREDENTIAL_ID>")
          }
          branch('<BRANCH>')
        }
      }
      scriptPath("<JENKINS_FILE_PATH>")
    }
  }
}
Related