当前位置:网站首页>Application and practice of Jenkins pipeline
Application and practice of Jenkins pipeline
2022-07-02 15:09:00 【Cloud computing security】
Catalog
Preface
Let's talk today Jenkins Pipeline, namely CD Pipeline implementation of , I believe that for operation and maintenance 、 Development and testing ,Jenkins No stranger , It realizes the pull of our project 、 structure 、 Release 、 After construction, wait for a series of operations . For traditional build operations , This series of operations requires various operations , In the face of hundreds of thousands of servers , A little bit of operation will appear inadequate .
So... Is derived Pipeline Tools , It is Jenkins A plug-in for ( You need to install the plug-in manually ), Its main feature is :
- Execute in order ;
- Pull Warehouse source code ;
- code review ;
- Audit trail .
Next, let's talk about basic grammar , And then through an actual pipeline script to achieve CD function .
One 、 Grammatical structure
Official references :https://www.jenkins.io/doc/
1.1 declarative
1、 Grammar format
pipeline {
agent any
stages {
stage('Build') {
steps {
//
}
}
stage('Test') {
steps {
//
}
}
stage('Deploy') {
steps {
//
}
}
}
}
2、 Functional specifications
First, declarative syntax has a pipeline {} Structure , All phases or steps need to be executed in this structure , The bottom layer is based on groovy Language implementation .
- agent: Where is it
nodeWhich agent tools are used to perform pipeline tasks on ,any Express arbitrarily . - stages: Express
Stage, One pipeline {} There can only be one in the structure stages {} Stage . - stage: Express
Sub stage, Included in stages {} in , There can be multiple stage() {}. - steps: Express
step, Included in stage() {} in , There can only be one steps {}. - //: The double slash here is a comment , The content of this part is Jenkins Concrete implementation of declarative pipeline .
That is to say, declarative grammar can only be realized by including at least the above stages and steps .
1.2 Scripted
1、 Grammar format
node {
stage('Build') {
//
}
stage('Test') {
//
}
stage('Deploy') {
//
}
}
2、 Functional specifications
First, declarative syntax has a node {} Structure , All phases need to be executed in this structure , Its bottom layer is also based on groovy Language implementation . Be careful , There is no steps, Only through stage That is to say .
- stage: Express
Stage,stage() {} Used to realize specific pipeline functions . - //: The double slash here is a comment , The content of this part is Jenkins The concrete implementation of scripted pipeline .
That is to say, scripted syntax can only be realized by including at least the above stages and steps .
Two 、 Application practice
2.1 How to choose grammar ?
In practical application scenarios , Usually, the two grammatical formats are used together , Generally, script syntax is used in declarative syntax , Achieve a complementary effect . If your CD There are more logical judgments in , You can choose scripted pipeline script , Otherwise, declarative pipeline scripts are preferred .
2.2 Spring Boot
This case is based on Spring Boot Project to realize pipeline function .
1、 To write Jenkinsfile
It can be done by Blue Ocean、 Fragment generator and manual writing generate this file , Can be uploaded to Git Warehouse version management .
def getHost(){
def remote = [:]
remote.name = "$SEV"
remote.host = "$SEV"
remote.user = "$UserName"
remote.port = xxxx
remote.password = "$PassWord"
remote.allowAnyHosts = true
return remote
}
pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-v $HOME/.m2:/root/.m2'
}
}
parameters {
gitParameter (name: 'YOU_BRANCH',
type: 'PT_BRANCH_TAG',
branchFilter: 'origin/(.*)',
defaultValue: 'master',
selectedValue: 'DEFAULT',
sortMode: 'DESCENDING_SMART',
description: ' Please select build branch ')
choice(name: 'YOU_SEV',
choices: ['xx.xx.xx.xx', 'xx.xx.xx.xx'],
description: ' Please select a remote server ',)
extendedChoice(name: 'YOU_JOB',
description: ' Please select the project to be published ',
multiSelectDelimiter: ',',
quoteValue: false,
saveJSONParameterToFile: false,
type: 'PT_CHECKBOX',
value: 'all,a,b,c,d',
visibleItemCount: 10)
choice(name: 'YOU_ENV',
choices: ['test', 'pro'],
description: ' Please select the environment ')
booleanParam(name: 'START',
defaultValue: 'false',
description: ' Whether to start after construction ')
}
stages {
stage('pull Code ') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH_TAG}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
gitTool: 'Default',
submoduleCfg: [],
userRemoteConfigs: [[url: 'http://xxx/test/project.git',credentialsId: 'git',]]
])
}
}
stage("build All bags ") {
when {
environment name: 'YOU_JOB', value: 'all'
}
steps {
sh 'mvn clean package -Dmaven.test.skip=true -P test'
}
}
stage("build Specified package ") {
when {
not {
environment name: 'YOU_JOB', value: 'all'
}
}
steps {
script {
for (pre_job in YOU_JOB.tokenize(',')){
// Automatically solve build dependency problems
sh "&& mvn clean install -pl ${pre_job} -am -Dmaven.test.skip=true"
}
}
}
}
stage ("deploment") {
steps {
script {
for (p_name in YOU_JOB.tokenize(',')){
sshPut remote: server, from: "${WORKSPACE}/${p_name}/target/${p_name}.jar", into: "/opt/$ENV/${p_name}/", override: true
}
}
}
}
stage('start_project') {
steps {
script {
if ( START == 'true' ) {
for (p_item in YOU_JOB.tokenize(',')){
withCredentials([usernamePassword(credentialsId: 'Test-Platform', passwordVariable: 'PassWord', usernameVariable: 'UserName')]){
script {
def remote = [:]
remote.name = "$SEV"
remote.host = "$SEV"
remote.user = "$UserName"
remote.port = xxxx
remote.password = "$PassWord"
remote.allowAnyHosts = true
// Execute startup script ( Start the specified project )
sshCommand remote: remote, command: "sudo sh start.sh"
}
}
}
} else {
echo ' Manual start '
}
}
}
}
}
}
scene : The root project has multiple subprojects
Realization function :
- structure : All items 、 Designated project ;
- start-up : Start automatically after the build 、 Start manually after the build .
2、 New assembly line project

3、 Add pipeline script
Copy and paste the pipeline script generated above into the box below .

4、 Finally, click build

3、 ... and 、 summary
1、 Grammatical choice : declarative 、 Scripted combination
2、 structure : A parameterized 、 many ( single ) The project build
3、 Script management : Pictured above , Just write the script directly , Of course, it can also be Git Version management , As shown in the figure below :

边栏推荐
- 07_哈希
- C language exercises - (array)
- Reuse and distribution
- 08_ 串
- Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
- Jenkins Pipeline 应用与实践
- TiDB 集群最小部署的拓扑架构
- 牛客练习赛101
- C # delay, start the timer in the thread, and obtain the system time
- Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
猜你喜欢

LeetCode 209. 长度最小的子数组
![[noi Simulation Competition] scraping (dynamic planning)](/img/ee/27a07f80207a2925f5065e633eb39f.png)
[noi Simulation Competition] scraping (dynamic planning)

06_栈和队列转换

MFC 定时器使用

Learn the method code example of converting timestamp to uppercase date using PHP

taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface

蜻蜓低代码安全工具平台开发之路

TiDB数据迁移工具概览

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

.NET Core 日志系统
随机推荐
mathjax 入门(web显示数学公式,矢量的)
传感器数据怎么写入电脑数据库
Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
Sharp tool SPL for post SQL calculation
C#延时、在线程中开启定时器、获取系统时间
可视化搭建页面工具的前世今生
AtCoder Beginner Contest 254
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
Base64 编码原来还可以这么理解
btrace-(字节码)动态跟踪工具
Mavn 搭建 Nexus 私服
数据库内容输出有问题怎么解决
学习使用php实现公历农历转换的方法代码
About text selection in web pages and counting the length of selected text
08_ 串
使用 TiUP 部署 TiDB 集群
CTO如何帮助业务?
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Database connection pool and data source
Recommended configuration of tidb software and hardware environment