Remotely trigger Jenkins' Pipeline task

Scenes

  1. Although you can configure the Jenkins task to trigger when submitting code to Git, sometimes it does not need to be triggered every time you submit the code, but only executes when needed.
  2. In addition to manually executing tasks on the Jenkins page, you can also initiate an HTTP request to the Jenkins website to trigger the execution of specified tasks. This article will actually trigger the execution of multiple Jenkins tasks through Http requests.

Overview

For pipeline-type Jenkins tasks, the Generic Webhook Trigger plug-in is generally used to support remote triggering. The following three points need to be noted during use:

  1. How to write the requested URL when the Jenkin task is triggered remotely;
  2. Http request parameters, how to be used as parameters of pipeline script;
  3. Suppose there is Jenkins task A. At a certain time, 10 requests triggering this task arrive at the same time. How does Jenkins handle it? (I will focus on this issue later)

In response to the above problems, let's do a real battle:

  1. Initiate http request to Jenkins service;
  2. The request parameters are the address and branch name of a Github code repository;
  3. After receiving this request, Jenkins executes a pipeline task;
  4. The task is to download the code of the specified Github repository, the process is shown in the following figure:
    Insert picture description here

Environmental information

  1. Operating system: CentOS 7.7
  2. Jenkins:2.190.3
  3. Generic Webhook Trigger插件:1.66

For the deployment of Jenkins, please refer to the article "Helm Deployment and Experience Jenkins"

Actual combat

  1. First install the plugin, as shown below, enter the plugin management page:
    Insert picture description here
  2. The steps to install the plug-in are shown in the figure below, please follow the order in the red box:
    Insert picture description here
  3. After a while, the plug-in is successfully installed online, as shown in the figure below, then try to create a pipeline task:
    Insert picture description here
  4. Create a new pipeline task named remote-test :
    Insert picture description here
  5. Enter the setting page, as shown in the red box below, the Generic Webhook Trigger option appears :
    Insert picture description here
  6. After the Generic Webhook Trigger is checked , the page will change. As shown in the figure below, in the red box, first set the value of token to token-remote-test , so that if the value of the token parameter in the http request is equal to token-remote-test , it will Trigger the current task:
    Insert picture description here
  7. Next, set the request parameters, as shown in the red box below, enter the fixed ref parameters (used by the plugin, you must enter), and then set the repositoryURL and branch , that is, the Github code warehouse address and branch name, so http The repositoryURL and branch parameters in the request can be passed to the subsequent pipeline script;
  8. Next, you can write the pipeline script:
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']]])
            }
        }            
    }
}

The above script has the following key points:

a. The parameters of triggers, GenericTrigger and genericVariables are fixed, just write according to the above example;

b. printContributedVariables and printPostContent are true, will print out the content of the request parameters when performing the task;

c. There are two stages here. When show-param is executed , all http request parameters will be printed out;

d. checkout is an API provided by the pipeline for downloading the code of the Github repository. The value of the branches parameter uses the http request parameter branch, and the value of the userRemoteConfigs.url parameter uses the http request parameter repositoryURL

  1. Write the above pipeline script in the red box below, and then click the Save button at the bottom to save:
    Insert picture description here
  2. After the task configuration is completed, use Postman to initiate a request to Jenkins to verify that the Jenkins task is triggered by the Http request;

verification

  1. The address of my Jenkins website is: http://192.168.133.149:32049 , so the request address to trigger the task is: http://192.168.133.149:32049/generic-webhook-trigger/invoke?token=token-remote -test , note that the value of the token parameter and the token value in the task settings must be the same;
  2. The configuration on Postman is shown in the figure below. Please configure it in numerical order. The value of the repositoryURL parameter is https://github.com/zq2599/jenkinsdemo.git . This is a java project I put on Github and can be downloaded normally. :
    Insert picture description here
  3. After the configuration is complete, click the Send button to send the request. The return code received under normal circumstances is 200, as shown in the red box below. If it is not 200 (for example, 404), please check the parameter settings of the Jenkins task (for example, the token is inconsistent):
    Insert picture description here
  4. Go back to the Jenkins page to view the log. The red box as shown below, the request parameters are printed out:
    Insert picture description here
  5. Continue to see that the Github source code is successfully downloaded:
    Insert picture description here
  6. At this point, the actual combat of Jenkins pipeline triggered by Http is completed. We can trigger Jenkins tasks according to different needs through various means such as programs and scripts, and pass different parameters to the tasks.

Concurrency issues

Remote triggering of Jenkins tasks is flexible and convenient, but there will be problems when processing concurrent requests: 10 requests arrive at the same time, only one will be executed . The details and solutions of this problem are in the next article "Remote triggering of Jenkins Pipeline task concurrency problem "Handling" detailed discussion;

Source code download

My Github address is as follows:

name link Remarks
Project Homepage https://github.com/zq2599/blog_demos The project's homepage on GitHub
git repository address (https) https://github.com/zq2599/blog_demos.git The warehouse address of the project source code, https protocol
git repository address (ssh) [email protected]:zq2599/blog_demos.git The warehouse address of the project source code, ssh protocol

There are multiple projects in it, please use jenkinsremotedemo , as shown in the red box below, this is a Java project, the remote-test file inside is the pipeline script used in this article, and the Java code (App.java) of this project can be sent to Jenkins Simultaneously initiate multiple remote trigger requests:
Insert picture description here

Welcome to pay attention to my public number: programmer Xinchen

Insert picture description here

Published 376 original articles · praised 986 · 1.28 million views

Guess you like

Origin blog.csdn.net/boling_cavalry/article/details/105189564