jenkins 之ShareLibrary 介绍
src 目录类似标准Java 目录结构,执行流水线时,将此目录添加到类路径中
vars 目录托管脚本文件,这些脚本在文件管道中作为变量公开
resources 目录允许library从外部库中使用步骤来加载相关联的非Groovy文件

示例https://github.com/ruidongchenxi/ShareLibrary-jenkins.git
groovy 按装
[root@jenkins ~]# wget https://groovy.jfrog.io/artifactory/dist-release-local/groovy-zips/apache-groovy-sdk-4.0.28.zip mv groovy-4.0.28 /usr/local/groovy [root@jenkins ~]# vim /etc/profile.d/groovy.sh export GROOVY_HOME=/usr/local/groovy #export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$GROOVY_HOME/bin:$PATH
groovy 数据类型
groovy 数据类型string
常用方法: contains()是否包含特定内容返回true 或fasle
size() leng() 字符串数量大小长度
toString()转换string类型
indexOf()元素索引
字符串表示:单引号、双引号、三引号
区别
groovy:000> 'hello'
===> hello
groovy:000> name = "zhangsan"
===> zhangsan
groovy:000> "my name is ${name}"
===> my name is zhangsan
groovy:000> 'my name is ${name}'
===> my name is ${name}
常用方法: contains()是否包含特定内容返回true 或fasle
groovy:000> "chenxi".contains("en")
===> true
groovy:000> "chenxi".contains("yu")
===> false
size() length() 字符串数量大小长度
groovy:000> "chenxi".size()
===> 6
groovy:000> "chenxi".size()
===> 6
toString()转换string类型
indexOf()元素索引
endsWith()是否以指定字符串尾部
groovy:000> "chenxi".endsWith("yu")
===> false
groovy:000> "chenxi".endsWith("xi")
===> true
minus() plus() 去掉、增加字符串
reverse() 反向排序
substring(1,2) 字符串的指定索引开始的子字符串
toUpperCase() toLowerCase() 字符串大小转换
groovy:000> "chenxi".toUpperCase()
===> CHENXI
groovy:000> "CHENXI".toLowerCase()
===> chenxi
split() 字符串分割 默认空格分割 返回列表
groovy:000> "h,w,e,".split(',')
===> [h, w, e]
groovy:000> h1 = "h,w,e,"
===> h,w,e,
groovy:000> for(i in h1){
groovy:001> println(i)
groovy:002> }
h
,
w
,
e
,
===> null
+ 加 -减
groovy:000> "chenxi".size()
===> 6
数据类型list
常用方法:
+ - += -= 元素增加减少
groovy:000> [1,2,3,4]+5
===> [1, 2, 3, 4, 5]
<< 添加元素
groovy:000> [1,2,3,4]<<9
===> [1, 2, 3, 4, 9]
isEmpty() 判断是否为空
interface([2,3]) disjoint([1]) 集合交集、判断是否有交集
flatten() 合并嵌套的列表
unique()去重
groovy:000> [2,3,4,5,5,6].unique()
===> [2, 3, 4, 5, 6]
reverse() sort() 反转升序
count() 元素个数
join()将元素按照参数连接
groovy:000> [2,3,4,5,5,6].join("-")
===> 2-3-4-5-5-6
sum() min() max() 求和 最小值 最大值
contain()包含特定元素
remove(2) removeAll()
each{}
groovy:000> [2,3,4,5,5,6].each{
groovy:001> println it}
2
3
4
5
5
6
===> [2, 3, 4, 5, 5, 6]
map
表示:[:]
常用方法:
size() map大小
['key'].key get() 获取value
isEmpty() 是否为空
containkey() 是否包含key
containValue() 是否包含指定的value
keySet() 生成key 列表
each{} 遍历map
remove('a') 删除元素(k-v)
def 函数
语法
def PrintMes(value){
println(value)
return value
}
PrintMes("value")
[root@jenkins ~]# groovy tset.groovy
cx
[root@jenkins ~]# cat tset.groovy
def PrintMas(){
println("cx")
}
PrintMas()
[root@jenkins ~]# cat tset.groovy
def PrintMas(){
println("cx")
}
PrintMas()
[root@jenkins ~]# vim tset.groovy
[root@jenkins ~]# vim tset1.groovy
[root@jenkins ~]# cat tset1.groovy
def PrintMes(value){
println(value)
return value
}
PrintMes("value")
[root@jenkins ~]# vim tset3.groovy
[root@jenkins ~]# groovy tset3.groovy
value
value
[root@jenkins ~]# cat tset3.groovy
def PrintMes(value){
println(value)
return value
}
response = PrintMes("value")
PrintMes(response)
示例
// 1.指定node/和工作目录
// 2.指定运行选项(可以省略)
@Library("jenkins@master") //共享库
String workspace = "/opt/jenkins/workspace"
pipeline{
agent{node{ label "masetr" //指定运行节点的标签或者名称
customWorkspace "${workspace}"//指定工作目录
}
}
options{
timestamps() //日志时间
skipDefaultCheckout()//删除隐士checkout scm 语句
disableConcurrentBuilds()//禁止并行
timeout(time:1,unit:'HOURS')//流水线超时时间设置1小时
}
// 3.指定stages阶段
// 解释:在这里添加三个阶段
// GetCode
// Build
// CodeScan
stages{
stage("GetCode"){
steps{ //步骤
timeout(time:5,unit:"MINUTES"){ //步骤时间
script{//填写运行的代码
println('获取代码')
}
}
}
}
//打包
stage("Build"){
steps{
timeout(time:20,unit:"MINUTES"){
script{//填写运行的代码{
println('应用打包')
}
}
}
}
//代码扫秒
stage("CodeScan"){
steps{
timeout(time:30,unit:"MINUTES"){
println('代码扫描')
}
}
}
}
// 构建后操作
// 解释
// always{}:总是执行的脚步片段
// success{}: 成功后执行
// failure{}:失败后执行
// aborted{}: 取消后执行
// currendBuild 是一个全局变量
// description: 构建描述
post{
always{
script{
println('always')
}
}
success{
script{
currentBuild.description+="\n 构建成功"
}
}
failure{
script{
currentBuild.description+="\n 构建失败"
}
}
aborted{
script{
currentBuild.description+="\n 取消构建"
}
}
}
}
Pipeline 介绍
1.1 agent (代理)
agent 指定流水钱的执行节点
参数
any 在任何可用节点上执行Pipeline
none 没有指定agent的时候默认
label 在指定标签上运行Pipeline
node 允许额外的选项
下面两种一样的
agent {node {label 'labelname'}}
agent {label 'labelname'}
1.2 post
定义一个或多个steps,这些阶段根据流水钱或阶段的完成情况而运行(取决于流水钱中post部分位置)。post支持以下post-condition块中其中之一:always,changed,failure,success,unstable,aborted。这些条件块允许在post部分的步骤的执行取决于流水钱阶段完成状态
always无论流水线或者阶段完成状态
changed只有当流水线或者阶段完成状态与之前不同时
failure只有当流水线或者阶段状态为failure运行
success只有当流水线或者阶段状态为success运行
unstable 只有当流水线或者阶段状态为unstable运行,例如测试失败
aborted 只有当流水线或者阶段状态为aborted运行。手动取消
1.3 stages(阶段)
包含一系列一个或多个stage指令,建议stages至少包含一个stage指令用于交付过程的每个离散部分,比如测试、构建、部署
pipeline{
agent any
stages{
stage('Example'){//阶段
steps{ //步骤
echo "Hello World"
}
}
}
}
1.4 steps(步骤)
steps 是每个中要执行的每个步骤
1.5 environment
environment 指令指定一个键值对序列,该序列将被定义为所有步骤的环境变量,或者特定于阶段的步骤,取决于environment指定在流水线内的位置
该指令支持一个特殊方法credentials(),该方法可用于jenkins 环境中通过标识符访问预定义的凭证。对于类型为"Secret Text"的凭证。credentials()将确保指定的环境变量包含秘密文本内容,对于类型为"SStandard username and password" 的凭证,指定的环境变量指定为username:password,并且两个额外的环境变量将自动定义:分别为MYVARNAME_USE和MYVARNAME_PSW
pipeline{
agent any
environment{
CC = 'clang'
}
stages{
stage('Example'){
environment{
AN_ACCESS_KEY =credentials('my-prefined-secret-text')
}
stage{
sh 'printenv'
}
}
}
}
1.5.2 options
options 指令允许从流水线内部配置特定流水项选项。流水线提供了许多这样的选项,比如buildDiscarder.但也可以由插件提供,比如timestamps
buildDiscarder:为最近的流水线运行的特定数量保存组件和控制台输出
disableConcurrentBuilds:不允许同时执行流水线,可被用来防止同时访问共享资源
overrideIndexTriggers:允许覆盖分支索引触发器默认处理
skipDefaultCheckout:在agen指令中。跳过从源代码控制检出代码默认情况;该指令会跳过从源代码控制系统检出代码的操作agent
skipStagesAfterUnstable:一旦构建状态变得UNSTABLE(不稳定)。跳过该阶段
checkoutToSubdirectory:在工作空间子目录中自动地执行源代码检出操作;在工作区的子目录中执行自动源代码控制检出
timeout:设置流水线运行的超时时间。在此之后,jenkins将中止流水线
retry:在失败时,重新尝试整个流水线的指定次数
timestamps: 预测所有流水线生成控制台输出,与该流水线出发的时间一致
1.5.3 参数
为流水线运行时设置项目相关的参数
string 字符串类型的参数,例如
parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') }
booleanParam 布尔值
parameters { booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: '') }
文件类型参数
parameters { text(name: 'DEPLOY_TEXT', defaultValue: 'One\nTwo\nThree\n', description: '') }
1.5.4触发器
cron计划任务定期执行触发器
triggers { cron('H */4 * * 1-5') }
pollSCM 与cron定义类型,但是由jenkins 定期检测源码变化
triggers { pollSCM('H */4 * * 1-5') }
upstream 接受逗号分隔的工作字符串和阈值。当字符串中任何作业以最小阈值结束时,流水线被重新触发
triggers { upstream(upstreamProjects: 'job1,job2', threshold: hudson.model.Result.SUCCESS) }
1.5.5 tools
获取自动安装或手动放置工具的环境变量,支持maven/jdk/gradle.工具的名称必须在系统设置->全局工具配置定义
pipeline {
agent any
tools {
maven 'apache-maven-3.0.1'
}
stages {
stage('Example') {
steps {
sh 'mvn --version'
}
}
}
}
1.5.6 input
input 用户执行阶段的时候,由人工确认是否继续
message: 呈现给用户提示信息
id: 可选默认为stage(名称)
ok: 默认表单提交的ok 文本
submitter: 可选。以逗号分隔的用户列表或允许提交的外部组名,默认允许任何用户
submitterParameter: 环境变量的可选名称,如果存在,用submitter名称设置
parameters: 提示提交者提供的一个可选参数列表
pipeline {
agent any
stages {
stage('Example') {
input {
message "Should we continue?"
ok "Yes, we should."
submitter "alice,bob"//谁可以执行
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
}
steps {
echo "Hello, ${PERSON}, nice to meet you."
}
}
}
}
1.5.7 when
when 指令允许流水线根据给定的条件决定是否应该执行阶段。when指令包含多个条件,所有的子条件必须返回True,阶段才能执行,这与子条件在allOf条件下嵌套的情况相同
内置条件
branch:当正在构建的分支与模式给定的分支匹配时,执行这个阶段,适用多分支流水线
when { branch 'master' }
environment: 当指定环境变量是给定的值时,执行这个步骤
when { environment name: 'DEPLOY_TO', value: 'production' }
expression:当指定Groovy表达式为true时执行这个阶段
when { expression { return params.DEBUG_BUILD } }.
not:当嵌套条件错误时,执行这个阶段
when { not { branch 'master' } }
allOf:当所有嵌套条件执行结果为真是,执行这个阶段,必须包含一个条件
when { allOf { branch 'master'; environment name: 'DEPLOY_TO', value: 'production' } }
anyOf:当至少有一嵌套条件为真时,执行这个阶段,必须包含至少一个条件
when { anyOf { branch 'master'; branch 'staging' } }
1.5.7 parallel 并行
声明式流水线的阶段可以在他们内部声明多个嵌套阶段,他们将是并行执行。注意一个阶段必须只有一个steps或者parallel的阶段,嵌套阶段本身不能包含进一步parallel阶段。但其他的阶段行为与任何其他stage parallel的阶段不能包含agent 或 tools 阶段,因为他们没有steps
另外,通过failFast true 到包含parallel 的 stage中,其中一个进程失败时,你可以强制所有的parallel阶段被终止
pipeline {
agent any
stages {
stage('Non-Parallel Stage') {
steps {
echo 'This stage will be executed first.'
}
}
stage('Parallel Stage') {
when {
branch 'master'
}
failFast true
parallel {
stage('Branch A') {
agent {
label "for-branch-a"
}
steps {
echo "On Branch A"
}
}
stage('Branch B') {
agent {
label "for-branch-b"
}
steps {
echo "On Branch B"
}
}
stage('Branch C') {
agent {
label "for-branch-c"
}
stages {
stage('Nested 1') {
steps {
echo "In stage Nested 1 within Branch C"
}
}
stage('Nested 2') {
steps {
echo "In stage Nested 2 within Branch C"
}
}
}
}
}
}
}
}
1.6 step
161 script
此script步骤会将Scripted Pipeline 块在声明流水线中执行,对于大多数用例来说,应该声明流水线中脚本步骤不是必要得,但它可以提供一个逃生出口。非平凡的模块和或复杂的script块应该被移到共享库
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
script {
def browsers = ['chrome', 'firefox']
for (int i = 0; i < browsers.size(); ++i) {
echo "Testing the ${browsers[i]} browser"
}
}
}
}
}
}
ShareLibrary 概述
src 目录类似标准Java 目录结构,执行流水线时,将此目录添加到类路径中 vars 目录托管脚本文件,这些脚本在文件管道中作为变量公开 resources 目录允许library从外部库中使用步骤来加载相关联的非Groovy文件
完整示例
@Library('jenkinslib')-
def tools = new org.devops.tools()
String workspace = "/opt/jenkins/workspace"
pipeline{
agent{node{ label "masetr" //指定运行节点的标签或者名称
customWorkspace "${workspace}"//指定工作目录
}
}
parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') }
options{
timestamps() //日志时间
skipDefaultCheckout()//删除隐士checkout scm 语句
disableConcurrentBuilds()//禁止并行
timeout(time:1,unit:'HOURS')//流水线超时时间设置1小时
}
stages{
stage("GetCode"){
when { environment name: 'test', value: 'abcd' }
steps{ //步骤
timeout(time:5,unit:"MINUTES"){ //步骤时间
script{//填写运行的代码
println('获取代码')
println("${test}")
input id: 'Test', message: '是否继续', ok: '是,继续吧!', parameters: [choice(choices: ['a', 'b'], name: 'test')], submitter: 'admin,'
}
}
}
}
stage("1"){
failFast true
parallel {
stage("Build"){
steps{
timeout(time:20,unit:"MINUTES"){
script{
println('应用打包')
def mvnHome = tool "m2"
println(mvnHome)
sh "${mvnHome}/bin/mvn --version"
}
}
}
}
//代码扫秒
stage("CodeScan"){
steps{
timeout(time:30,unit:"MINUTES"){
println('代码扫描')
tools.PrintMes("this is my lib!")
}
}
}
}
post{
always{
script{
println('always')
}
}
success{
script{
currentBuild.description+="\n 构建成功"
}
}
failure{
script{
currentBuild.description+="\n 构建失败"
}
}
aborted{
script{
currentBuild.description+="\n 取消构建"
}
}
}
}
使用docker 按照gitlab
# step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils # Step 2: 添加软件源信息 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # Step 3: 安装Docker sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Step 4: 开启Docker服务 sudo service docker start # 注意: # 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。 # vim /etc/yum.repos.d/docker-ce.repo # 将[docker-ce-test]下方的enabled=0修改为enabled=1 # # 安装指定版本的Docker-CE: # Step 1: 查找Docker-CE的版本: # yum list docker-ce.x86_64 --showduplicates | sort -r # Loading mirror speeds from cached hostfile # Loaded plugins: branch, fastestmirror, langpacks # docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable # docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable # docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable # Available Packages # Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos) # sudo yum -y install docker-ce-[VERSION] mkdir -p /srv/gitlab/config mkdir -p /srv/gitlab/logs mkdir -p /srv/gitlab/data docker run -d \ --hostname gitlab.local \ -p 80:80 \ -p 443:443 \ -p 2222:22 \ --name gitlab \ --restart always \ -v /srv/gitlab/config:/etc/gitlab \ -v /srv/gitlab/logs:/var/log/gitlab \ -v /srv/gitlab/data:/var/opt/gitlab \ gitlab/gitlab-ce:latest centos 8 初始化yum rm -f /etc/yum.repos.d/CentOS-* curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo yum clean all && yum makecache yum install -y yum-utils yum -y install wget
草都可以从石头缝隙中长出来更可况你呢

浙公网安备 33010602011771号