【问题标题】:Job DSL to create "Pipeline" type job工作 DSL 创建“管道”类型的工作
【发布时间】:2016-06-24 04:58:30
【问题描述】:

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

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

【问题讨论】:

    标签: jenkins jenkins-plugins jenkins-pipeline jenkins-workflow jenkins-job-dsl


    【解决方案1】:

    你应该使用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()     
        }
      }
    }
    

    【讨论】:

    • -1 不要仅在 node 声明中运行 build。这将为管道分配一个完整的执行程序,它将等待触发的构建完成。充其量是浪费,最坏的情况是它会导致死锁。您可以跳过节点块,并且将动态分配一个享元执行器,而不会消耗一个完整的执行器。
    • @Diasiare 是的,你是对的。我更新了答案。谢谢你:)
    【解决方案2】:

    我相信这个问题是在询问如何使用 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 来创建您的管道作业。

    【讨论】:

    • 谢谢!在我看来,这应该是正确的答案。无论如何,这正是我想要的。我只想使用 Job DSL 插件来定义作业本身,然后在 Jenkinsfile 中有管道的实际代码。我不明白使用 DSL 插件定义管道有什么意义。
    • 如果我正在生成流水线作业的代码但想将其签入某个存储库并希望流水线作业从该存储库中读取其 Jenkinsfile,该怎么办?我该如何办理登机手续?这可能吗?
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      首先您需要安装 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>")
          }
        }
      }
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2016-11-07
          • 2015-10-28
          • 2018-01-01
          • 2018-07-18
          • 2022-10-25
          • 1970-01-01
          • 2012-04-12
          • 1970-01-01
          • 2018-06-20
          相关资源
          最近更新 更多