Can't pass `json` string to `sh` step in Jenkins pipeline Can't pass `json` string to `sh` step in Jenkins pipeline jenkins jenkins

Can't pass `json` string to `sh` step in Jenkins pipeline


Yes quotes are silently dropped . You can refer to this page ,it shows escaping of characters in a very elaborate way :https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4


Following solution works:

MANIFEST= sh( script:"(aws ecr batch-get-image --repository-name ${ECRNAME} --image-ids imageTag=${IMAGE} --query 'images[0].imageManifest' --output json)",returnStdout: true)MANIFEST1 = "${MANIFEST}".replace('\\n', '')sh( script:"aws ecr put-image --repository-name ${ECRNAME} --image-tag ${env.RELEASE_SCOPE}_latest --image-manifest ${MANIFEST1}",returnStdout: true)

Notice: I have used --output json and then replaced '\n' from json.


Ran into this exact same issue and ended up using a shell script to handle the AWS ECR tagging. Tried various combinations of quotes, escaping, even tried changing the return type of the batch-get-image query to JSON. Once it's passed to the put-image command, either returned the Invalid JSON Syntax error or ended up creating a duplicate image in ECR with the new tag.

Here's the shell script:

#!/usr/bin/env sh repo=$1tag=$2tag_as=$3MANIFEST=$(aws ecr batch-get-image --repository-name $1 --image-ids imageTag=$2 --query images[].imageManifest --output text)aws ecr put-image --repository-name $1 --image-tag $3 --image-manifest "$MANIFEST"

In Jenkins:

result = sh(returnStdout: true, script: "./retag.sh ${REPO_NAME} ${TAG} ${TAG_AS}")