Jenkins skills of the same type and different types of job call and parameter transfer (triggered with parameters)

1. Background

Before entering the topic, first introduce the dependencies between projects . What is a dependency? For example, the project Test1 needs to be built after the project Test2 component is completed. This is the dependency. To explain in the interface on Jenkins, it is the red area option in Figure 1.

Insert picture description here

Environmental preparation

  1. a computer
  2. Jenkins environment (available online, you can find it at will)

2. Mutual calls between jobs of the same type

Mutual call and parameter transfer between Project Job

For those using local Jenkins, please install the Parameterized Trigger Plugin

In order to imitate this process, I created Test1 and Test2. The build content in each project is windows batch commonds. You can enter the commands at will.

1. The relevant configuration of Test1 is as follows

The first thing we need to know is that we want to passpackageNameThis parameter
(1) first set this parameter in Test1. Insert picture description here
(2) Choose Trigger parameterized build on other projects

Projects to build select the project you want to trigger, here we writeTest2. Trigger when build is is selected as required.

You can define the parameters you want to pass in the predefined parameters, written as follows, or you can pass the environment variables of the current job to another project. Insert picture description here
So Test1 is set

2. The relevant configuration of Test2 is as follows

For the configuration in Test2, select the parameterized build process. The parameter name should be the same as the one just written! [Insert the picture description here]
Verify that the parameters are passed correctly in windows batch commonds
Insert picture description here
3. Finally, let's take a look at the console output of the two Jobs. TheInsert picture description here
first is the console output of Test1. You can see that Test 2 is called

Then we look at the console output of Test2 and we
Insert picture description here
can see that, triggered by Number4 of Test1, the packageName parameter we passed was also passed.

Mutual call and parameter transfer between pipeline jobs

Let's introduce how to connect two JOBs in the pipeline code. We know that in the free-style job setting header in jenkins of the graphical interface, we can choose to trigger the execution of Job B after executing job A, so how does the pipeline realize this process?

Environmental preparation

Currently my JobA is named ProjectA-pipeline-demo, and JobB is named ProjectA1-pipeline-demo

1. The relevant configuration of ProjectA-pipeline-demo is as follows.
This is a simple pipeline job I wrote locally. The build function can be used to call and pass parameters between pipeline jobs.

// An highlighted block
import hudson.model.*;
 
 
pipeline{
    
     
	
	agent any
	stages{
    
    
		stage("Hello Pipeline") {
    
    
			steps {
    
    
			    script {
    
    
					println "Hello Pipeline!"
					println env.JOB_NAME
					println env.BUILD_NUMBER
				}
			}
		}
		
		stage("Init paramters in json") {
    
    
			steps {
    
    
			    script {
    
    
					println "read josn input file"
					def packagename = "xxx"
					println packagename
				    callTestingJob(packagename)
				}
			}
		}	
	}
}
def callTestingJob(packagename){
    
    
    println packagename
	build job:"1", propagate: false, wait: true,parameters: [
	     string(name:'packagename', value: packagename)
	                   ]	
}


3. Mutual calls between different types of jobs

Pipeline Job calls Project Job and passes parameters

Above we learned how to call and pass parameters between the same types. Then, how to connect different types of Jobs in series. Here we name the pipeline job ProjectB-pipeline-demo, and the Project Job name 1.

1. The relevant configuration of ProjectB-pipeline-demo is as follows

This is a simple pipeline code I wrote locally

// An highlighted block
import hudson.model.*;
 
 
pipeline{
    
     
	
	agent any
	stages{
    
    
		stage("Hello Pipeline") {
    
    
			steps {
    
    
			    script {
    
    
					println "Hello Pipeline!"
					println env.JOB_NAME
					println env.BUILD_NUMBER
				}
			}
		}
		
		stage("Init paramters in json") {
    
    
			steps {
    
    
			    script {
    
    
					println "read josn input file"
					def packagename = "xxx"
					println packagename
				    callTestingJob(packagename)
				}
			}
		}	
	}
}
def callTestingJob(packagename){
    
    
    println packagename
	build job:"1", propagate: false, wait: true,parameters: [
	     string(name:'packagename', value: packagename)
	                   ]	
}


Define a callTestingJob function to add the code to call Job B. transferpackagenameThis parameter.

2. The relevant configuration of Project 1 is as follows.
Insert picture description here
Add a parameter whose name is the same as that of ProjectB-pipeline-demo. Insert picture description here
Test with windows batch commonds to see if packeageName can be printed.

3. Look at the console output

Insert picture description here

The first is the console output of ProjectB-pipeline-demo. You can see that 1 is called

Then we look at the console output of 1 and we
Insert picture description here
can see that, triggered by Number 34 of ProjectB-pipeline-demo, the packageName parameter we passed was also passed.

references

【1】https://blog.csdn.net/winter199/article/details/90604933
【2】https://blog.csdn.net/u011541946/article/details/89344002

Guess you like

Origin blog.csdn.net/weixin_43841844/article/details/109636156