在/home/oracle/jenkins/workspace/test/
位置,我有多个目录。我想删除除test1
以外的所有目录,该目录是我使用终端-
rm -rf /home/oracle/jenkins/workspace/test/!("test1")
我想通过Jenkins管道实现,因此编写了方法-
def cleanWorkspaceDir() {
echo "Cleaning workspace"
sh '''rm -rf /home/oracle/jenkins/workspace/test/!("test1")
'''
}
但是它给出了错误-/home/oracle/jenkins/workspace/RedmineAndReviewboardProject/SVNCheckout@tmp/durable-810bac2b/script.sh: line 1: syntax error near unexpected token
('`
能帮我解决这个问题吗?
答案 0 :(得分:0)
您可以尝试以下操作:
def cleanWorkspaceDir() {
echo "Cleaning workspace"
sh '''find test/ -mindepth 1 '!' -name test1 -type d -exec rm -rf '{}' +
'''
}
pipeline {
agent { label 'slave' }
stages {
stage('Hello') {
steps {
sh 'mkdir -p test/{a/{p,q},b,c/{r,s},test1,test2}'
sh 'ls -lR'
cleanWorkspaceDir()
}
}
}
}
用以下任何一种替换上面的find
命令:
find /home/oracle/jenkins/workspace/test/ -mindepth 1 -type d -not -name test1 -delete
find /home/oracle/jenkins/workspace/test/ -mindepth 1 ! -name 'test1' -type d -exec rm -rf {} +