22

远程触发Jenkins的Pipeline任务

 3 years ago
source link: http://www.cnblogs.com/bolingcavalry/p/13796594.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

场景

  • 虽然能配置提交代码时触发Jenkins任务,但有时并不需要每次提交代码都触发,而是仅在有需要时才执行。
  • 除了在Jenkins页面上手动执行任务,还可以向Jenkins网站发起HTTP请求,触发指定任务的执行,本文就来实战通过Http请求同时触发多个Jenkins任务执行。

概述

对于pipeline类型的Jenkins任务,一般是通过插件

Generic Webhook Trigger来支持远程触发的,在使用过程中以下三点需要注意:
  1. 远程触发Jenk任务,请求的URL怎么写;
  2. Http请求的参数,如何作为pipeline脚本的参数;
  3. 假设有Jenkins任务A,某时刻有10个触发该任务的请求同时到达,Jenkins如何处理?(后面会重点讨论此问题)

针对上述问题,我们来做一次实战:向Jenkins服务发起http请求,请求参数是一个Github代码仓库的地址和分支名,Jenkins收到这个请求后,执行一个pipeline任务,该任务是下载指定的Github仓库的代码,流程如下图所示;

ZJ3iq2R.png!mobile

环境信息

  1. 操作系统:CentOS 7.7
  2. Jenkins:2.190.3
  3. Generic Webhook Trigger插件:1.66

关于Jenkins的部署,请参考文章《》

实战

  1. 先来安装插件Generic Webhook Trigger,如下图,进入插件管理页面:
    bmyymaf.png!mobile
  2. 安装插件的操作步骤如下图所示,请按照红框数字的顺序操作:
    NfemMju.png!mobile
  3. 稍等片刻,插件在线安装成功,如下图,接下来创建个pipeline任务试试:
    jeeIFn.png!mobile
  4. 新建名为remote-test的pipeline任务:
    miQRj2b.png!mobile
  5. 如下图红框所示,出现了Generic Webhook Trigger选项:
    bIZfM3M.png!mobile
  6. 勾选了Generic Webhook Trigger之后页面会发生变化,如下图,在红框位置,先设置token的值为token-remote-test,这样http请求中的token参数的值如果等于token-remote-test,就会触发当前任务:
    i6jmauy.png!mobile
  7. 接下来设置请求参数,如下图红框所示,先输入固定的ref参数,然后再把repositoryURL和branch也设置好,即Github代码仓库地址和分支名,这样http请求中repositoryURL和branch参数就能传递到后面的pipeline脚本中去了:
    Jn2qAfa.png!mobile
  8. 接下来可以写pipeline脚本了:
pipeline {
    agent any
    triggers {
        GenericTrigger(
            genericVariables: [
              [key: 'ref', value: '$.ref'],
              [key: 'repositoryURL', value: '$.repositoryURL'],
              [key: 'branch', value: '$.branch']
            ],
            token: 'token-remote-test' ,
            causeString: '$ref' ,
            printContributedVariables: true,
            printPostContent: true
        )
    }
    
    stages {
        stage('show-param') {
            steps {
                echo 'token参数:$token'
                echo '代码仓库:$repositoryURL'
                echo '代码分支:$branch'
            }
        }

        stage('down-sourcecode') {
            steps {
                echo '开始下载源码'
                checkout([$class: 'GitSCM', 
                    branches: [[name: '*/$branch']], 
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[url: '$repositoryURL']]])
            }
        }            
    }
}

上述脚本有以下几个关键点:

a. triggers 、GenericTrigger、genericVariables这几个参数是固定的,按上述示例去写即可;

b. printContributedVariables、printPostContent为true,会在执行任务时打印出请求参数的内容来;

c. 这里一共有两个stage,在show-param执行的时候,会将http请求参数全部打印出来;

d.checkout是pipeline提供的API,用于下载Github仓库的代码,其branches参数的值用上了http的请求参数branch,userRemoteConfigs.url参数的值用上了http的请求参数branchrepositoryURL

9. 上述pipeline脚本写在下图红框位置,然后点击底部的"Save"按钮保存:

Vjua6vm.png!mobile

10. 任务配置完成,接下来用postman发起http请求;

11. 我这边jenkins网站的地址是: http://192.168.133.149:32049 ,因此触发任务的请求地址是: http://192.168.133.149:32049/generic-webhook-trigger/invoke?token=token-remote-test ,注意token参数的值和任务设置中的token值保持一致就能触发任务;

12. 在Postman上的配置如下图所示,请按照数字顺序配置,repositoryURL参数的值为https://github.com/zq2599/jenkinsdemo.git,这是我放在Github上的一个java工程,可以正常下载:

eiYRNf7.png!mobile

13. 配置完成后点击Send按钮发送请求,正常情况下收到的返回码是200,如下图红框,如果非200(例如404),请检查参数和Jenkins任务的参数设置(例如token不一致):

eQZfErn.png!mobile

14. 回到Jenkins页面查看日志,如下图红框,请求参数都被打印出来了:

2uqUNvI.png!mobile

15. 继续往下看,可见Github源码成功下载:

2Ybqayn.png!mobile
  • 至此,通过Http触发Jenkins的pipeline实战就完成了,我们可以通过程序、脚本等各种手段,按照不同的需求来触发Jenkins任务,并传给任务不同的参数。

并发问题

  • 远程触发Jenkins任务虽然灵活方便,但是在处理并发请求时会有问题:10个请求同时到达,只有一个会执行,这个问题的破解方法留在下一篇文章《远程触发Jenkins的Pipeline任务的并发问题处理》详细讨论吧。

    https://github.com/zq2599/blog_demos

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK