Jenkins Pipeline使用File parameter
Jenkins Pipeline使用File parameter,将文件通过publish over cifs上传到Windows Share.
Jenkins Pipeline直接使用File parameter,无法将文件上传到当前job的workspace下面,需要通过调用第三方jenkinsci-unstashParam-library库实现,方法如下:
1.访问 https://github.com/janvrany/jenkinsci-unstashParam-library ,下载zip压缩包,然后将其拷贝到Jenkins本机目录 /datafile/Jenkins2Library/jenkinsci-unstashParam-library,如下图:
2.Jenkins - 系统配置 - Global Pipeline Libraries,新增Library,但由于Jenkins设计,需要从远程git或scm拉去库,所以需要初始化本地 git 存储库并将其用作SCM来实现需求,而不需要任何远程主机git remote service,
如下图所示
参考:https://www.jianshu.com/p/e1630651c95c
3.cd /datafile/Jenkins2Library/jenkinsci-unstashParam-library,然后 init a git repo 初始化git
git init git add . git commit -m "init"
4.检查git init 是否生效:
git status
/usr/bin/git ls-remote -h /datafile/Jenkins2Library/jenkinsci-unstashParam-library #在远程存储库中列出引用
以上完成后,在PipeLine中引用jenkinsci-unstashParam-library库即可,如下:
//引用jenkinsci-unstashParam-library library "jenkinsci-unstashParam-library" def file_upload pipeline{ agent any stages{ stage("cifspub"){ steps { script{ file_upload = unstashParam "myFileParam" sh "pwd ${file_upload}" println "curr file is: ${file_upload}" } //上传文件到Windows cifs share cifsPublisher(publishers: [[ configName: 'CifsShare_10.10.3.21', //cifs share name transfers: [[cleanRemote: false, excludes: '', flatten: false, makeEmptyDirs: true, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "$JOB_NAME/$BUILD_ID", //remote directory remoteDirectorySDF: false, removePrefix: '', sourceFiles: "${file_upload}" ]], //file parameter usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true]] ) } } } }
########################################################################################################
文件上传成功后,移动到
#!groovy def call(fileparam_name,fileparam,filetype,cifsname){ //调用jenkinsci-unstashParam-library库上传文件 library "jenkinsci-unstashParam-library" if(fileparam != ''){ uploadfilesplit = fileparam.split('\\.') as List }else{ println "\033[31m 还未上传文件,任务已终止。 \033[0m" return } if(uploadfilesplit[1] == filetype){ file_upload = unstashParam fileparam_name //file_upload = unstashParam "上传文件" //sh "pwd ${file_upload}" fileupload_remotepath = "$JOB_NAME/$BUILD_ID/${file_upload}" file_content=new File("$WORKSPACE/${file_upload}").readLines() file_size = file_content.size()-1 //拷贝文件到Windows cifs share cifsPublisher(publishers: [[ configName: cifsname, transfers: [[cleanRemote: false, excludes: '', flatten: false, makeEmptyDirs: true, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "$JOB_NAME/$BUILD_ID", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "${file_upload}" ]], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true]] ) //将上传的文件移动到build_id目录下 sh """ set +x mkdir $WORKSPACE/$BUILD_ID mv ${file_upload} $WORKSPACE/$BUILD_ID/ find $WORKSPACE/ -type f -mtime +15 -exec rm -f {} \\; set -x """ }else{ println "\033[31m 上传文件不是 $filetype 格式,任务已终止。 \033[0m" //skipRemainingStages = true //currentBuild.result = 'ABORTED' //return } }
将如下文件保存到/data/Jenkins2Library/SharedLibrary下,命名为UploadFile.groovy
#!groovy def call(fileparam_name,fileparam,filetype,cifsname){ //调用jenkinsci-unstashParam-library库上传文件 library "jenkinsci-unstashParam-library" if(fileparam != ''){ uploadfilesplit = fileparam.split('\\.') as List }else{ println "\033[31m 还未上传文件,任务已终止。 \033[0m" return } if(uploadfilesplit[1] == filetype){ file_upload = unstashParam fileparam_name //file_upload = unstashParam "上传文件" //sh "pwd ${file_upload}" fileupload_remotepath = "$JOB_NAME/$BUILD_ID/${file_upload}" file_content=new File("$WORKSPACE/${file_upload}").readLines() file_size = file_content.size()-1 //拷贝文件到Windows cifs share cifsPublisher(publishers: [[ configName: cifsname, transfers: [[cleanRemote: false, excludes: '', flatten: false, makeEmptyDirs: true, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "$JOB_NAME/$BUILD_ID", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "${file_upload}" ]], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true]] ) //将上传的文件移动到build_id目录下 sh """ set +x mkdir $WORKSPACE/$BUILD_ID mv ${file_upload} $WORKSPACE/$BUILD_ID/ find $WORKSPACE/ -type f -mtime +15 -exec rm -f {} \\; set -x """ }else{ println "\033[31m 上传文件不是 $filetype 格式,任务已终止。 \033[0m" //skipRemainingStages = true //currentBuild.result = 'ABORTED' //return } }
在pipeline中通过ShareLibrary调用:
library "SharedLibrary"
UploadFile("上传文件",上传文件,"csv","CifsShare_10.10.4.25")