Search code examples
jsonyamlaws-codebuildpyyaml

YAML_FILE_ERROR: did not find expected key


I tried to run Buildspec using aws codeBuild and trying to generate process.json file on-fly using jq command. But it gives an error while executing and build goes failed..

build: 
              commands:
                - cp $CODEBUILD_SRC_DIR/qe/performance/* apache-jmeter-5.2/bin/
                - cd apache-jmeter-5.2/bin/
                - DATE=`date "+%Y%m%d-%H-%M-%S"`
                - aws s3 cp $DATE-Report s3://$JMeterScanResultBucket/${ProjectName}/$DATE --recursive
                - jq -n --arg appname "$appname" '{apps: [ {project: wsg, issuetype: "Test Execution", summary: "Test Execution for junit Execution"}]}' > process.json

however, I have received following error: Line 20 goes to above "jq" command

DOWNLOAD_SOURCE 
Failed
YAML_FILE_ERROR: did not find expected key at line 20

Solution

  • A colon plus a space (or newline) in YAML means it's a key-value pair in a mapping:

    key: value
    

    Your jq command contains several colons followed by spaces.

    Since you want a single string, you must quote it.

    There are several ways to do that in YAML.

    Single or double quoting wouldn't be ideal here because the string contains both quote types.

    A folded block scalar is probably the best solution here. Newlines will be folded together as spaces.

    - >
      jq -n --arg appname "$appname"
      '{apps: [ {project: wsg, issuetype: "Test Execution",
      summary: "Test Execution for junit Execution"}]}'
      > process.json
    
    

    An alternative would be the literal block scalar, where you have to escape the linebreak like in a shell script:

    - |
      jq -n --arg appname "$appname" \
      '{apps: [ {project: wsg, issuetype: "Test Execution", \
      summary: "Test Execution for junit Execution"}]}' \
      > process.json