我可以为从詹金斯管道创建的管道作业重用 scm 吗?

时间:2021-03-05 13:39:29

标签: jenkins jenkins-pipeline jenkins-groovy jenkins-job-dsl

在我的 jenkins 管道脚本 (Jenkinsfile) 中,我正在使用 jobDsl 步骤创建 pipelineJob:s,如下所示:

pipeline {
  agent any
  stages {
    stage('create pipelines') {
      steps {
        jobDsl scriptText: """
          pipelineJob('myfolder/myname') {
            definition {
              cps {
                script(readFileFromWorkspace('Jenkinsfile.subfolder')
              }
            }
          }
        """
      }
    }
  }
}

上面的代码运行良好,但我相信这些工作最好从 SCM 获取他们的 Jenkinsfile。

cpsScm 变体,但如何重用当前管道中的 scm-info 以便不必单独指定每个参数?

我想要一些类似的东西:

// ...
// Note: this does not work, sadly :)

jobDsl scriptText: """
  pipelineJob('myfolder/myname') {
    definition {
     cpsScm = ${scm}
     scriptPath('Jenkinsfile.subfolder')
    }
  }
"""

// ...

到目前为止,我想出了:

cpsScm {
  scm {
    git {
      remote {
        url('${scm.getRepositories()[0].getURIs()[0].toString()}')
        credentials('bitbucket-jenkins')
      }
      branch('${env.BRANCH_NAME}')
    }
  }
  scriptPath('${pipelineFile}')
}

0 个答案:

没有答案