使用插件:File Parameter Plugin
之前寫過一篇關(guān)于Jenkins 用戶上傳文件到工作目錄的文章,那時候還需要使用sharedlibraries。現(xiàn)在使用這個插件可以非常簡單的上傳文件。話不多說,直接開始:文章來源:http://www.zghlxwxcb.cn/news/detail-727789.html
創(chuàng)建一個job,選擇This project is parameterized 如下截圖,然后選擇Base64 File Parameter,而不是File Parameter。
填Name,稍后會使用這個參數(shù)名獲取文件
然后就可以在執(zhí)行命令時使用如下方法將文件copy到當(dāng)前工作目錄或者任意目錄文章來源地址http://www.zghlxwxcb.cn/news/detail-727789.html
# 如果文件存在,則copy,不存在則跳過
withFileParameter(name:'THEFILE', allowNoFile: true) {
sh 'if [ -f "$THEFILE" ]; then cp $THEFILE ./$THEFILE_FILENAME; fi'
}
Pipeline
Linux agent
pipeline {
agent linux
parameters {
base64File 'THEFILE'
}
stages {
stage('Example') {
steps {
echo "test"
echo "$THEFILE"
echo "$env.WORKSPACE"
echo "$THEFILE_FILENAME"
sh 'ls -al'
withFileParameter(name:'THEFILE', allowNoFile: true) {
sh 'sleep 6; if [ -f "$THEFILE" ]; then cp $THEFILE ./$THEFILE_FILENAME; fi'
}
sh 'ls -al'
echo "test end"
}
}
}
}
Windows agent powershell
pipeline {
agent {
label 'windows'
}
parameters {
base64File 'THEFILE'
}
stages {
stage('Example') {
steps {
echo "$THEFILE_FILENAME"
withFileParameter(name:'THEFILE', allowNoFile: true) {
powershell '''
If ($env:LoginMacroFile_FILENAME) {
Copy-Item $env:THEFILE -destination $env:THEFILE_FILENAME
}
'''
}
}
}
}
}
到了這里,關(guān)于Jenkins 上傳文件到工作目錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!