Job DSL to create “Pipeline” type job

徘徊边缘 提交于 2019-11-30 04:42:47

You should use pipelineJob:

pipelineJob('job-name') {
  definition {
    cps {
      script('logic-here')
      sandbox()
    }
  }
}

You can define the logic by inlining it:

pipelineJob('job-name') {
  definition {
    cps {
      script('''
        pipeline {
            agent any
                stages {
                    stage('Stage 1') {
                        steps {
                            echo 'logic'
                        }
                    }
                    stage('Stage 2') {
                        steps {
                            echo 'logic'
                        }
                    }
                }
            }
        }
      '''.stripIndent())
      sandbox()     
    }
  }
}

or load it from a file located in workspace:

pipelineJob('job-name') {
  definition {
    cps {
      script(readFileFromWorkspace('file-seedjob-in-workspace.jenkinsfile'))
      sandbox()     
    }
  }
}

Example:

Seed-job file structure:

jobs
   \- productJob.groovy
logic
   \- productPipeline.jenkinsfile

then productJob.groovy content:

pipelineJob('product-job') {
  definition {
    cps {
      script(readFileFromWorkspace('logic/productPipeline.jenkinsfile'))
      sandbox()     
    }
  }
}

I believe this question is asking something how to use the Job DSL to create a pipeline job which references the Jenkinsfile for the project, and doesn't combine the job creation with the detailed step definitions as has been given in the answers to date. This makes sense: the Jenkins job creation and metadata configuration (description, triggers, etc) could belong to Jenkins admins, but the dev team should have control over what the job actually does.

@meallhour, is the below what you're after? (works as at Job DSL 1.64)

pipelineJob('DSL_Pipeline') {

  def repo = 'https://github.com/path/to/your/repo.git'

  triggers {
    scm('H/5 * * * *')
  }
  description("Pipeline for $repo")

  definition {
    cpsScm {
      scm {
        git {
          remote { url(repo) }
          branches('master', '**/feature*')
          scriptPath('misc/Jenkinsfile.v2')
          extensions { }  // required as otherwise it may try to tag the repo, which you may not want
        }

        // the single line below also works, but it
        // only covers the 'master' branch and may not give you
        // enough control.
        // git(repo, 'master', { node -> node / 'extensions' << '' } )
      }
    }
  }
}

Ref the Job DSL pipelineJob: https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob, and hack away at it on http://job-dsl.herokuapp.com/ to see the generated config.


This example worked for me. Here's another example based on what worked for me:

pipelineJob('Your App Pipeline') { 

  def repo = 'https://github.com/user/yourApp.git' 
  def sshRepo = 'git@git.company.com:user/yourApp.git' 

  description("Your App Pipeline") 
  keepDependencies(false) 

  properties{ 

    githubProjectUrl (repo) 
    rebuild { 
      autoRebuild(false) 
    } 
  } 

  definition { 

    cpsScm { 
      scm { 
        git { 
          remote { url(sshRepo) } 
          branches('master') 
          scriptPath('Jenkinsfile') 
          extensions { }  // required as otherwise it may try to tag the repo, which you may not want 
        } 
      } 
    } 
  }

If you build the pipeline first through the UI, you can use the config.xml file and the Jenkins documentation https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob to create your pipeline job.

In Job DSL, pipeline is still called workflow, see workflowJob.

The next Job DSL release will contain some enhancements for pipelines, e.g. JENKINS-32678.

If you are using a git repo, add a file called Jenkinsfile at the root directory of your repo. This should contain your job dsl.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!