当前位置:网站首页>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 .
边栏推荐
- Basic number theory - fast power
- Confusing basic concepts member variables local variables global variables
- 多元线性回归(sklearn法)
- Basic number theory - factors
- Meta标签详解
- It cold knowledge (updating ing~)
- 嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
- How many checks does kubedm series-01-preflight have
- Nodemon installation and use
- [daiy4] jz32 print binary tree from top to bottom
猜你喜欢

Halcon affine transformations to regions

fs. Path module

Halcon shape_ trans

Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)

Shift operation of complement

微信H5公众号获取openid爬坑记
![Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]](/img/ed/0483c529db2af5b16b18e43713d1d8.jpg)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]

Applet (subcontracting)

My experience from technology to product manager

Applet (global data sharing)
随机推荐
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
Use and programming method of ros-8 parameters
ROS learning 1- create workspaces and function packs
[牛客网刷题 Day4] JZ35 复杂链表的复制
Solutions of ordinary differential equations (2) examples
[Niuke brush questions day4] jz55 depth of binary tree
[daily training] 1200 Minimum absolute difference
MPSoC QSPI Flash 升级办法
kubeadm系列-02-kubelet的配置和启动
优先级队列(堆)
Programming implementation of subscriber node of ROS learning 3 subscriber
[formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
多元线性回归(梯度下降法)
asp. Net (c)
.NET服务治理之限流中间件-FireflySoft.RateLimit
File server migration scheme of a company
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
[牛客网刷题 Day4] JZ55 二叉树的深度
Illustrated network: what is gateway load balancing protocol GLBP?