我想将一些文件压缩为jenkins管道作业的输出。问题是,几乎所有文件夹都被没有内容的foldername @ tmp文件夹复制。我通过从zipfile中排除所有这些文件夹找到了解决方案。
但通过这样做,我发现管道表达式dir{}
内部和“sh”内部cd
之后的行为不同于之前或之外。看我的测试脚本:
node() {
stage('TEST') {
sh """
rm -r *
mkdir test
mkdir test/test2
mkdir test/test2/test3
mkdir test/test2@tmp
mkdir test/test2/test3@tmp
echo test > test/test.txt
echo test > test/test2/test.txt
echo test > test/test2@tmp/test.txt
// leads to expected behavior
zip testzip.zip -r test/test2/ -x *@tmp*
// leads to unexpected behavior
cd test
pwd
zip testzip.zip -r test2/ -x *@tmp*
"""
// leads to unexpected behavior
dir("test") {
sh "zip testzip.zip -r test2/ -x *@tmp*"
}
}
}
并输出:
[Pipeline] node
Running on master in /var/jenkins_home/jobs/TEST/workspace
[Pipeline] {
[Pipeline] stage
[Pipeline] { (TEST)
[Pipeline] sh
[workspace] Running shell script
+ rm -r testzip.zip test test@tmp
+ mkdir test
+ mkdir test/test2
+ mkdir test/test2/test3
+ mkdir test/test2@tmp
+ mkdir test/test2/test3@tmp
+ echo test
+ echo test
+ echo test
+ zip testzip.zip -r test/test2/ -x '*@tmp*'
adding: test/test2/ (stored 0%)
adding: test/test2/test.txt (stored 0%)
adding: test/test2/test3/ (stored 0%)
+ cd test
+ pwd
/var/jenkins_home/jobs/TEST/workspace/test
+ zip testzip.zip -r test2/ -x test2@tmp
adding: test2/ (stored 0%)
adding: test2/test.txt (stored 0%)
adding: test2/test3/ (stored 0%)
adding: test2/test3@tmp/ (stored 0%)
[Pipeline] dir
Running in /var/jenkins_home/jobs/TEST/workspace/test
[Pipeline] {
[Pipeline] sh
[test] Running shell script
+ zip testzip.zip -r test2/ -x test2@tmp
updating: test2/ (stored 0%)
updating: test2/test.txt (stored 0%)
updating: test2/test3/ (stored 0%)
updating: test2/test3@tmp/ (stored 0%)
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
你可以看到,第一次调用zip时,*@tmp*
没有被替换,而在第二次和第三次调用中,它被当前目录中的所有可能性替换。
我发现使用'*@tmp*'
解决了这个问题,但我想知道为什么行为不同! (因为在命令行上执行第二个选项将导致预期结果...)