实现您想要的效果的一种选择是使用以下设置:
创建两个 Jenkins 作业:
- “组件 A”(SCM 更改时自动触发)
- “组件B”(“手动”触发)
Step #1
定义branch
构建参数 https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build对于“组件 B”:
使用此参数作为“Git Plugin”分支说明符:
现在您应该能够通过为其指定适当的分支(标签)参数来手动触发“组件 B”构建,例如tags/5.3.0
.
Step #2
将新的“执行外壳”构建步骤添加到“组件 A”构建中,这将从工作区的配置文件中提取“组件 B”版本,并准备b.properties
具有“组件 B”构建参数的文件。
安装一个参数化触发器 https://plugins.jenkins.io/parameterized-triggerJenkins 插件,并向“组件 A”作业添加新的“触发/调用在其他项目上构建”构建步骤:
使用您的b.properties
文件作为构建参数的来源。
现在,每次重新构建“组件 A”时,都会触发新的“组件 B”构建,并以目标分支/标签作为构建参数。
添加通配符支持
如果你想支持通配符版本,你可以使用git ls-remote
命令查找最新标签,如下所示:
#B=$(obtain B version from the config file in a usual way)
LATEST=$(\
git ls-remote --tags YOUR_REPOSITORY_URL "$B"\
|cut -d / -f3|sort -r --version-sort|head -1\
)
cat <<EOF > b.properties
branch=tags/$LATEST
EOF
这将列出远程“组件 B”存储库中与“B”版本模式匹配的所有标签,并将最新版本号保存在LATEST
多变的。
将其添加到“组件 A”作业的“执行 Shell”步骤中,
它应该能够处理版本号模式,例如:5.3.*
问题是 shell 脚本将以 Jenkins 守护程序用户身份运行,
因此它必须配置正确的凭据才能访问远程 Git 存储库(例如通过 ssh 公钥)。
或者,您可能想查看凭证绑定插件 https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Binding+Plugin,以重用存储在 Jenkins 本身中的 Git 凭据。
使用 Jenkins 2.0 风格的管道
您还可以使用 Jenkins 2.0 风格来解决手头的任务Pipeline https://jenkins.io/doc/book/pipeline/,这将允许您将组件 A 和 B 的代码检出到单个工作区中,然后对它们应用一些常见的构建步骤。
你的管道可能看起来像这样:
node {
//Settings
def credentialsId = '8fd28e34-b04e-4bc5-874a-87f4c0e05a03'
def repositoryA = 'ssh://[email protected] /cdn-cgi/l/email-protection/projects/a.git'
def repositoryB = 'ssh://[email protected] /cdn-cgi/l/email-protection/projects/b.git'
stage('Checkout component A') {
git credentialsId: credentialsId ,
url: repositoryA , branch : "master"
}
stage("Resolve and checkout component B") {
def deps = readProperties file: 'meta.properties'
echo "Resolved B version = ${deps['b']}"
dir("module/b") {
//Clone/Fetch Component B
checkout scm:[
$class: 'GitSCM',
userRemoteConfigs: [[url: repositoryB, credentialsId: credentialsId]],
branches: [[name: 'refs/tags/*']]
],
changelog: false, poll: false
//Checkout the tag, matching deps['b'] pattern
sshagent([credentialsId]) {
sh "git checkout \$(git tag -l \"${deps['b']}\" |sort -r --version-sort|head -1)"
}
}
}
stage("Build A+B") {
//Apply a common build step
}
}
这里我们使用“readProperties”命令,它是管道实用程序步骤插件 https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Utility+Steps+Plugin从中提取“组件 B”版本模式meta.properties
。还有 readYaml、readJSON 命令可用。
接下来我们获取/克隆“组件 B”,其中changelog: false, poll: false
标志,以防止其注册用于 SCM 轮询,并将其放入当前工作区的“module/b”文件夹中。
然后调用 shell 命令来根据我们上面获得的版本模式选择标签,并签出它(5.3.* 样式通配符也应该有效)。
The sh
调用,被包装在sshagent https://wiki.jenkins-ci.org/display/JENKINS/SSH+Agent+Plugin,使其重用适当的
来自 Jenkins 凭证存储的凭证。