[英]How to read and execute config.yaml file from scripted jenkins pipeline
[英]how to execute jenkins pipeline from config file
我有一个通用的多分支项目,我在大约 100 个不同的 git 存储库上使用它。 jenkins 作业是自动生成的,唯一的区别是 git repo。
由于它们都以相同的方式构建并且我不想在所有存储库中复制相同的 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 文件中不能有另一个管道。 我不想只定义函数,而是在该文件中定义整个管道。 这个问题有什么解决办法吗? 我也对完全避免整个问题的解决方案感兴趣。
谢谢你。
您可以在内部使用管道共享库:
// 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.