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

  1. def createVersion(String BUILD_NUMBER){
  2. returnnewDate().format('yyMM')+"-${BUILD_NUMBER}"
  3. }
  4. pipeline {
  5.     agent any
  6.     stages {
  7.         stage('Build'){
  8.             steps {
  9.                 echo "${createVersion(BUILD_NUMBER)}"
  10. }
  11. }
  12. }
  13. }

The log is as follows :

There is a more elegant way of writing , Define the variable in environment part

  1. def createVersion(String BUILD_NUMBER){
  2. returnnewDate().fromat('yyMM')+"-${BUILD_NUMBER}"
  3. }
  4. pipeline {
  5. agent any
  6. environment {
  7. _version = createVersion(BUILD_NUMBER)
  8. }
  9. stages {
  10. stage('Build'){
  11. steps {
  12. echo "${_version}"
  13. }
  14. }
  15. }
  16. }

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

  1. @Library('global-shared-library')_
  2. pipeline {
  3. agent any
  4. stages {
  5. stage('Build'){
  6. steps {
  7. sayHello("world")
  8. }
  9. }
  10. }
  11. }

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

  1. def call(String name ='human'){
  2.     echo "Hello, ${name}."
  3. }

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 .

  1. // vars/mvn.groovy
  2. def call(mvnExec){
  3.     configFileProvider([configFile(fileId:'maven-global-settings', variable:'MAVEN_GLOBAL_ENV')]){
  4.         mvnExec("${MAVEN_GLOBAL_ENV}")
  5. }
  6. }

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 .

  1. @Libray('[email protected]') _
  2. pipeline {
  3.     agent any
  4.     tools {
  5.         maven 'mvn-3.5.4
  6. }
  7.     stages {
  8.         stage('Build'){
  9.             steps {
  10.                 mvn { settings ->
  11.                     sh "mvn -s ${settings} clean install"
  12. }
  13. }
  14. }
  15. }
  16. }
原网站

版权声明
本文为[Chen Bucheng I]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210602191027803H.html