0

I have created a Jenkins pipeline shared library to retrieve a pod IP connecting to a kubernetes cluster which fails throwing an error below. Any pointers for fixing this would be great:

JSL:

docker.withRegistry('https://' + dockerRegistry, dockerCredentialsId) {
    docker.image(kubectlImage).inside("""--entrypoint=''"""){
      sh """
      #!/bin/sh
      set +x
      kubectl get pods -n ${namespace}  -o json > $WORKSPACE/pods.json
      podIP=$(jq -r '.items[] | select(.metadata.generateName | test(\"${appName}\")).status.podIP' $WORKSPACE/pods.json}
      echo "Pod IP:$podIP"
      """
    }
  }

Error during the pipeline execution -

70: illegal string body character after dollar sign;
16:18:23     solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 70, column 14.
16:18:23           podIP=$(jq -r '.items[] | select(.metadata.generateName | test(\"${appName}\")).status.podIP' $WORKSPACE/pods.json)
16:18:23                  ^
16:18:23  
16:18:23  1 error
16:18:23  
16:18:23    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
16:18:23    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
16:18:23    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
16:18:23    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
Avi
  • 1,453
  • 4
  • 18
  • 43
  • Does this answer your question? [Unable to run shell script inside Jenkins pipeline](https://stackoverflow.com/questions/50350713/unable-to-run-shell-script-inside-jenkins-pipeline) – Chin Huang Dec 09 '21 at 16:57

1 Answers1

0

The """ syntax is for multiline-interpolated Groovy strings. This is not what you want in either of the usage above. We can use a single line literal string ' and a multiline literal string instead:

docker.withRegistry('https://' + dockerRegistry, dockerCredentialsId) {
  docker.image(kubectlImage).inside('--entrypoint=""'){
    sh '''
    #!/bin/sh
    set +x
    kubectl get pods -n ${namespace}  -o json > $WORKSPACE/pods.json
    podIP=$(jq -r ".items[] | select(.metadata.generateName | test($appName)).status.podIP" $WORKSPACE/pods.json}
    echo "Pod IP:$podIP"
    '''
  }
}

Since WORKSPACE is a pipeline environment variable exported to the shell interpreter, it is fine as is. If you had used the interpolated syntax, it would need to have been accessed in the env object i.e. env.WORKSPACE.

The definition for appName is not shown in the question, so if that does not resolve as expected, then the question will need to be updated accordingly.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67