如何使用 GitHub Org Plugin 从 jenkins 管道 (jenkinsfile) 触发另一个作业? [英] How can I trigger another job from a jenkins pipeline (jenkinsfile) with GitHub Org Plugin?
问题描述
如何从 Jenkinsfile
中触发另一个作业的构建?
How can I trigger build of another job from inside the Jenkinsfile
?
我假设这项工作是同一 github 下的另一个存储库组织,一个已经拥有自己的 Jenkins 文件的组织.
I assume that this job is another repository under the same github organization, one that already has its own Jenkins file.
我也想只在分支名称为 master 时才这样做,因为触发任何本地分支的下游构建没有意义.
I also want to do this only if the branch name is master, as it doesn't make sense to trigger downstream builds of any local branches.
更新:
stage 'test-downstream'
node {
def job = build job: 'some-downtream-job-name'
}
不过,执行时我得到一个错误
Still, when executed I get an error
找不到名为 some-downtream-job-name 的参数化作业
No parameterized job named some-downtream-job-name found
我确信这个工作存在于 jenkins 中,并且与当前工作在同一个组织文件夹下.这是另一个拥有自己的 Jenkinsfile
的作业.
I am sure that this job exists in jenkins and is under the same organization folder as the current one. It is another job that has its own Jenkinsfile
.
请注意,此问题特定于 GitHub 组织插件 它为您的 GitHub 组织中的每个存储库和分支自动创建和维护作业.
Please note that this question is specific to the GitHub Organization Plugin which auto-creates and maintains jobs for each repository and branch from your GitHub Organization.
推荐答案
首先,在node
中包装build
步骤是浪费一个executor slot.您的上游执行器将无缘无故地闲置.
First of all, it is a waste of an executor slot to wrap the build
step in node
. Your upstream executor will just be sitting idle for no reason.
其次,从多分支项目中,您可以使用环境变量 BRANCH_NAME
使逻辑以当前分支为条件.
Second, from a multibranch project, you can use the environment variable BRANCH_NAME
to make logic conditional on the current branch.
第三,job
参数采用绝对或相对作业名称.如果您给出一个没有任何路径限定的名称,那将指代同一文件夹中的另一个作业,在多分支项目的情况下,这意味着同一存储库的另一个分支.
Third, the job
parameter takes an absolute or relative job name. If you give a name without any path qualification, that would refer to another job in the same folder, which in the case of a multibranch project would mean another branch of the same repository.
所以你想写的大概是
if (env.BRANCH_NAME == 'master') {
build '../other-repo/master'
}
这篇关于如何使用 GitHub Org Plugin 从 jenkins 管道 (jenkinsfile) 触发另一个作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!