如何在许多项目中共享声明性管道

pmr*_*pmr 5 jenkins jenkins-pipeline

我在不同的存储库中有很多项目,它们共享相同的基本CI工作流程,我可以很容易地表达为声明性管道:

pipeline {
  agent any

  options {
    buildDiscarder(logRotator(numToKeepStr: '20'))
  }

  stages {
    stage('CI') {
      steps {
        echo 'Do CI'
      }
    }

    stage('QA') {
      steps {
        echo 'Do QA'
      }
    }
  }

  post {
    always {
      junit allowEmptyResults: true, testResults: '**/target/surefire-reports/TEST-*.xml'
      // etc...
    }

    failure {
      echo 'Failure mail'
      // etc
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想在我的所有项目中使用相同的声明性管道,并且能够在一个地方更改管道的定义,并自动在所有项目中使用更改.

基本上我要在项目中做什么; s Jenkinsfile是这样的:

loadPipelineFromScm 'repository', 'pipeline.groovy'
Run Code Online (Sandbox Code Playgroud)

我已经可以使用共享库执行此操作,但之后我无法再使用Declarative Pipeline功能.

有没有办法在许多存储库之间共享声明性管道?

小智 0

我一直在为自己的工作处理同样的问题。我能想到的最佳解决方案是在我的组织中的每个项目/存储库中包含一个通用的Jenkinsfile:

node
{
  checkout([$class: 'GitSCM', branches: [[name: env.DELIVERY_PIPELINE_BRANCH]], userRemoteConfigs: [[credentialsId: env.DELIVERY_PIPELINE_CREDENTIALS, url: env.DELIVERY_PIPELINE_URL]]])
  stash includes: '*.groovy', name: 'assets', useDefaultExcludes: false
  load './Jenkinsfile.groovy'
}
Run Code Online (Sandbox Code Playgroud)

我使用了环境变量,以防情况需要改变,可能比我当前的示例更加动态(无论如何,这一切仍在开发中)。

然后 stash 用于保存稍后使用的其余 groovy 脚本,并将它们取消存储在声明性管道中。

最后它加载声明性管道。不影响视线,基本上一切正常。

所以这并不完全是您想要的,我宁愿首先有能力从 SCM 中提取。但是,嘿,目前它对我来说已经足够好了。