当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Halcon snap, get the area and position of coins

Programming implementation of ROS learning 5-client node

fs. Path module

Solutions of ordinary differential equations (2) examples

Blogger article navigation (classified, real-time update, permanent top)

Halcon shape_ trans

Wechat H5 official account to get openid climbing account

深度学习模型与湿实验的结合,有望用于代谢通量分析

TF coordinate transformation of common components of ros-9 ROS

The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
随机推荐
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
My university
[牛客网刷题 Day4] JZ55 二叉树的深度
Array, date, string object method
Mengxin summary of LIS (longest ascending subsequence) topics
kubeadm系列-00-overview
Hello everyone, welcome to my CSDN blog!
Meta tag details
[beauty of algebra] solution method of linear equations ax=0
[matlab] matlab reads and writes Excel
Typescript hands-on tutorial, easy to understand
多元线性回归(sklearn法)
js异步错误处理
.NET服务治理之限流中间件-FireflySoft.RateLimit
MPSoC QSPI flash upgrade method
Jenkins Pipeline 方法(函数)定义及调用
File server migration scheme of a company
Kubedm series-00-overview
asp.net(c#)的货币格式化
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)