Jenkins Sharable library

本文介绍了如何在Jenkins中配置和使用共享库,包括创建两个git仓库,一个存放pipeline,另一个存放代码库。详细展示了devops_pipeline和devops仓库的目录结构,以及`gitCheckout.groovy`、`build.groovy`和`test.groovy`的Groovy脚本内容。接着讲解了Jenkins全局管道库的配置步骤,并提供了新建流水线item的测试配置。通过这些步骤,实现了Jenkins共享库与脚本的联动执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 准备工作

1.1 需要两个仓库

一个仓库放pipeline,另一个仓库放代码库

git仓库描述
devops_pipelineSharable library pipeline
devops代码库

devops_pipeline 目录结构:

$ ls -l
total 5
-rw-r--r-- 1 rsq rsq  21 Oct 24 13:48 README.md
-rwxr-xr-x 1 rsq rsq 595 Oct 24 14:20 test.groovy*
drwxr-xr-x 1 rsq rsq   0 Oct 24 13:56 vars/
$ ls -l vars
total 2
-rwxr-xr-x 1 rsq rsq 178 Oct 24 14:17 build.groovy*
-rwxr-xr-x 1 rsq rsq 819 Oct 24 14:07 gitCheckout.groovy*

devops目录结构:

$ ls -l
total 2
-rw-r--r-- 1 rsq rsq Oct 24 13:41 README.md
-rw-r--r-- 1 rsq rsq 29 Oct 24 13:52 build.sh
$ cat build.sh
echo "This is a test scripts"

1.2 Jenkins 配置Share Library

Manager Jnekins --> Configure System --> Global Pipeline Libraries
在这里插入图片描述

1.3 相关groovy

gitCheckout.groovy

#!/usr/bin/env groovy
def call(String gitRepo, String gitBranch, String gitCredential) {
    checkout(
        [   
            $class: 'GitSCM', 
            branches: [[name: gitBranch]], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [
                [$class: 'CheckoutOption', timeout: 60],
                [$class: 'GitLFSPull'],
                [$class: 'CloneOption', noTags: false, reference: '', shallow: false, timeout: 60],
                [$class: 'SubmoduleOption', timeout: 60, disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', trackingSubmodules: false]
            ], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: gitCredential, 
            url: gitRepo]]
        ]
    )
}

build.groovy

#!/usr/bin/env groovy
def call() {
    dir("${WORKSPACE}") {
        sh '''
            echo "This is a test scripts."
            echo "${WROKSPACE}"
        '''
    }
}

test.groovy 主要实现跟share library联动
test.groovy

#!/usr/bin/env groovy
// share library entrypoint.
@Library("devops") _
node("devops_linux") {
    // define const
    def gitRepo = "${env.gitRepo}"
    def gitBranch = "${env.gitBranch}"
    def gitCredential = "${env.gitCredential}"
    // custom workspace
    env.WORKSPACE = "/home/devops/test"

    stage("Clean Env") {
        sh '''
            echo "---------------------"
            echo ${WORKSPACE}
        '''
    }

    stage("Git Clone") {
        gitCheckout(gitRepo, gitBranch, gitCredential)
    }

    stage("Build") {
        build()
    }
}

2 Jenkins 新建流水线 item 测试

在这里插入图片描述

Configure item,pipeline SCM选择devops_pipeline这个仓库,Script Path选择 test.groovy
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RSQ博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值