我想创建一个变量名作为POD内部脚本,以分配kubectl输出,然后在运行kubectl port-forward POD时传递此变量。
但是我收到了下面的错误
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 151: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 151, column 80.
e-context ${KUBE_CLUSTER_STAGE}这是我的脚本。
environment {
POD = ''
}
steps {
script {
withCredentials([file(credentialsId: 'mbtkubeconfig', variable: 'config')]){
try {
// Expose PostreSQL
sh '''#!/bin/sh
chmod ug+w ${config}
export KUBECONFIG=\${config}
kubectl config use-context ${KUBE_CLUSTER_STAGE}
kubectl config set-context --current --namespace=database
POD = `$(kubectl get po -n database --selector='role==master' -o jsonpath="{.items[0].metadata.name}")`
kubectl port-forward pods/$POD 5432:64000 & echo \$! > filename.txt
'''当我在没有变量的情况下尝试时,没有任何error.Here是脚本运行时没有任何错误。
sh """#!/bin/sh
chmod ug+w ${config}
export KUBECONFIG=\${config}
kubectl config use-context ${KUBE_CLUSTER_STAGE}
kubectl config set-context --current --namespace=database
kubectl get pods -n database
kubectl port-forward pods/my-postgres-postgresql-helm-0 5432:64000 & echo \$! > filename.txt
"""发布于 2020-09-06 15:50:28
当您使用sh运行命令时,请确保使用的是",而不是'。只有在使用"${config}"时才会解析Groovy变量。
顺便说一句,尽管不需要解析变量,但使用env.标记变量被认为是最佳实践。例如,尝试使用${env.KUBE_CLUSTER_STAGE}标记集群阶段
https://stackoverflow.com/questions/63752223
复制相似问题