当前位置:网站首页>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 .
边栏推荐
- Blogger article navigation (classified, real-time update, permanent top)
- RT-Thread内核快速入门,内核实现与应用开发学习随笔记
- One dimensional vector transpose point multiplication np dot
- Bit operation related operations
- 3D reconstruction open source code summary [keep updated]
- Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
- Huber Loss
- Applet (global data sharing)
- 某公司文件服务器迁移方案
- [formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
猜你喜欢
Redis implements a high-performance full-text search engine -- redisearch
Use and programming method of ros-8 parameters
Shift operation of complement
Beautiful soup parsing and extracting data
Nodemon installation and use
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
Halcon Chinese character recognition
[daiy4] copy of JZ35 complex linked list
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
Wechat H5 official account to get openid climbing account
随机推荐
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
深度学习模型与湿实验的结合,有望用于代谢通量分析
[daily training] 1200 Minimum absolute difference
驾驶证体检医院(114---2 挂对应的医院司机体检)
[Niuke brush questions day4] jz55 depth of binary tree
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
OpenFeign
Programming implementation of ROS learning 5-client node
ROS learning 4 custom message
kubeadm系列-00-overview
IT冷知识(更新ing~)
Programming implementation of ROS learning 2 publisher node
kubeadm系列-02-kubelet的配置和启动
JS asynchronous error handling
Halcon snap, get the area and position of coins
scipy.misc.imread()
Luo Gu p3177 tree coloring [deeply understand the cycle sequence of knapsack on tree]
asp. Net (c)
Nodemon installation and use
Introduction Guide to stereo vision (1): coordinate system and camera parameters