2

Following command works on bash shell but YAML parser (bitbucket-pipelines.yaml) reports it as invalid syntax

https://bitbucket-pipelines.atlassian.io/validator

- export TASK_VERSION=$(aws ecs register-task-definition --family MyTask --container-definitions "[{\"name\":\"MyContainer\",\"image\":\"1234567.dkr.ecr.us-west-1.amazonaws.com/ecs-repository:1.0.13\",\"cpu\":50,\"portMappings\": [{\"hostPort\": 80,\"containerPort\": 80,\"protocol\":\"tcp\"}],\"memory\":300}]" | jq --raw-output '.taskDefinition.revision')

Even though I have escaped all double quotes using \ char, I think [ and { are throwing it off.

Any ideas?

HirenP
  • 61
  • 1
  • 6
  • Please share the output of the command. – Greg May 14 '18 at 21:30
  • What is the output of `echo "$TASK_VERSION"`? – Greg May 14 '18 at 21:31
  • Output of the echo "$TASK_VERSION" command is integer. It is the version number of aws ecs task. If you put this command in any YAML validator, it doesn't parse as valid. Notice that --container-definitions value is a JSON string, which throws parser off. – HirenP May 14 '18 at 21:34

1 Answers1

6

I assume the line in question is part of a larger, validly structured bitbucket-pipelines.yaml file.

Try placing the script in a block scalar.

- |
  export TASK_VERSION=$(aws ecs register-task-definition --family MyTask --container-definitions "[{\"name\":\"MyContainer\",\"image\":\"1234567.dkr.ecr.us-west-1.amazonaws.com/ecs-repository:1.0.13\",\"cpu\":50,\"portMappings\": [{\"hostPort\": 80,\"containerPort\": 80,\"protocol\":\"tcp\"}],\"memory\":300}]" | jq --raw-output '.taskDefinition.revision')
Greg
  • 23,155
  • 11
  • 57
  • 79
  • 1
    Yes, a block scalar is the way to go (if you don't need YAML escape sequences). I'll add a link to my article about YAML quoting http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html for more information – tinita May 14 '18 at 21:48
  • 1
    Also it should be noted that a folded block scalar `>` would be better to read here. You can break the long line into several lines, and they will be folded together when loading – tinita May 14 '18 at 21:50
  • Worked !! Life saver ! Thanks @Greg – HirenP May 14 '18 at 22:05