【问题标题】:Define Jenkins pipeline on an external file在外部文件上定义 Jenkins 管道
【发布时间】:2018-08-11 18:36:43
【问题描述】:

我们有几个具有相同结构和行为的管道作业:更新 ansible 存储库,执行带有一些参数的剧本,其值取决于环境,并通过检查执行进行测试。我们尝试在外部文件中抽象出一般行为。

JenkinsfileAnsible:

#!/usr/bin/env groovy
import groovy.json.JsonOutput

node {
}

def executePlaybook(environment){
  pipeline{

    agent any

    stages{
      stage('Update repository'){
        ...
      }
      stage('Esecute playbook'){
        ...
      }
      stage('Execute tests'){
        ...
      }
    }
  }
}
return this

每个环境都会有一个特定的 Jenkinsfile 来设置参数并加载通用 Jenkinsfile 以执行管道。

JenkinsfileDev:

#!/usr/bin/env groovy
import groovy.json.JsonOutput

node{
    checkout scm
    def ansible = load "../JenkinsfileAnsible"
    ansible.execute_playbook("development")
}

代码已被简化,我们可以轻松加载外部文件或执行定义的函数。问题是我们想在通用文件中定义管道,对于每个环境都是一样的,只是调用它,但我们不能让它工作。

我们遇到了错误,因为 Jenkins 无法识别外部文件中的管道定义。

有什么建议吗?不可以吗?我们缺少什么吗?

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-groovy


    【解决方案1】:

    您可以使用来自 https://jenkins.io/doc/book/pipeline/shared-libraries/ 的 Jenkins Pipeline 共享库

    方法是有一个像这样的 Jenkinsfile:

    @Library('your-pipeline') _
    thePipeline([param1: val1])
    

    在管道库代码中,类似:

    def call(Map<String, String> pipelineConfig) {
    
        pipeline{
    
            agent any
    
            stages{
               stage('Update repository'){
               //You can use your pipelineConfig param1 etc.
            }
            stage('Esecute playbook'){
               ...
            }
            stage('Execute tests'){
               ...
            }
        }
    }
    

    您可以将配置参数用于不同的环境,甚至可以为不同的环境创建不同的管道。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      相关资源
      最近更新 更多