I have a rather complex command that needs to be run in an "sh" step:
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '3548afa5-6360-428f-8ae2-xxxxxxxxxx', passwordVariable: 'dbPassword', usernameVariable: 'dbUser']]) { def cmd = """kubectl run ${serviceName} --image=${dockerRegistry}/${serviceName}:${env.BUILD_NUMBER} --replicas=2 --port=${externalPort} --env=\'MONGODB_URL=mongodb://${env.dbUser}:${env.dbPassword}@${dbCluster}/${serviceName}-uat?${dbOptions}\' --expose --service-overrides='{ "spec": { "type": "LoadBalancer" }}' || true""" // execute shell for the command above sh cmd }
Now, if I print this command, it renders as expected.
kubectl run my-service --image=XXXXXXXX.eu-west-1.amazonaws.com/my-service:124 --replicas=2 --port=8083 --env='MONGODB_URL=mongodb://****:****@****-shard-00-00-XXXX.mongodb.net:27017,****-shard-00-01-XXXX.mongodb.net:27017,****-shard-00-02-XXXX.mongodb.net:27017/my-service-uat?ssl=true&replicaSet=****-shard-0&authSource=admin' --expose --service-overrides='{ "spec": { "type": "LoadBalancer" }}'
However if I look at the logs at what the pipeline actually run in sh, they are not the same:
[Pipeline] sh
[my-service] Running shell script
+ kubectl run my-service --image=XXXXXXXX.eu-west-1.amazonaws.com/my-service:124 --replicas=2 --port=8083 '--env=MONGODB_URL=mongodb://****:****@****-shard-00-00-XXXX.mongodb.net:27017,****-shard-00-01-XXXX.mongodb.net:27017,****-shard-00-02-XXXX.mongodb.net:27017/my-service-uat?ssl=true&replicaSet=****-shard-0&authSource=admin' --expose '--service-overrides={ "spec": { "type": "LoadBalancer" }}'
error: invalid env: ****-shard-00-01-XXXX.mongodb.net:27017
Note it hasn't honoured the quotes in the --env command and it adds its own '.