提问人:alessandro ferrucci 提问时间:11/15/2023 最后编辑:alessandro ferrucci 更新时间:11/15/2023 访问量:45
Jenkins 仅在推送到 master 时运行流水线
Jenkins only run pipeline when push to master
问:
我有一个简单的 Jenkins 管道项目,我想在推送到 master 分支时触发它,我使用以下设置配置了管道:
生成触发器:用于 GITScm 轮询的 GitHub 挂钩触发器。
管道:
- 定义:来自 SCM 的管道脚本
- SCM:Git
- 存储库 URL:$URL
- 凭据:在 Jenkins 中配置的 SSH 密钥
- 分支说明符:refs/heads/master(我也尝试将其设置为*/master,但无济于事)
脚本路径:Jenkinsfile
现在的问题是,无论我的推送发生在什么位置,这个管道都会被启动并运行。例如,删除或创建标记将启动此管道。ref
在同一个 Jenkins 实例中,我没有看到类似定义的管道出现这种行为,但它们的定义完全相同(除了指向同一 GitHub 实例中的不同存储库)。
有没有人知道我可以在哪里寻找解决为什么任何 GitHub 推送,无论它的目的是什么,都会构建这个管道?ref
编辑:
我注意到的是,对于行为正确的构建(不要在非主推送上执行),Jenkins 日志显示以下内容:
[2023-11-14 18:20:08] [INFO] Received PushEvent for https://$GITHUB_REPO from $IP_ADDRESS ⇒ https://$JENKINSURL/webhook
[2023-11-14 18:20:08] [INFO] Poked GoodPipeline
[2023-11-14 18:20:08] [FINEST] lastBuildRevisionSha1 matches sha1:029801983091280128101290129012, returning lastBuild
[2023-11-14 18:20:09] [FINEST] lastBuildRevisionSha1 matches sha1:029801983091280128101290129012, returning lastBuild
对于此行为不正确的管道,日志显示以下内容:
[2023-11-14 18:16:23] [INFO] Received PushEvent for https://$GITHUB_REPO from $IP_ADDRESS ⇒ https://$JENKINS_URL/webhook
[2023-11-14 18:16:23] [INFO] Poked MyPipeline
[2023-11-14 18:16:24] [FINEST] lastBuildRevisionSha1 matches sha1:103981293812312391239012390123, returning lastBuild
[2023-11-14 18:16:24] [FINEST] lastBuildRevisionSha1 matches sha1:103981293812312391239012390123, returning lastBuild
[2023-11-14 18:16:25] [FINEST] lastBuild is null
[2023-11-14 18:16:25] [FINEST] No match found in getLastBuild for sha1:103981293812312391239012390123, returning null
[2023-11-14 18:16:25] [INFO] SCM changes detected in MyPipeline. Triggering #5
我在管道中的历史记录中确实有以前的版本出现故障,所以我不确定为什么它找不到它们。
答:
您的分支说明符似乎是正确的(或)。但是,请确保没有错别字或多余的空格。
分支中的 Jenkinsfile 不应具有任何可能覆盖分支说明符的配置。refs/heads/master
*/master
master
作为测试/解决方法:要在 Jenkinsfile 中实现检查,如果它不是分支,它将退出管道,您可以在管道脚本的开头添加一个条件:master
pipeline {
agent any
stages {
stage('Precheck') {
steps {
script {
if(env.BRANCH_NAME != 'master') {
currentBuild.result = 'ABORTED'
error('Building on master branch only')
}
}
}
}
// rest of your pipeline
}
}
GitHub Push 到 refs/tags/1.6.0 仍然导致管道执行。
转到 GitHub 存储库的设置,检查 Jenkins 服务器的 Webhook 配置,并确保将 Webhook 设置为仅针对“推送”事件触发,而不针对“标记推送”事件触发。
此外,修改 Jenkinsfile 以明确检查 ref 是标签还是分支。如果它是标记,则可以中止生成。
pipeline {
agent any
stages {
stage('Precheck') {
steps {
script {
if(env.GIT_BRANCH.startsWith('refs/tags/')) {
currentBuild.result = 'ABORTED'
error('Ignoring tag push')
}
}
}
}
// rest of your pipeline
}
}
我注意到的是,对于行为正确的构建(不要在非主推送上执行),Jenkins 日志显示以下内容
在出现故障的管道中,日志条目表明 Jenkins 无法将传入的提交 SHA 与管道历史记录中的任何先前版本相关联。
这导致 Jenkins 将每一次推送都视为新的更改,从而触发构建。[FINEST] lastBuild is null
[FINEST] No match found in getLastBuild for sha1...
确保在 Jenkins 中正确配置了 SCM 轮询。它应该准确地反映你所针对的存储库和分支。
仔细检查您的 Jenkinsfile 和流水线配置,了解工作流水线和故障流水线之间的任何差异,尤其是在 SCM 的设置方式上。
此外,请验证日志中提到的 SHA1 是否与存储库分支中的提交相对应。如果不是来自 ,则可能表示 Jenkins 错误地处理了来自其他分支或标记的事件。master
master
检查故障管道的生成历史记录。如果 Jenkins 维护构建历史记录或将构建与特定 SHA1 相关联的方式存在问题,这可能会导致您遇到的问题。
评论
git
评论
*master
refs/tags/1.6.0