Jenkins - 触发器

触发方式

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
posted @ 2024-09-05 22:43  Anliven  阅读(123)  评论(0编辑  收藏  举报