如何检查管道是否由拉取请求触发
How to check if a pipeline is triggered from a pull request
我正在使用 Jenkins 管道通过 GitHub 组织插件构建 Pull Requests 分支。
我可以很好地构建它们,但我想避免一些步骤(例如发布工件)。检查 git 的当前分支为我提供了 PR 的目标,因为在尝试构建之前 PR 分支正在合并到目标中。
如何检查构建是从 PR 启动还是从常规分支构建启动?
至少在 Jenkins 2.16 上 env.BRANCH_NAME
给出了源分支而不是目标分支。你可以这样做:
if (env.BRANCH_NAME == "master") {
sh "./publish.sh"
}
其他可能有用的环境变量是 CHANGE_*
变量。例如,
CHANGE_AUTHOR='me'
CHANGE_ID='6'
CHANGE_TARGET='master'
CHANGE_TITLE='Update README.md'
CHANGE_URL='https://github.com/test-org/test-repo/pull/6'
有关这些以及更多内容的文档:https://ci.eclipse.org/webtools/env-vars.html/
要专门检测 GitHub 拉取请求,可以使用:
script {
if (env.BRANCH_NAME == 'master') {
sh 'make'
} else if (env.BRANCH_NAME.startsWith('PR')) {
// do actions for pull request
} else {
// some other branch
}
}
当然,如果您希望在您的主存储库中有以 PR 开头的分支,这将是不可靠的。它的好处是 script
也可以在 post
中使用,而不仅仅是 stages
,这很有用,因为 when
不允许在 post
中使用。如果您不关心它,那么值得研究一下 when
指令。 Cloudbees and Jenkins 中有一些文档和一些示例。
env 变量 CHANGE_ID
仅在通过 Pull Request 检查触发构建时才存在。
For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset.
我正在使用 Jenkins 管道通过 GitHub 组织插件构建 Pull Requests 分支。
我可以很好地构建它们,但我想避免一些步骤(例如发布工件)。检查 git 的当前分支为我提供了 PR 的目标,因为在尝试构建之前 PR 分支正在合并到目标中。
如何检查构建是从 PR 启动还是从常规分支构建启动?
至少在 Jenkins 2.16 上 env.BRANCH_NAME
给出了源分支而不是目标分支。你可以这样做:
if (env.BRANCH_NAME == "master") {
sh "./publish.sh"
}
其他可能有用的环境变量是 CHANGE_*
变量。例如,
CHANGE_AUTHOR='me'
CHANGE_ID='6'
CHANGE_TARGET='master'
CHANGE_TITLE='Update README.md'
CHANGE_URL='https://github.com/test-org/test-repo/pull/6'
有关这些以及更多内容的文档:https://ci.eclipse.org/webtools/env-vars.html/
要专门检测 GitHub 拉取请求,可以使用:
script {
if (env.BRANCH_NAME == 'master') {
sh 'make'
} else if (env.BRANCH_NAME.startsWith('PR')) {
// do actions for pull request
} else {
// some other branch
}
}
当然,如果您希望在您的主存储库中有以 PR 开头的分支,这将是不可靠的。它的好处是 script
也可以在 post
中使用,而不仅仅是 stages
,这很有用,因为 when
不允许在 post
中使用。如果您不关心它,那么值得研究一下 when
指令。 Cloudbees and Jenkins 中有一些文档和一些示例。
env 变量 CHANGE_ID
仅在通过 Pull Request 检查触发构建时才存在。
For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset.