首页 > 其他分享 >Jenkins - 触发器

Jenkins - 触发器

时间:2024-09-05 22:35:41浏览次数:10  
标签:触发器 -- Pipeline header trigger jenkins Jenkins 100

触发方式

jenkins自带的job触发方式

Generic Webhook Trigger插件

在需要外界其他系统触发Jenkins任务的场景,通常使用Generic Webhook Trigger插件来配置 Jenkins 触发器
Generic Webhook Trigger 插件允许使用 Webhook将外部系统与 Jenkins 集成。
通过配置 Webhook,在特定事件发生时,外部系统可以触发Jenkins 构建。

插件安装完成后,在pipeline类型项目中可以看到Generic Webhook Trigger 选项

勾选后,会出现提示信息和参数填写框

示例-1 解析Request参数

配置请求URL中传递的参数

配置触发的token参数(Token值具有唯一性,避免同时出发其他配置相同的任务)

设置打印调试日志(用于调试和排错)

对应的pipeline内容

pipeline {
    agent {
        label 'master'
    }
    stages {
        stage('web-trigger') {
            steps {
                script {
                    echo "${version}"
                    echo "${username}"
                }
            }
        }
    }
}

curl命令模拟请求测试

curl "http://192.168.16.200:8080/generic-webhook-trigger/invoke?token=test-web-trigger&version=1.1.1&username=jenkins"

curl命令测试结果

$ curl "http://192.168.16.200:8080/generic-webhook-trigger/invoke?token=test-web-trigger&version=1.1.1&username=jenkins" | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   254  100   254    0     0  45035      0 --:--:-- --:--:-- --:--:-- 50800
{
    "jobs": {
        "dev-demo2": {
            "regexpFilterExpression": "",
            "triggered": true,
            "resolvedVariables": {
                "username": "jenkins",
                "username_0": "jenkins",
                "version": "1.1.1",
                "version_0": "1.1.1"
            },
            "regexpFilterText": "",
            "id": 58,
            "url": "queue/item/58/"
        }
    },
    "message": "Triggered jobs."
}

pipeline输出日志

Generic Cause
[Pipeline] Start of Pipeline
 Received:




Contributing variables:

    username = jenkins
    username_0 = jenkins
    version = 1.1.1
    version_0 = 1.1.1


[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/dev-demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (web-trigger)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
1.1.1
[Pipeline] echo
jenkins
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

示例-2 解析Header参数

在示例-1的基础上,配置Header参数

pipeline内容对应添加

echo "${header_name}"
echo "${header_id}"

curl命令模拟请求测试

$ curl --location --request GET "http://192.168.16.200:8080/generic-webhook-trigger/invoke?token=test-web-trigger&version=1.1.1&username=jenkins" --header 'header_name:admin' --header 'header_id:100' | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   338  100   338    0     0  57395      0 --:--:-- --:--:-- --:--:-- 67600
{
    "jobs": {
        "dev-demo2": {
            "regexpFilterExpression": "",
            "triggered": true,
            "resolvedVariables": {
                "header_id": "100",
                "header_id_0": "100",
                "header_name": "admin",
                "header_name_0": "admin",
                "username": "jenkins",
                "username_0": "jenkins",
                "version": "1.1.1",
                "version_0": "1.1.1"
            },
            "regexpFilterText": "",
            "id": 64,
            "url": "queue/item/64/"
        }
    },
    "message": "Triggered jobs."
}

pipeline日志输出

Generic Cause
[Pipeline] Start of Pipeline
 Received:




Contributing variables:

    header_id = 100
    header_id_0 = 100
    header_name = admin
    header_name_0 = admin
    username = jenkins
    username_0 = jenkins
    version = 1.1.1
    version_0 = 1.1.1


[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/dev-demo2
[Pipeline] {
[Pipeline] stage
[Pipeline] { (web-trigger)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
1.1.1
[Pipeline] echo
jenkins
[Pipeline] echo
admin
[Pipeline] echo
100
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

示例-3 解析Post参数

配置Post参数

  • Expression获取数据并赋值给Variable,
  • 变量名称可以自定义,Expression根据数据格式和内容编写

配置token

配置调试日志

pipeline内容

pipeline {
    agent {
        label 'master'
    }
    stages {
        stage('web-trigger') {
            steps {
                script {
                    echo "${devopsName}"
                }
            }
        }
    }
}

curl命令模拟请求测试

$ curl --location --request POST "http://192.168.16.200:8080/generic-webhook-trigger/invoke?token=test-web-trigger-post" \
--header 'Content-Type: application/json' \
--data-raw "{'name':'admin','id':'100','group':{'name':'jenkins','id':'101'}}" | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   260  100   195  100    65   2199    733 --:--:-- --:--:-- --:--:--  2954
{
    "jobs": {
        "dev-demo3": {
            "regexpFilterExpression": "",
            "triggered": true,
            "resolvedVariables": {
                "devopsName": "jenkins"
            },
            "regexpFilterText": "",
            "id": 72,
            "url": "queue/item/72/"
        }
    },
    "message": "Triggered jobs."
}

pipeline输出日志

Generic Cause
[Pipeline] Start of Pipeline
 Received:

{'name':'admin','id':'100','group':{'name':'jenkins','id':'101'}}


Contributing variables:

    devopsName = jenkins


[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/dev-demo3
[Pipeline] { (hide)
[Pipeline] stage
[Pipeline] { (web-trigger)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
jenkins
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

标签:触发器,--,Pipeline,header,trigger,jenkins,Jenkins,100
From: https://www.cnblogs.com/anliven/p/18398042

相关文章

  • Jenkins Job 构建动态参数配置
    在做jenkins项目时,有些参数不是固定写死的,而是动态变化的,这时我们可以用ActiveChoices插件来远程调用参数。实现结果如下:操作步骤: 注意:筛选关键字参数必须位于筛选列表的前面,如该文章示例keyword必须在affectedPlatformIds前面   GroovyScript代码如下:import......
  • docker-compose轻松部署jenkins
    一、安装部署docker1、安装docker-ce可以参考:docker-ce镜像_docker-ce下载地址_docker-ce安装教程-阿里巴巴开源镜像站(aliyun.com)https://developer.aliyun.com/mirror/docker-ce?spm=a2c6h.13651102.0.0.3e221b11EZ9mMK2、配置docker镜像加速[root@docker~]#cat/etc......
  • Jenkins启动报错java.lang.NullPointerException
    centos6.9下安装Jenkins,可以正常安装,但是启动后,Jenkins.log中发现报错java.lang.NullPointerException:Cannotloadfromshortarraybecause"sun.awt.FontConfiguration.head"isnullatjava.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:......
  • Jenkins流水线共享库
    绝大部份项目的构建流程是:拉取源码---执行单元测试---构建目标包---构建镜像---推送镜像---集群拉取镜像部署,此脚本为Jenkins流水线共享库(Jenkinspiplinesharedlibrary),将所有步骤统一定义到此脚本中,所有项目引用共享库即可,支持多分支构建,不同的分支发布到不同的环境。引用共......
  • Jenkins - 在Rocky Linux系统下安装Jenkins和配置Agent节点
    安装Jenkins主节点下载和安装JenkinsLTS版本本机信息[root@localhost~]#cat/etc/system-releaseRockyLinuxrelease9.4(BlueOnyx)[root@localhost~]#[root@localhost~]#uname-aLinuxlocalhost.localdomain5.14.0-427.33.1.el9_4.x86_64#1SMPPREEMPT_DYNA......
  • CentOS 8 安装 jenkins、gitlab-ce、SonarQube教程
    一、在CentOS8上安装jenkins在一台CentOS8(IP:10.0.0.81RAM:2GB)上安装jenkins-2.452.41、配置Java环境从Jenkins-2.357版本开始JDK最低要求java11,而jenkins-2.346.1是最后一个支持java1.8的版本。点击查看代码1.先查看系统是否自带了jdk,若有输出信息,批量卸载......
  • jenkins动态切换环境
    一.代码层实现动态切换1.首先在conftest.py下声明pytest_addoption钩子函数,写法如下defpytest_addoption(parser):#设置要接收的命令行参数parser.addoption("--env",default="prod",choices=['pre','uat','prod','test'],......
  • highgo数据库加触发器实例-记
    需求:t_key_areas_info发生变化时,更新t_building_info_manage表相关字段。1、新建functioncreateorreplacefunctiont_key_areas_info_trigger_func()returnstriggeras$$declareBEGINIF(TG_OP='INSERT')THENRAISENOTICE'thisisaraisedemo,......
  • jenkins发送html邮件配置步骤与注意事项?
    jenkins发送html邮件如何实现?Jenkins的配置方法?通过jenkins发送html邮件,开发团队可以及时获取构建状态的详细报告,从而快速响应问题,提高工作效率。AokSend将详细介绍jenkins发送html邮件的配置步骤与注意事项,帮助读者更好地利用这一功能。jenkins发送html邮件:安装插件要实现......
  • jenkins安装及自动化部署-docker部署
    一、安装1.创建jenkins工作目录mkdir/home/chanfi/docker/jenkins2.下载JDK#进入工作目录下cd/home/chanfi/docker/jenkins#创建JDK目录mkdirjavacdjava#下载jdk(或到官网中下载:https://www.oracle.com/java/technologies/downloads/?er=221886)wgethttps://d......