【发布时间】: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