Home > OS >  Jenkins pipeline returns "Bad Subtitution" for shell command
Jenkins pipeline returns "Bad Subtitution" for shell command

Time:10-06

I'm attempting to run the following command in a shell block in my Jenkins pipeline:

jq '.Resources[].TargetService.Properties.TaskDefinition = "'"arn:aws:ecs:us-east-1:${ACCOUNT_NUMBER}:task-definition/${TASK_NAME}:${NEW_REVISION}"'"'

This command works perfectly fine when I run it directly on the Jenkins node in shell.

When I insert it into the Pipeline like this:

stage('process json') {
    steps {
        dir('mydir') {
            sh """
                NEW_REVISION=\$(cat revision.txt)
                jq '.Resources[].TargetService.Properties.TaskDefinition = "'"arn:aws:ecs:us-east-1:\${env.AWS_ACCOUNT_NUMBER}:task-definition/\${env.TASK_NAME}:\${NEW_REVISION}"'"'
            """
        }
    }
}

I get a Bad substitution error without any more information. As far as I know, I'm escaping variables and quotation correctly. I can bypass the error if I remove the double quotes like this:

jq '.Resources[].TargetService.Properties.TaskDefinition = "arn:aws:ecs:us-east-1:${ACCOUNT_NUMBER}:task-definition/${TASK_NAME}:${NEW_REVISION}"'

But that ends up processing the variables literally.

Notes: I'm aware of the security issue by not passing jq --arg and prepared to modify my command after I can get the simpler format working. revision.txt contains a numeric value. The env.* variables are declared earlier as part of the pipeline environment.

CodePudding user response:

env is a Jenkins Object and you seem to be escaping env.* variables as well. If you have already exported these variables as Environment variables they should be available to you in the shell environment. So simply drop the env part from the variables or remove the escape characters from such variables and let Jenkins interpolate them.

stage('process json') {
    steps {
        dir('mydir') {
            sh """
                NEW_REVISION=\$(cat revision.txt)
                jq '.Resources[].TargetService.Properties.TaskDefinition = "'"arn:aws:ecs:us-east-1:\${AWS_ACCOUNT_NUMBER}:task-definition/\${TASK_NAME}:\${NEW_REVISION}"'"'
            """
        }
    }
}
  • Related