当前位置:网站首页>Continuous delivery jenkinsfile syntax
Continuous delivery jenkinsfile syntax
2022-06-25 10:37:00 【Tester_ muller】
Realization Pipeline The functional scripting language is called Jenkinsfile, from Groovy Language implementation .Jenkinsfile It is usually placed in the project root directory , Controlled by source control software along with the project , There is no need to create " Free style \" Same project , You may need to copy many settings to a new project at a time , Provides some direct benefits :
- Pipeline Code review on / iteration
- Pipeline Audit trail for
- Pipeline The only real source of , Can be viewed and edited by multiple members of the project Pipeline Support :Declarative( stay Pipeline 2.5 Introduction in ) and Scripted Pipeline Two formats . Both support the establishment of Pipeline, Both can be used in Web UI Define a pipeline in Jenkinsfile, take Jenkinsfile Creating and checking files into the source code control library is often considered a best practice .
Declared Pipeline
Declared Pipeline Trees
Declared Pipeline Must be included in fixed format Pipeline {} block , Each declaration statement must have a separate line , There is no need to use a semicolon at the end of the line . block ( blocks{} ) Only chapters can be included (Sections), Instructions (Directives), step (Steps) Or assignment statement .
block blocks{}
A statement enclosed in braces , Such as Pipeline{},Section{},parameters{},script{}
chapter (Sections)
A chapter usually contains one or more instructions or steps . Such as agent 、post、stages、steps
Instructions (Directives)
environment、options、parameters、triggers( Trigger )、stage、tools、when
node (agent)
There must be ,agent Must be in Pipeline The top level definition within the block , but stage It is optional whether to use in
Parameters :any/none/label/node/docker/dockerfile
Common options label/cuetomWorkspace/reuseNode
Example :
agent { label 'my-label' }
agent {
node {
label 'my-label'
customWorkspace '/some/other/path'
}
}
agent {
docker {
image 'nginx:1.12.2'
label 'my-label'
args '-v /tmp:/tmp'
}
}
After construction (post)
It's not necessary , be used for Pipeline The outermost layer of or stage{} in , Mainly used to express Jenkins What needs to be done after the build action is completed .
Example :
pipeline {
agent any
stages {
stage('Example'){
steps {
echo 'Hello world'
}
}
}
post {
always {
echo 'say goodbay'
}
}
}
Stage set (stages)
There must be , Includes one or more that are executed sequentially stage command , stay Pipeline It can only be used once in the , Usually located in agent/options Back .
step (steps)
There must be ,steps be located stage Inside the instruction block , Include one or more step. There is only one step Keywords can be ignored if step And its {}.
Environmental Science (environment)
It's not necessary ,environment Defines a set of global environment variable key value pairs , Exist in pipeline {} perhaps stage In command . Perform special methods credentials() Can get Jenkins Document clear text content predefined in .
Example :
environment {CC='clang'}
environment {AN_ACCESS_KEY = credentials('my-prefined-secret-text')}
steps {sh 'printenv'}
Options (options)
It's not necessary , predefined Pipeline Proprietary configuration information , Can only be defined once
Example :
pipeline {
agent any
options{
timeout(time:1,unit: 'HOURS')
}
}
Parameters (parameters)
It's not necessary , Define the parameters of the parametric build optional parameters , Parameter type booleanParam,choice,file,text,password,run,string
Example :
parameters {
string(name: 'PERSON', defaultValue: 'Jenkins', description: ' Input text parameters ')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
}
trigger (rtiggers)
It's not necessary , Definition Pipeline Automatically triggered mode options cron、pollSCM、upstream
Example :
triggers {
cron('0 8 * * 1-5')
}
Script Pipeline
Script Pipeline Statement tree
One Script Pipeline It can be divided into several Stage, Every Stage Represents a group of operations , for example Build,Test;Node representative Jenkins node , such as Master, Slave Such a node ;Step Is the most basic unit of operation , In the corresponding Node The action statements executed on the node are written directly in the node {} in .
Flow control statement
Simultaneous interpreting script language is the same as traditional script language. ,Script Pipeline It is executed from top to bottom , have access to Groovy Expression for process control , Such as if/else Statement controls the process through logical condition judgment :
node {
stage('Example') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
Exception handling statements
Script Pipeline Another way of script flow control is the exception handling mechanism . When any step is abnormal for various reasons , Must be used in code try/catch/finally Statement block for exception capture , And through the preset code for processing , Ensure that the script can execute smoothly :
stage('Error Handling') {
node{
echo "This is test demo for the error handling"
try {
echo "This is in the try block."
sh 'exit 1'
}catch (exc) {
echo "Something failed, I'm in the catch block."
}finally {
echo "Finally, I'm in the finally block."
}
}
}
Jenkinsfile Call in plugin function
stay Jenkinsfile In addition to using regular logic 、 Process control , You can also call Jenkins Plug in function of . The following examples are used to illustrate .
Email Extension plug-in unit
Email Extension yes Jenkins An external plug-in in , Used to send mail , from Jenkins Of Plugin Manager Installation in . stay Pipeline The operation of the plug-in can be triggered through code calls in , Realize the function of sending mail .
Basic configuration
After installation Email Extension After the plug-in , First, you need to select one to send Jenkins Email address of notification mail , The specific parameters of the email should be known in advance ( Generally, the parameters can be found on the configuration parameter information page of the mailbox ); After that Manage Jenkins -> Configure System Fill the corresponding parameters in the corresponding configuration items of the plug-in , The parameters that must be configured are as follows :
- SMTP server:smtp Service address
- SMTP port:smtp Port number
- Use SMTP Authentication: Enable smtp Security check
- User Name: Sender's user name
- Password: Sender's password
- Default Recipients: Default recipient Other parameters can be configured according to your own needs ,Email Extension The configuration reference screenshot is as follows :
Pipeline Code
pipeline{
agent {
label 'master'
}
stages{
stage(' Send email test ') {
steps{
echo 'Test Email'
}
}
}
post {
always {
emailext body: '$DEFAULT_CONTENT', recipientProviders: [[$class: 'RequesterRecipientProvider']], subject: '$DEFAULT_SUBJECT'
}
}
}
边栏推荐
- Difference between malloc and calloc
- QT: parsing JSON
- String implementation strstr()
- Tutorial on installing SSL certificates in Microsoft Exchange Server 2007
- The path of Architects
- MCU development -- face recognition application based on esp32-cam
- WPF 绑定表达式和绑定数据源(一)
- Shardingsphere proxy 4.1 sub database and sub table
- [image fusion] image fusion based on morphological analysis and sparse representation with matlab code
- Dell technology performs the "fast" formula and plays ci/cd
猜你喜欢
Your driver settings have been set to force 4x antialiasing in OpenGL applications
Mongodb's principle, basic use, clustering and partitioned clustering
[paper reading | deep reading] line: large scale information network embedding
网络协议学习---LLDP协议学习
Binder explanation of Android interview notes
什么是 CRA
[image fusion] image fusion based on morphological analysis and sparse representation with matlab code
单片机进阶---PCB开发之照葫芦画瓢(二)
【RPC】I/O模型——BIO、NIO、AIO及NIO的Rector模式
The left sliding menu +menu item icon is grayed out
随机推荐
Handler asynchronous message processing
NETCORE performance troubleshooting
String longest common prefix
Flask博客实战 - 实现侧边栏最新文章及搜索
Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}
DigiCert和GlobalSign单域名OV SSL证书对比评测
P2P network core technology: Gossip protocol
ShardingSphere-Proxy 5.0 分库分表(一)
MCU development -- face recognition application based on esp32-cam
Principle of distribution: understanding the gossip protocol
【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
Mqtt beginner level chapter
OpenCV学习(二)---树莓派上安装opencv
DDS learning notes of opendds
手机办理广州证券开户靠谱安全吗?
Google Earth Engine(GEE)——evaluate實現一鍵批量下載研究區內的所有單張影像(上海市部分區域)
[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
Yolov5更换上采样方式
Difference between malloc and calloc
Kotlin implements a simple login page