stash:保存临时文件
将文件保存起来,以便同一次构建的其他步骤或阶段使用。 如果整个流水线在同一台机器上执行,那stash是多余的,一般用于跨Jenkins node使用。 stash步骤会将文件存储在tar文件中,对于大文件的stash操作会消耗Jenkins master的计算资源。
name
:字符串类型,保存文件的集合的唯一标识
allowEmpty
:布尔类型,允许stash内容为空
excludes
:字符串类型,排除文件,如果排除多个使用「逗号」分隔
includes
:字符串类型,stash文件,留空表示全部
useDefaultExcludes
:布尔类型,true:使用Ant风格路径默认排除文件 Ant风格 匹配任何单字符匹配项目根路径下所有在project路径下的.a文件最长匹配原则: /project/dir/file.jsp
,现在存在两个路径匹配模式 /**/*.jsp
和/project/dir/*.jsp
那么会根据模式/project/dir/*.jsp
来匹配
unstash:取出之前stash的文件
pipeline {
agent none
stages {
stage('stash') {
agent {label "master"}
steps {
writeFile file: "a.txt",text: "
stash(name: "abc", includes: "a.txt")
}
}
stage('unstash') {
agent {label "node2"}
steps {
script {
unstash("abc")
def content = readFile("a.txt")
echo "${content}"
}
}
}
}
}
实例
#!/usr/bin/env groovy
def registry = "registry.uzxxd.tech"
def project = "cxxu-shop"
def service_name="backend-api"
def image_pull_secret = "registry-pull-secret"
def harbor_auth = "52e40e53-6358-4a5e-bba1-04455e5963b6"
def git_auth = "2bda0281-9bdd-43ed-90e1-57d2c13b8778"
def k8s_auth = "264af109-9069-4969-abdd-b0f09577166c"
pipeline {
agent {
kubernetes {
...
}
}
parameters {
extendedChoice defaultValue: 'hd', description: '请选择任务类型', \
descriptionPropertyValue: '灰度发布,发布版本,回滚版本', multiSelectDelimiter: ',', name: 'task-type', \
quoteValue: false, saveJSONParameterToFile: false, type: 'PT_RADIO', value: 'hd,publish,re', \
visibleItemCount: 5
extendedChoice defaultValue: 'none', description: '权重灰度百分比', \
multiSelectDelimiter: ',', name: 'qz', type: 'PT_RADIO', \
value: '10,20,30,50,80,100'
extendedChoice defaultValue: 'none', description: '当前版本子集', \
multiSelectDelimiter: ',', name: 'zj', type: 'PT_RADIO', \
value: 'v1,v2'
extendedChoice defaultValue: 'v1:30,v2:70', description: '灰度状态', multiSelectDelimiter: ',', \
name: 'zt', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', \
value: 'v1:30,v2:70', visibleItemCount: 2
extendedChoice multiSelectDelimiter: ',', name: 'version1', description: '选择回滚的版本', \
propertyFile: '/var/jenkins_home/running-data/cxxu-shop/version', propertyKey: 'k1', \
quoteValue: false, saveJSONParameterToFile: false, type: 'PT_RADIO', visibleItemCount: 5
extendedChoice defaultValue: 'none', description: '选择发布的服务', \
multiSelectDelimiter: ',', name: 'Service', type: 'PT_CHECKBOX', \
value: 'buyer-api,seller-api,manager-api,consumer,base-api,sba-server'
}
stages {
stage('测试') {
steps {
writeFile file: "a.txt",text: "11111111111111111111111"
stash(name: "abc", includes: "a.txt")
sh """
echo "OK"
pwd && hostname && ls -lh
echo ${version1}
"""
}
}
stage('unstash') {
agent {label "master"}
steps {
script {
unstash("abc")
def content = readFile("a.txt")
echo "${content}"
}
sh """[ ! -d /var/jenkins_home/running-data/${project} ] && mkdir /var/jenkins_home/running-data/${project} -p
pwd && hostname && ls -lh && ls -lh /var/jenkins_home/
ls -lh /var/jenkins_home/running-data/${project}
cat /var/jenkins_home/running-data/cxxxu-shop/version
cat /var/jenkins_home/running-data/cxxxu-shop/version|awk -F"[,=]" '{print "k1="\$(NF-3)","\$(NF-2)","\$(NF-1)","\$NF",${BUILD_NUMBER}"}' >version
mv version /var/jenkins_home/running-data/cxxxu-shop/version
"""
}
}
}
}