当前位置:网站首页>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"}}}}}
边栏推荐
- The idea of "6 points + gene family" without experiment~
- 我在深圳,到哪里开户比较好?现在网上开户安全么?
- GLOG from getting started to getting started
- [live review] battle code pioneer phase 7: how third-party application developers contribute to open source
- Use go to process millions of requests per minute
- Coinbase will launch the first encrypted derivative product for retail traders
- Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 2)
- Database migration tool flyway vs liquibase (II)
- 11+! Methylation modification patterns based on m6A regulatory factors in colon cancer are characterized by different tumor microenvironment immune spectra
- 怎么可以打新债 开户是安全的吗
猜你喜欢

保险APP适老化服务评测分析2022第06期

如何优雅的写 Controller 层代码?

FreeRTOS概述与体验

qt -- QTabWidget 中支持拖拽TabBar项

美团基于 Flink 的实时数仓平台建设新进展
![[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

电商红包雨是如何实现的?拿去面试用(典型高并发)

GLOG from getting started to getting started
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/fa/e52bd8a1a404a759ef6ba88e8da0f0.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
【老卫搞机】090期:键盘?主机?全功能键盘主机!
随机推荐
Single gene pan cancer + simple experiment can be published 7 points+
【云驻共创】解读HarmonyOS 应用与服务生态
Kubernetes best practice: graceful termination
GTest从入门到入门
2022年有什么低门槛的理财产品?钱不多
9+! Predicting lymph node status from histology of colorectal cancer by deep learning
New progress in the construction of meituan's Flink based real-time data warehouse platform
5W1H talk about open source - what is open source?
As one of the bat, what open source projects does Tencent have?
11+的基于甲基化组和转录组综合分析识别葡萄膜黑色素瘤中新的预后 DNA 甲基化特征~
Variable parameter template implements max (accepts multiple parameters, two implementation methods)
Realization of alarm clock with AHK
[mysql_16] variables, process control and cursors
Is it safe to apply for new bonds to open an account
怎么申请打新债 开户是安全的吗
万名校园开发者花式玩AI,亮点看这张图就够啦!
我真傻,招了一堆只会“谷歌”的程序员!
Insurance app aging service evaluation analysis 2022 issue 06
qt -- QTabWidget 中支持拖拽TabBar项
《opencv学习笔记》-- 矩阵归一化 normalize()函数