当前位置:网站首页>Jenkins pipeline method (function) definition and call
Jenkins pipeline method (function) definition and call
2022-07-05 08:59:00 【Cloud computing security】
I checked some documents on the Internet ,Jenkins Pipeline Method call
There are still relatively few articles , Even if there is, the description is not very clear . So according to my own experience, I summed up how to Jenkinsfile Make method calls . If there is anything improper , Please correct .
First, let's talk about why we use Method
? There's a scene like this , I have more than a dozen remote servers , These servers are all my tests ( Or online ) The server , The package I built needs to be deployed to these servers , So I will define the relevant login information for each remote server ( user name 、 Password or key ) etc. . These configurations are actually the same for each server , does IP It's just different ( user name 、 I will keep the key or password consistent for the time being ), So I can define the same part as a method or function , And call these methods or functions in my pipeline stage , When you define too many functions , You can also write these functions Shared library
in , Then the pipeline calls the shared library .
This case uses declarative
Jenkinsfile For example !
1、 Define methods ( function )
Method defined in pipeline {}
outside , And in pipeline {}
Call within . as follows , I have defined the remote login information of a remote server , It is convenient for me to publish the built package to the remote server .
// Create a function
def RemoteHost(Jarnname, Host, ProJect) {
withCredentials([usernamePassword(credentialsId: '131', passwordVariable: 'PassWord', usernameVariable: 'UserName')]){
def remote = [:]
remote.name = "$Jarnname"
remote.host = "$Host"
remote.user = "$UserName"
remote.password = "$PassWord"
remote.port = 22
remote.allowAnyHosts = true
sshPut remote: remote, from: "./target/$Jarnname", into: "./${ProJect}/", override: true
}
}
// Pipeline steps
pipeline {
stages {
// Stage + step
}
}
2、 Calling method ( function )
Look at the following code block Project release
Stage , The function defined above is called here , And pass parameters .
// Create a function
def RemoteHost(Jarnname, Host, ProJect) {
withCredentials([usernamePassword(credentialsId: '131', passwordVariable: 'PassWord', usernameVariable: 'UserName')]){
def remote = [:]
remote.name = "$Jarnname"
remote.host = "$Host"
remote.user = "$UserName"
remote.password = "$PassWord"
remote.port = 22
remote.allowAnyHosts = true
sshPut remote: remote, from: "./target/$Jarnname", into: "./${ProJect}/", override: true
}
}
// Pipeline steps
pipeline {
agent {
docker {
image 'maven:v2'
args '-v /var/lib/jenkins/workspace/$JOB_NAME:/root/.m2 -v /root/mavenRepository:/usr/repository'
}
}
...
...
stages {
// Pull the code
stage(' Pull the code ') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH_TAG}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
gitTool: 'Default',
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://codeup.aliyun.com/xx/java_code.git',credentialsId: 'yourid',]]
])
}
// The project build
stage(" Build the project ") {
steps {
sh "mvn clean package -Dmaven.test.skip=true -P test"
}
}
// Project release ( Call the method here )
stage (" Project release ") {
steps {
script {
switch("$p_name"){
case "cms":
Jarnname = "demo.jar"
Host = "192.168.56.131"
ProJect = "cms"
// Call the method and pass parameters
RemoteHost("${Jarnname}", "${Host}", "${ProJect}")
break
}
}
}
}
...
...
}
}
In this case , We don't have to write the defined method content every time , Just call its function name .
Be careful : It's just me Jenkinsfile Part of !
thus ,Jenkins Pipeline Method definition and call have been completed .
边栏推荐
- 12. Dynamic link library, DLL
- Task failed task_ 1641530057069_ 0002_ m_ 000000
- kubeadm系列-01-preflight究竟有多少check
- ECMAScript6介绍及环境搭建
- Warning: retrying occurs during PIP installation
- 微信H5公众号获取openid爬坑记
- Blue Bridge Cup provincial match simulation question 9 (MST)
- Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
- Halcon shape_ trans
- Shift operation of complement
猜你喜欢
nodejs_ fs. writeFile
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
ROS learning 4 custom message
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Use and programming method of ros-8 parameters
Programming implementation of ROS learning 5-client node
Blogger article navigation (classified, real-time update, permanent top)
Hello everyone, welcome to my CSDN blog!
Applet (global data sharing)
Applet (use of NPM package)
随机推荐
Understanding rotation matrix R from the perspective of base transformation
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
File server migration scheme of a company
Nodejs modularization
Programming implementation of ROS learning 2 publisher node
The location search property gets the login user name
Ros-11 common visualization tools
迁移学习和域自适应
Applet (global data sharing)
Redis implements a high-performance full-text search engine -- redisearch
Yolov4 target detection backbone
优先级队列(堆)
Rebuild my 3D world [open source] [serialization-1]
One dimensional vector transpose point multiplication np dot
JS asynchronous error handling
Multiple linear regression (gradient descent method)
Huber Loss
notepad++
Mengxin summary of LCs (longest identical subsequence) topics
Wechat H5 official account to get openid climbing account