Gitlab触发jenkins并获取项目post参数 Gitlab触发jenkins并获取项目post参数

编程之家收集整理的这篇文章主要介绍了Gitlab触发jenkins并获取项目post参数 Gitlab触发jenkins并获取项目post参数编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Gitlab触发jenkins并获取项目post参数

 

jenkins -- Generic Webhook Trigger插件

插件是git webhook的高阶应用,安装后会暴露出来一个公共API,GWT插件接收到 JSON 或 XML 的 HTTP POST 请求后,根据我们配置的规则决定触发哪个Jenkins项目。
定义需要的变量

插件有两种配置方式

1.图形界面配置-创建流水线任务在触发器中配置(本文不采用此法)

image

2.pipeline 脚本中配置-注意此方法需要手动触发一次构建任务生成 Generic Webhook Trigger配置

jenkins配置

1.安装插件

image


勾选Generic Webhook Trigger后,点击【Install without restart】安装插件

image

image

2.创建Jenkins任务

在Jenkins Dashboard中,点击【新建任务】

image

输入任务名称,选择流水线类型后,点击确定创建任务。

 

 

点击刚才创建好的任务。

 

 


点击【配置】。

 

 


选择【流水线】。

image

3.pipeline内容

pipeline {
    agent any
    riggers{
        GenericTrigger(
            genericVariables:[
                [key:'event_name',value:'$.event_name'],//触发动作  pubat or tag_pubat
                [key:'user_email',value:'$.user_email'],//GitLab公共邮箱需要自行配置否则获取不到
                [key:'project_name',value:'$.project.name'],//项目名称 DevOps_Test
                [key:'git_url',value:'$.project.git_http_url'],//git_url http://xxx.xxx.xxx/devops/DevOps_Test.git
                [key:'ref',value:'$.ref'],//分支或tag信息
                [key:'group_name',value:'$.project.namespace'],//GITLAB_GROUP
                [key:'commits_id',value:'$.commits[0].id'] //gitlab commits id
            ],token:"qazwsx",//gitlab webhook触发token 多个任务配置同一个token会一起触发
            causeString:'Triggered on $ref',printContributedVariables:true,printPostContent:true
        )
    }

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

gitlab传递的post数据是json格式

{
  "object_kind": "push","event_name": "push","before": "a2e3c8d96b30967da1fa9579096d52c2b3757d2a","after": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d","ref": "refs/heads/master","checkout_sha": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d","message": null,"user_id": 324,"user_name": "h_y","user_username": "h_y","user_email": "","user_avatar": null,"project_id": 3199,"project": {
    "id": 3199,"name": "hello","description": "","web_url": "http://gitlab.example.com/h_y/hello","avatar_url": null,"git_ssh_url": "git@gitlab.example.com:h_y/hello.git","git_http_url": "http://gitlab.example.com/h_y/hello.git","namespace": "h_y","visibility_level": 0,"path_with_namespace": "h_y/hello","default_branch": "master","ci_config_path": null,"homepage": "http://gitlab.example.com/h_y/hello","url": "git@gitlab.example.com:h_y/hello.git","ssh_url": "git@gitlab.example.com:h_y/hello.git","http_url": "http://gitlab.example.com/h_y/hello.git"
  },"commits": [
    {
      "id": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d","message": "type\n","title": "type","timestamp": "2020-05-28T15:09:37+08:00","url": "http://gitlab.example.com/h_y/hello/-/commit/9ff547f1010f40c54aefe693d32c026cfc7d8f4d","author": {
        "name": "h_y","email": "h_y@example.com"
      },"added": [

      ],"modified": [
        "Jenkinsfile"
      ],"removed": [

      ]
    },{
      "id": "a49de07609ad97132c0c42aca35c75694ab80085","timestamp": "2020-05-28T15:08:47+08:00","url": "http://gitlab.example.com/h_y/hello/-/commit/a49de07609ad97132c0c42aca35c75694ab80085",{
      "id": "a2e3c8d96b30967da1fa9579096d52c2b3757d2a","timestamp": "2020-05-28T15:07:58+08:00","url": "http://gitlab.example.com/h_y/hello/-/commit/a2e3c8d96b30967da1fa9579096d52c2b3757d2a","removed": [

      ]
    }
  ],"total_commits_count": 3,"push_options": {
  },"repository": {
    "name": "hello","visibility_level": 0
  }
}

创建完成手动触发一次构建生成插件配置文件

image

gitlb配置

 

 

http://jenkinsserver:8080//generic-webhook-trigger/invoke?token=qazwsx

image

image

集成测试

image

小编说

以上是编程之家为你收集整理的Gitlab触发jenkins并获取项目post参数 Gitlab触发jenkins并获取项目post参数全部内容。

如果觉得编程之家网站内容还不错,欢迎将编程之家推荐给好友。

原文地址:https://www.cnblogs.com/cndevops

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


基础环境、流程图1)流程图jenkinspipeline发布应用2)、jenkins上安装kubectl#主节点上查看kubectl位置[root@k8s01~]#whichkubectl/usr/bin/kubectl#将k8s节点的kubectl文件cp至jenkinsscp10.0.0.101:/usr/bin/kubectl.cp./kubectl/usr/local/bin/kubectl
本节是建立在流水线入门内容的基础上,而且,应当被当作一个参考。对于在实际示例中如何使用流水线语法的更多信息,请参阅本章在流水线插件的2.5版本中的使用Jenkinsfile部分,流水线支持两种离散的语法,具体如下对于每种的优缺点,参见语法比较。正如本章开始讨论的,流水线最
Linuxcentos7安装Jenkins(jenkins-2.227-1.1.noarch.rpm)时遇到问题:1.出现问题1:安装jenkins一直失败,提示Nosuchfileordirectory[helen@linuxprobeJenkins]$sudorpm-ivhjenkins-2.227-1.1.noarch.rpmPreparing...####################
作为一个拥有很多账号的当代美少女,忘记用户名密码什么的是再正常不过的事了。“忘记密码”这一功能给我真的带来了很大的福音啊。但是最近重新使用Jenkins时,看到需要输入用户名密码,顿时脑子一片空白。经过一系列的查找,终于解决了问题,害,只有把方法记下来才是王道,以后妈妈再
Jenkins、GitLab和Maven(Nexus)完成自动构建迈向持续集成持续集成,毋庸置疑的是这是两部分要求,一个是持续一个是集成,那么持续和集成到底表示了什么要求和含义呢?其中的集成比较容易理解就是指开发人员将自己开发的代码不断地交付给全部系统的代码,在Git仓的概念之下也就是不断地
 下载文档https://www.jenkins.io/zh/download/  Debian下载1.下载安装keyhttps://pkg.jenkins.io/debian-stable/jenkins.io.key  
流水线建设从Jenkins部署开始Jenkins是一个独立的开源自动化服务器,可以用来自动化,例如构建、测试和部署软件等各种任务,是一个实现CI(持续集成)的很好的工具。Jenkins可以通过本地系统包、Docker安装,甚至可以在安装Java运行时环境的机器上独立运行。JenkCI/CD的概念谈到持
原因:这种情况是SSHServer中的RemoteDirectory路径不存在。解决方法:重新输入目标服务器中存在的路径。      
什么是持续集成持续集成(Continuousintegration,简称CI)指的是,频繁地(一天多次)将代码集成到主干。持续集成的目的,就是让产品可以快速迭代,同时还能保持高质量。它的核心措施是,代码集成到主干之前,必须通过自动化测试。只要有一个测试用例失败,就不能集成。通过持续集成,团
Tomcat安装:1.下载Tomcat包:http:/omcat.apache.org/并解压2.启动:双击bin\startup.bat文件浏览器访问:http://localhost:8080/3.Tomcat配置登录:打开confomcat-users.xml文件添加如下代码:<rolerolename="manager-gui"/><userusername="admin"password=&q