当前位置:网站首页>Pipeline shared library
Pipeline shared library
2022-06-24 12:22:00 【Chen Bucheng I】
One . brief introduction
When used in large quantities pipeline after , The built-in function does not cover all the requirements , Extension is required at this time pipeline.
pipeline It's essentially a Groovy Script . therefore , Can be in pipeline Define function in , And use Groovy Scripting features of the language . We defined createVersion function , And used Date class
def createVersion(String BUILD_NUMBER){returnnewDate().format('yyMM')+"-${BUILD_NUMBER}"}pipeline {agent anystages {stage('Build'){steps {echo "${createVersion(BUILD_NUMBER)}"}}}}
The log is as follows :
There is a more elegant way of writing , Define the variable in environment part
def createVersion(String BUILD_NUMBER){returnnewDate().fromat('yyMM')+"-${BUILD_NUMBER}"}pipeline {agent anyenvironment {_version = createVersion(BUILD_NUMBER)}stages {stage('Build'){steps {echo "${_version}"}}}}
If in a Jenkinsfile Define a function , It's harmless . But if 20 individual Jenkinsfile This function is defined repeatedly in 20 All over , There's a problem .
Two . Shared library extensions
Jenkins pipeline Provides “ Shared library ”(Shared library) technology , Duplicate code can be defined in a separate code control repository , Other Jenkins pipeline Load and use it . Similar to the module package in programming ( The actual is ), Other methods can be cited , Directly in the present pipeline Use .
Create a shared library project , The directory structure is as follows
Push the code to git Warehouse , Get into Jenkins Of Manage Jenins-》Configure System -》Global Pipeline Libraries Configuration page
Configuration item :
- Name : Unique identification of the shared library , stay Jenkinsfile Will be used in .. Default version : The default version . Can be branch name 、tag Labels etc. .
- Load implicitly: Implicitly load . If this is checked , The global shared library will be automatically loaded , stay Jenkinsfile No explicit reference is required in , You can use it directly .
- Allow default version to be overridden : If this is checked , Is allowed “Default version” By Jenk-insfile Configuration overrides in .
- [email protected] changes in job recent changes: If this is checked , Then the last change information of the shared library will be printed in the construction log together with the change information of the project . .- Retrieval method: How to get the shared library code . We choose Modern SCM” Options , Then choose to use Git Warehouse .
Tips : Except that it can be used Git Warehouse hosting shared library code , You can also use SVN Warehouse hosting . Use different code repositories to host ,“Default version” The value of is written differently . This book only introduces Git Warehouse trusteeship .
Shared library uses
stay pipeline Call inside
@Library('global-shared-library')_pipeline {agent anystages {stage('Build'){steps {sayHello("world")}}}}
stay Jenkins pipeline At the top of the , Use @Library Specify the shared library . Be careful ,global-shared-library Is the shared library identifier we defined in the previous step .
After importing the shared library , We can go straight to Jenkins pipeline Use in vars In the catalog sayHello, and Jenkins pipeline The common steps of are used in the same way .
thus , The complete definition and basic use of a shared library are introduced . To sum up, there are four steps : 1. According to the source code structure agreed by the shared library , Realize your own logic .
2. Hosting shared library code into a code warehouse .
3. stay Jenkins Shared libraries are defined in the global configuration , In order to let Jenkins Know how to get shared library code .
4. stay Jenkinsfile Use in @Library Reference shared library .
Use @Library Annotations can specify the version of the shared library in the code repository .
@Library('[email protected]<version>')_<version> It can be :
because Jenkins Support adding multiple shared libraries at the same time , therefore @Library Annotations also allow us to introduce multiple shared libraries at the same time , Such as :@Library ( [‘global-shared-library’ , ‘[email protected]]).
It should be noted that ,Jenkins The way to handle functions with the same name in multiple shared libraries is to define them first and take effect . in other words , If global-shared-library And otherlib There is a with the same name sayHello, and @Library The introduction of global-shared-library stay otherlib front , So there's only global-shared-library Of sayHello take effect .
Shared library structure
Review the catalog
First of all to see vars Catalog . Put it in vars The directory can be from pipeline Directly called global variables , The file name of the variable is in pipline Function name called in , The file name is humped .
Use vars Global variables under the directory can call Jenkins pipeline Steps for . just as sayHello.groovy Script , Use it directly echo step
def call(String name ='human'){echo "Hello, ${name}."}
When we're in Jenkins Write in sysHello(“world”) when , What it actually calls is sysHello.groovy In the document call function .
call Function also supports receiving closures (Closure), Under the case , We define a mvn Global variables .
// vars/mvn.groovydef call(mvnExec){configFileProvider([configFile(fileId:'maven-global-settings', variable:'MAVEN_GLOBAL_ENV')]){mvnExec("${MAVEN_GLOBAL_ENV}")}}
above call The content of the function is to configFileProvider Wordy writing is encapsulated in mvn variable . In this way, we can perform more succinctly mvn The command .
@Libray('[email protected]') _pipeline {agent anytools {maven 'mvn-3.5.4}stages {stage('Build'){steps {mvn { settings ->sh "mvn -s ${settings} clean install"}}}}}
边栏推荐
- I'm in Shenzhen. Where can I open an account? Is it safe to open an account online now?
- 【老卫搞机】090期:键盘?主机?全功能键盘主机!
- 最新热点:使用铜死亡相关基因进行肿瘤预后分型!
- Which commercial insurance endowment insurance is good? Ranking of commercial endowment insurance products in 2022
- GLOG从入门到入门
- Is it safe to apply for new bonds to open an account
- Adjustment method of easynvr video platform equipment channel page display error
- 分布式系统解决之道:目录、消息队列、事务系统及其他
- About the unsupported instruction set SSE 4.2 of CPU in virtualization
- 夜晚读书 -- 关于微服务和容器
猜你喜欢

How to write controller layer code gracefully?

qt -- QTabWidget 中支持拖拽TabBar项

Basic path test of software test on the function of the previous day

【数字IC/FPGA】Booth乘法器

Group planning - General Review
Cloud native database: the outlet of the database, you can also take off

"Meng Hua Lu" is about to have a grand finale. It's better to learn it first than to look ahead!
![[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map

GTest从入门到入门

Install Kali on the U disk and persist it
随机推荐
Tsingsee green rhino video "cloud side end" +ai intelligent security system is integrated into the mainstream development trend
炒伦敦金短线稳定赚钱技巧?在哪里炒伦敦金安全靠谱?
u盘安装kali并且持久化
打新债的条件 开户是安全的吗
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
Concentrate on research preparation, Tencent cloud, see you next year!
Is it safe to apply for new bonds to open an account
9+! Predicting lymph node status from histology of colorectal cancer by deep learning
Is GF Securities reliable? Is it safe to open a securities account?
怎样申请打新债 开户是安全的吗
I'm in Shenzhen. Where can I open an account? Is it safe to open an account online now?
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
Insurance app aging service evaluation analysis 2022 issue 06
最新热点:使用铜死亡相关基因进行肿瘤预后分型!
[digital ic/fpga] booth multiplier
Can Tencent's tendis take the place of redis?
《opencv学习笔记》-- CV::Mat类
我在深圳,到哪里开户比较好?现在网上开户安全么?
怎么申请打新债 开户是安全的吗
美团基于 Flink 的实时数仓平台建设新进展