如何从配置文件执行 Jenkins 管道



我有一个通用的多分支项目,我在大约 100 个不同的 git 存储库中使用。Jenkins 作业是自动生成的,唯一的区别是 git 存储库。

由于它们都以相同的方式构建,并且我不想在所有存储库中复制相同的 jenkins groovy 文件,因此我使用"构建配置 -> 模式 -默认 jenkinsfile">"。

它打破了将 jenkinsfile 放在 SCM 中的规则,就像我更喜欢做的那样。为了尽量减少影响,我希望那个时髦的文件只签出"真正的"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'
}
}
}    
}

我遇到的问题是我无法在我正在加载的时髦文件中有另一个管道。 我不想只定义函数,而是真正定义该文件中的整个管道。这个问题有什么解决方案吗?我也对完全避免整个问题的解决方案感兴趣。

谢谢。

您可以拥有一个包含管道的共享库:

// 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)

最新更新