当前位置:网站首页>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 .
边栏推荐
- scipy.misc.imread()
- kubeadm系列-02-kubelet的配置和启动
- Halcon blob analysis (ball.hdev)
- 什么是防火墙?防火墙基础知识讲解
- Multiple linear regression (sklearn method)
- fs. Path module
- Configuration and startup of kubedm series-02-kubelet
- Programming implementation of ROS learning 2 publisher node
- [daiy4] jz32 print binary tree from top to bottom
- Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
猜你喜欢
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

资源变现小程序添加折扣充值和折扣影票插件

My university

TF coordinate transformation of common components of ros-9 ROS

Halcon color recognition_ fuses. hdev:classify fuses by color
![[matlab] matlab reads and writes Excel](/img/80/78e4c7fcd27473526e480d4b930e2c.jpg)
[matlab] matlab reads and writes Excel

容易混淆的基本概念 成员变量 局部变量 全局变量
![[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis](/img/59/823b587566f812c76e0e4dee486674.jpg)
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis

牛顿迭代法(解非线性方程)
![[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation](/img/d8/7291a5b14160600ba73810e6dd1eb5.jpg)
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
随机推荐
C#绘制带控制点的Bezier曲线,用于点阵图像及矢量图形
3D reconstruction open source code summary [keep updated]
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
AdaBoost use
Mengxin summary of LCs (longest identical subsequence) topics
生成对抗网络
Configuration and startup of kubedm series-02-kubelet
Applet (subcontracting)
asp.net(c#)的货币格式化
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
kubeadm系列-02-kubelet的配置和启动
Shift operation of complement
Characteristic Engineering
Blue Bridge Cup provincial match simulation question 9 (MST)
皮尔森相关系数
[daiy4] jz32 print binary tree from top to bottom
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Use arm neon operation to improve memory copy speed
Summary of "reversal" problem in challenge Programming Competition