在 Jenkins as a Job 中编写脚本时出错

Error in writing script in Jenkins as a Job

我正在 Jenkins 中为 运行 写一个脚本作为工作 - 它删除了 kubernetes pvc 的:

node('example') {
    docker.withRegistry('https://registry.hub.docker.com', 'dockerhub-creds') {
         docker.image('example').inside() {
            sh "kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Mounted By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | xargs -n2 bash -c 'kubectl -n  delete pvc [=10=]'"
         }
    }
}

现在,当我在 Jenkins 项目配置脚本区域中添加它时...它给我这个错误:

我认为最简单的方法是使用 '''(3 x 单引号)将命令括起来。
我创建了示例来说明它是如何工作的。

首先我创建了两个 PVCs (block-pvc,block-pvc2),它们应该被脚本删除。

# kubectl get pvc -A
NAMESPACE   NAME         STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
default     block-pvc    Pending                                                     9m45s
test        block-pvc2   Pending                                                     9m42s

然后我将命令添加到我的管道中:

sh '''
    ./kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Used By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | xargs -n2 bash -c './kubectl -n  delete pvc [=11=]'
'''

在这个构建的控制台输出中,我们可以看到它按预期工作:

persistentvolumeclaim "block-pvc" deleted
persistentvolumeclaim "block-pvc2" deleted