Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jenkins 如何在web钩子URL中动态传递git分支名称?_Jenkins_Continuous Integration_Gitlab_Jenkins Pipeline_Jenkins Declarative Pipeline - Fatal编程技术网

Jenkins 如何在web钩子URL中动态传递git分支名称?

Jenkins 如何在web钩子URL中动态传递git分支名称?,jenkins,continuous-integration,gitlab,jenkins-pipeline,jenkins-declarative-pipeline,Jenkins,Continuous Integration,Gitlab,Jenkins Pipeline,Jenkins Declarative Pipeline,我在Jenkins中使用参数化的声明性下游作业。 在webhook期间,我需要将git_repo和git_分支作为参数传递 我在Gitlab存储库中定义了这个参数 示例 https://myjenkins.com/job/myjob-builder-downstream/buildWithParameters?token=1qqq1f54ff88e373b3c0&git_repo=git@mygitlab:development/myproduct.git&git_branch=

我在Jenkins中使用参数化的声明性下游作业。 在webhook期间,我需要将git_repo和git_分支作为参数传递

我在Gitlab存储库中定义了这个参数

示例

https://myjenkins.com/job/myjob-builder-downstream/buildWithParameters?token=1qqq1f54ff88e373b3c0&git_repo=git@mygitlab:development/myproduct.git&git_branch=master
在webhook期间,我不知道如何将分支名称动态地传递给下游作业

提前感谢您的帮助。

主要提供商(bitbucket、github和gitlab)不允许我们在静态webhook url注册步骤中使用此级别的参数化:

比特桶

Github

Gitlab



那么,解决方案是什么? 这些提供者为我们提供了另一种选择:Webhook post有效负载解释

它是如何工作的

当github(示例)调用我们的webhook url时,发送一个带有json的正文http,其中包含大量关于事件的信息:

  • 分支机构名称
  • 存储库名称
  • 执行推送事件的用户名
  • git提交消息
因此,在webhook url的后端,您必须解析此json并获得所需的值,然后启动自定义逻辑。下面是这些json主体的一些示例:

不幸的是,github、gitlab和bitbucket的JSON不同



詹金斯插件 如果您使用一些jenkins插件,您可以使这个json解析无效。一个由供应商提供。对你来说。如果查看源代码,您将查看json解析



简易webhook插件 如果你与几个提供商合作,或者自定义插件对你没有帮助,你可以试试我的通用插件

它是如何工作的

插件公开与您的方法或其他插件的url类似的公共url:

https://myjenkins.com/project/myjob-builder-downstream/buildWithParameters?token=1qqq1f54ff88e373b3c0&git_repo=git@mygitlab:development/myproduct.git&git_branch=master

但有一些不同,我认为,更干净、更简单:

http://my_jenkins.com/easy-webhook-plugin-RDcd4y3LkDcMKKhG/?scmId=gitlab&jobId=hello_word_job

其中必须指明scmId(gitlab或bitbucket)和任何jenkins作业的id

当执行git push时,gitlab会将json发送到此url,我的插件会对其进行解析,并将一些标准参数转发给您的作业:

  • 寄存名称
  • 布兰奇纳姆
  • 作者
  • 事件消息
您可以使用jenkins中的经典“params”变量访问这些参数,并执行任何您想要的操作

node {
   echo 'New build detected with these incoming parameters: '+params
}
请跟随官方或随时与我联系