当前位置:网站首页>35-Jenkins-Shared library application
35-Jenkins-Shared library application
2022-07-31 08:29:00 【Love learning de test Xiaobai】
前言
- 本篇来学习Jenkins中使用共享库
Reason for using shared library
- As the pipeline is adopted by more and more projects in the organization,Common patterns are likely to emerge. Sharing pipelines across multiple projects helps reduce redundancy and preserve code
- 共享库目录结构
(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
- srcDirectories are similar to standardsJava源目录结构.执行流水线时,This directory will be added to the classpath.
- vars目录托管脚本文件,These script files are in“管道”is exposed as a variable.
- resources目录允许1ibraryResourceUse a step from an external library to load the associated nonGroovy文件.
定义共享库
- 目录: src/org/devops/tools.groovy
pacakge org.devops // 不可省略
//打印信息
def PrintMsg(msg){
println(msg)
}
//Print information with color 需安装ansiColor插件
def Post(value,color){
colors = ['red' : "\033[40;31m >>>>>>>>>>>${value}<<<<<<<<<<< \033[0m",
'blue' : "\033[47;34m ${value} \033[0m",
'green' : "\033[40;32m >>>>>>>>>>>${value}<<<<<<<<<<< \033[0m" ]
ansiColor('xterm') {
println(colors[color])
}
}
- 目录:src/org/devops/GlobalVars.groovy
package org.devops //This specifies the directory location where the file is located
class GlobalVars {
//The name should be the same as the file name
static String name = "大海"
static int age = 28
static String city = "北京"
}
- 目录:vars/sayHello.groovy
def call(String name) {
echo "Hello, ${name}!"
}
- 目录:vars/log.groovy
def info(message) {
echo "INFO: ${message}"
}
def warning(message) {
echo "WARNING: ${message}"
}
使用共享库
1. 配置共享库
- Manage Jenkins --> Configure System -->-Global Pipeline Libraries
- Name :共享库的唯一标识,在Jenkinsfile中会使用到..
- Default version :默认版本.可以是分支名、tag标签等.
- Load implicitly:隐式加载.如果勾选此项,将自动加载全局共享库,在Jenkinsfile中不需要显式引用,就可以直接使用.
- Allow default version to be overridden :如果勾选此项,则表示允许“Default version”被Jenk-insfile中的配置覆盖.
- [email protected] changes in job recent changes:如果勾选此项,那么共享库的最后变更信息会跟项目的变更信息一起被打印在构建日志中.
- Retrieval method:获取共享库代码的方法.我们选择LegacySCM”选项,进而选择使用GitRepository configuration shared library
2. Pipeline中使用
@Library('[email protected]') _ // _ 不可省略
import org.devops.GlobalVars
def mytools = new org.devops.tools()
pipeline {
agent any
stages {
stage("hello"){
steps{
script{
mytools.Post("This is ShareLib",'green')
println "${GlobalVars.name}"
println "${GlobalVars.age}"
println "${GlobalVars.city}"
}
}
}
stage("world"){
steps{
script{
mytools.PrintMsg("This is ShareLib")
sayHello('大海')
log.info 'Starting'
log.warning 'Nothing to do!'
}
}
}
}
}
- @Library(‘[email protected]’)_
- <version>可以是:
- 分支,如@Library ( ‘[email protected]’).
- tag标签,如@Library ( ‘[email protected]’ )
- git commit id,如@Library ( ‘[email protected]’)
- 因为Jenkins支持同时添加多个共享库,所以@Library注解还允许我们同时引入多个共享库,如:@Library ( [‘global-shared-library’ , '[email protected]]).
边栏推荐
- SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
- [PSQL] SQL Basic Course Reading Notes (Chapter1-4)
- 手把手教你开发微信小程序自定义底部导航栏
- Collation and sharing of related classic papers and datasets in the field of deep learning communication
- 安装部署KubeSphere管理kubernetes
- ZCMU--1862: zbj的狼人杀
- 【C#】说说 C# 9 新特性的实际运用
- Regarding "computing power", this article is worth reading
- 如何使用mysql binlog 恢复数据
- 日志导致线程Block的这些坑,你不得不防
猜你喜欢
随机推荐
Chapter 9 Exceptions try...except...else...finally
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
SSM整合案例分析(详解)
SSM框架讲解(史上最详细的文章)
一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
linux redis6.2.6配置文件
MySQL 5.7升级到8.0详细过程
蚂蚁核心科技产品亮相数字中国建设峰会 持续助力企业数字化转型
Linux redis6.2.6 configuration file
CNN--各层的介绍
【云原生与5G】微服务加持5G核心网
Environment_Variable_and_SetUID
二维坐标工具API
Ceph单节点部署
shell/bash脚本命令教程
MySQL安装常见报错处理大全
进程调度的基本过程
机器学习---线性回归、Logistic回归问题相关笔记及实现
48页智慧城市规划蓝图 解决方案
Unreal基础概念