Jenkins share-library共享库

1,391次阅读
没有评论
Jenkins

share library是jenkins pipeline的一项以模块方式管理流水线代码,提高复用度,简练流水线编码。会编程的读者估计会好理解点,类似包、模块的意义,其工程目录结构如下

(root)
+- src                     # Groovy source files
|   +- org
|       +- foo
|           +- Bar.groovy  # for org.foo.Bar class
+- vars
|   +- foo.groovy          # for global 'foo' variable
|   +- foo.txt             # help for 'foo' variable
+- resources               # resource files (external libraries only)
|   +- org
|       +- foo
|           +- bar.json    # static helper data for org.foo.Bar

共享库的使用方式有以下几种:

  • 全局配置
  • 文件夹下配置
  • 隐式加载
  • 指定加载

代码库中的目录结构如下

[root@k8s-node02 jenkins-shared-library]# tree
.
├── Jenkinsfile
├── Jenkinsfile-oneline.groovy
├── README.md
├── src
│   └── io
│       └── github
│           └── devops
│               └── ws
│                   └── Maven.groovy
└── vars
    ├── mvn.groovy
    ├── mvn.txt
    └── pip.groovy

6 directories, 7 files

[root@k8s-node02 jenkins-shared-library]# cat src/io/github/devops/ws/Maven.groovy
package io.github.devops.ws;

def Build() {
    sh script: 'mvn clean package', label: 'Maven build'
}

def Fake() {
    sh 'echo do nothing but demonstration'
}
[root@k8s-node02 jenkins-shared-library]# cat vars/mvn.groovy
import io.github.devops.ws.Maven;

def fake() {
    new Maven().Fake()
}
[root@k8s-node02 jenkins-shared-library]# cat vars/pip.groovy
def call() {
    pipeline {
        agent any

        stages {
            stage('Demo') {
                steps {
                    println "hello, I'm from a shared-library"
                }
            }
        }
    }
}

例如使用指定加载,我们的流水线代码则如下

// 指定分支branch: devops-ws-demo@dev
// library identifier: 'devops-ws-demo@dev', retriever: modernSCM([
library identifier: 'devops-ws-demo@master', retriever: modernSCM([
    $class: 'GitSCMSource',
    remote: 'https://github.com/devops-ws/jenkins-shared-library',
    traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]
])

pipeline {
    agent any

    stages {
        stage('Demo') {
            steps {
                script {
                    mvn.fake()
                }
            }
        }
    }
}

此时运行我们的流水线输出

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] library
Loading library devops-ws-demo@master
Attempting to resolve master from remote references...
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git ls-remote -h -- https://github.com/devops-ws/jenkins-shared-library # timeout=10
Found match: refs/heads/master revision 188c2e3bfe60b8a2f659432ca297ebb1e273a34e
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository https://github.com/devops-ws/jenkins-shared-library
 > git init /var/jenkins_home/workspace/demo01@libs/8aee1be2e90d14a3350006b889aa7de47ea2b58bc128bf1527d9d130eba0f977 # timeout=10
Fetching upstream changes from https://github.com/devops-ws/jenkins-shared-library
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --no-tags --force --progress -- https://github.com/devops-ws/jenkins-shared-library +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/devops-ws/jenkins-shared-library # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision 188c2e3bfe60b8a2f659432ca297ebb1e273a34e (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 188c2e3bfe60b8a2f659432ca297ebb1e273a34e # timeout=10
Commit message: "Provide a better default"
First time build. Skipping changelog.
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/demo01
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Demo)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo do nothing but demonstration
do nothing but demonstration
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

若是使用全局共享配置,则需要去jenkins dashboard>manager jenkins>configure system中找到Global Pipeline Libraries

Jenkins

配置好后我们的流水线代码如下

@Library('devops-ws-demo') _
// 指定branch: devops-ws-demo@test
//@Library('devops-ws-demo@test') _

//指定多个共享库
//@Library(['my-shared-library', 'otherlib@abc1234']) _
pipeline {
    agent any

    stages {
        stage('Demo') {
            steps {
                script {
                    mvn.fake()
                }
            }
        }
    }
}
2
xadocker
版权声明:本站原创文章,由 xadocker 2020-10-15发表,共计3511字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码
载入中...
0.319