31

我已经安装Pipeline Plugin了以前称为Workflow Plugin的。
https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin

我想知道如何使用 Job Dsl 创建和配置类型的作业Pipeline

在此处输入图像描述

4

5 回答 5

36

你应该使用pipelineJob

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

您可以通过内联来定义逻辑:

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

或从位于工作区的文件中加载它:

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

例子:

种子作业文件结构:

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

然后productJob.groovy内容:

pipelineJob('product-job') {
  definition {
    cps {
      script(readFileFromWorkspace('logic/productPipeline.jenkinsfile'))
      sandbox()     
    }
  }
}
于 2016-11-07T14:57:38.207 回答
32

我相信这个问题是在询问如何使用 Job DSL 创建一个引用项目的 Jenkinsfile 的管道作业,并且没有将作业创建与迄今为止的答案中给出的详细步骤定义结合起来。这是有道理的:Jenkins 作业创建和元数据配置(描述、触发器等)可能属于 Jenkins 管理员,但开发团队应该控制作业的实际作用。

@meallhour,下面是你想要的吗?(在 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' << '' } )
      }
    }
  }
}

参考 Job DSL pipelineJob: https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob,然后在http://job-dsl.herokuapp.com/上修改它以查看生成的配置.


这个例子对我有用。这是另一个基于对我有用的示例:

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 
        } 
      } 
    } 
  }

如果您首先通过 UI 构建管道,则可以使用 config.xml 文件和 Jenkins 文档https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob来创建您的管道作业。

于 2017-08-29T02:56:57.177 回答
6

在 Job DSL 中,管道仍称为工作流,请参见workflowJob

下一个 Job DSL 版本将包含对管道的一些增强,例如JENKINS-32678

于 2016-03-09T17:14:40.990 回答
1

首先,您需要安装 Job DSL 插件,然后在 jenkins 中创建一个 freestyle 项目,然后从 build 部分的下拉列表中选择 Process job DSLs。

选择使用提供的 DSL 脚本并提供以下脚本。

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
              }
            }
          }
        }
      }
    ''')   
    }
  }
}

或者您可以通过指向位于远程 git 存储库中的 jenkins 文件来创建您的作业。

pipelineJob("job-name") {
  definition {
    cpsScm {
      scm {
        git {
          remote {
            url("<REPO_URL>")
            credentials("<CREDENTIAL_ID>")
          }
          branch('<BRANCH>')
        }
      }
      scriptPath("<JENKINS_FILE_PATH>")
    }
  }
}
于 2020-06-26T17:37:33.067 回答
0

如果您使用的是 git 存储库,请在存储库的根目录中添加一个名为 Jenkinsfile 的文件。这应该包含您的工作 dsl。

于 2016-03-11T08:37:43.573 回答