当前位置:网站首页>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
node
Which 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 :
边栏推荐
- 为什么只会编程的程序员无法成为优秀的开发者?
- About text selection in web pages and counting the length of selected text
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- . Net core logging system
- Jenkins Pipeline 应用与实践
- C # delay, start the timer in the thread, and obtain the system time
- Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
- Mavn 搭建 Nexus 私服
- Solve the problem that El radio group cannot be edited after echo
- Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
猜你喜欢
随机推荐
传感器数据怎么写入电脑数据库
N皇后问题的解决
php获取数组中键值最大数组项的索引值的方法
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
Tmall product details interface (APP, H5 end)
LeetCode_字符串_简单_412.Fizz Buzz
C#延时、在线程中开启定时器、获取系统时间
About text selection in web pages and counting the length of selected text
jmeter脚本参数化
使用 TiUP 部署 TiDB 集群
原则、语言、编译、解释
Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
Xilinx Vivado set *. svh as SystemVerilog Header
Yolov6 training: various problems encountered in training your dataset
关于网页中的文本选择以及统计选中文本长度
Printf function and scanf function in C language
牛客练习赛101
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
Leetcode - Search 2D matrix
Socket and socket address