在Jenkins管道作业中访问BitBucket有效负载数据 [英] Access BitBucket payload data in Jenkins pipeline job
问题描述
我有一份詹金斯的管道作业;它被配置为基于由BitBucket Webhook调用的远程触发器.这有效并且正在触发构建.
I've got a Jenkins pipeline job; it is configured to build on remote trigger which is being called by a BitBucket webhook. This works and is triggering the build.
我还需要访问BitBucket发送的有效负载数据( )中获取有关推送的详细信息,例如特定分支.
I also need to access the payload data sent by BitBucket (described here) to get details about the push such as the specific branch.
BitBucket插件通常会解析此有效载荷并将其呈现给作业作为环境变量,但是我无法将管道作业设置为连接到该插件的特定存储库,因此它无济于事.
The BitBucket plugin would usually parse this payload and present it to the job as an environment variable, but I can't set the pipeline job as being connected to a specific repo for that plugin, so it doesn't help.
我尝试进行测试,以查看数据是否可以通过几种不同的方式获得,例如:
I tried testing to see if the data was available in a few different ways like so:
node {
stage 'Desperation'
echo "${params.push}"
echo "${env.BITBUCKET_PAYLOAD}"
echo "${env.push}"
}
这些不起作用(我也没真正希望它们起作用).
These don't work (nor did I really expect them to).
是否有任何方法可以保存此有效载荷数据?我唯一能想到的就是拥有一个自由式作业,并在该作业上建立与BitBucket的连接,然后在重新格式化数据后调用此作业.不过,这似乎太笨拙了.
Is there any way to get hold of this payload data? The only thing I can come up with is having a freestyle job and setting up the connection to BitBucket on that, then calling this job afterwards with the data reformatted. That seems horribly clunky though.
推荐答案
最终,我不得不解决这个问题.
Ultimately I had to settle for a workaround.
我的最终设置是管道作业my-build
和自由式作业my-build-trigger
.
My final setup is a Pipeline job my-build
and a Freestyle job my-build-trigger
.
my-build-trigger
很简单.它在SCM部分中设置了git repo,并在构建触发器部分中检查了Build when a change is pushed to BitBucket
.
my-build-trigger
is straightforward. It has a git repo set up in the SCM section and Build when a change is pushed to BitBucket
checked in the build triggers section.
在构建后操作中,我选择了Trigger parameterized build on other projects
.
In post-build actions I selected Trigger parameterized build on other projects
.
以上内容很重要. Build other projects
不会触发管道作业.它只会说my-build is not buildable
.
The above is important. Build other projects
will not trigger a Pipeline job. It will simply say my-build is not buildable
.
将您的有效负载参数传递到预定义参数下的下游(管道)作业中,输入为payload=${BITBUCKET_PAYLOAD}
.
Pass your payload parameter onto your downstream (Pipeline) job under predefined parameters, entering as payload=${BITBUCKET_PAYLOAD}
.
BITBUCKET_PAYLOAD
是由BitBucket负载对象中的BitBucket插件填充的环境变量.您可以从BitBucket 此处
BITBUCKET_PAYLOAD
is an environment variable populated by the BitBucket plugin from the BitBucket payload object. You can see payload documentation from BitBucket here
您的构建后内容将如下所示:
Your post-build will look like this:
在管道作业上,然后应创建一个名为payload
(字符串类型)的相应参数.
On your pipeline job, you should then create a corresponding parameter called payload
(String type).
在您的Jenkinsfile
中,您可以像这样使用此有效负载:
In your Jenkinsfile
you can use this payload like so:
node {
stage 'Echo stuff pointlessly'
def payload = new groovy.json.JsonSlurper().parseText("${params.payload}")
// Echoes the new commit hash
echo payload.push.changes[0].new.target.hash
}
请注意,您的作业中可能会出现序列化错误(java.io.NotSerializableException: groovy.json.internal.LazyMap
).在此问题中进行了解释.简而言之,您需要在使用后使payload
和其他JSONObject变量无效.
Be warned, you may get a serialization error in your job (java.io.NotSerializableException: groovy.json.internal.LazyMap
). That is explained in this question. In short, you need to nullify the payload
and other JSONObject vars after use.
任何评论或改进总是值得赞赏的.
Any comments or improvements are always appreciated.
这篇关于在Jenkins管道作业中访问BitBucket有效负载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!