当前位置:网站首页>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 :

边栏推荐
- Mavn 搭建 Nexus 私服
- How does CTO help the business?
- kityformula-editor 配置字号和间距
- MFC A对话框调用B对话框函数并传参
- Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
- Recommended configuration of tidb software and hardware environment
- info [email protected]: The platform “win32“ is incompatible with this module.
- qml 弹窗框架,可定制
- MFC 定时器使用
- Leetcode - Search 2D matrix
猜你喜欢

Reuse and distribution

【无标题】LeetCode 2321. 拼接数组的最大分数

buuctf-pwn write-ups (7)

【C语言】详解指针的初阶和进阶以及注意点(1)

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

数据分析思维分析方法和业务知识——业务指标

Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)

07_哈希

Dragonfly low code security tool platform development path

Li Chuang EDA learning notes 15: draw border or import border (DXF file)
随机推荐
Printf function and scanf function in C language
【NOI模拟赛】伊莉斯elis(贪心,模拟)
.NET Core 日志系统
Record an error report, solve the experience, rely on repetition
蜻蜓低代码安全工具平台开发之路
About text selection in web pages and counting the length of selected text
TiDB数据迁移场景综述
Leetcode - Search 2D matrix
数据分析常见的英文缩写(一)
MFC console printing, pop-up dialog box
Reuse and distribution
Large top heap, small top heap and heap sequencing
Find the maximum inscribed circle of the contour
使用 TiUP 部署 TiDB 集群
Btrace- (bytecode) dynamic tracking tool
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
. Net core logging system
Introduction to C language -- array
Socket and socket address
Learn the method code example of converting timestamp to uppercase date using PHP