我有一个通用的多分支项目,我在大约100个不同的git repos上使用它。jenkins作业是自动生成的,唯一的区别是git。
因为它们都是以相同的方式构建的,而且我不想在所有的repos中复制相同的jenkins groovy文件,所以我使用“->模式-> (默认情况下是jenkinsfile)”。
它打破了将jenkinsfile放入SCM中的规则,正如我所希望的那样。为了将影响降到最低,我希望那个groovy文件只签出“真正的”jenkinsfile并执行它。
我用这个剧本:
pipeline {
agent {label 'docker'}
stages {
stage('jenkinsfile checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: 'master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'gipc_synthesis']],
submoduleCfg: [],
userRemoteConfigs: [[url: 'ssh://git@camtl1bitmirror.gad.local:7999/mtlstash/mvt/gipc_synthesis.git']]]
)
}
}
stage('Load user Jenkinsfile') {
//agent any
steps {
load 'gipc_synthesis/jenkins/synthesis_job.groovy'
}
}
}
}我遇到的问题是,在我正在加载的groovy文件中不能有另一个管道。我不想只定义函数,而是定义文件中的整个管道。这个问题有什么解决办法吗?我也对完全避免整个问题的解决办法感兴趣。
谢谢。
发布于 2020-01-14 15:34:38
您可以在管道中拥有一个共享库:
// my-shared.git: vars/build.groovy
def call(String pathToGit) // and maybe additional params
{
pipeline {
agent { ... }
stages {
stage('jenkinsfile checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: 'master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'gipc_synthesis']],
submoduleCfg: [],
userRemoteConfigs: [[url: pathToGit]]]
)
}
}
}
}
}并在您的Jenkinsfile中使用,例如:
#!groovy
@Library('my-shared') _
def pathToGit = 'ssh://git@camtl1bitmirror.gad.local:7999/mtlstash/mvt/gipc_synthesis.git'
build(pathToGit)https://stackoverflow.com/questions/59735517
复制相似问题