简体   繁体   中英

YAML_FILE_ERROR Message: Expected Commands[0] to be of string type:

I'm trying to build my project on AWS using CodeBuild. I have placed this buildspec file in the root directory. CodeBuild is able to read the file but it is unable to proceed forward. But I got the following error on CodeBuild.

CodeBuild Log Error:

> [Container] 2020/05/19 08:56:07 Waiting for agent ping 
> [Container] 2020/05/19 08:56:09 Waiting for DOWNLOAD_SOURCE 
> [Container] 2020/05/1908:56:14 Phase is DOWNLOAD_SOURCE [Container] 2020/05/19 08:56:14 YAML location is myRepoPath/buildspec.yml [Container] 2020/05/19 08:56:14 Phase complete: DOWNLOAD_SOURCE State: FAILED 
> [Container] 2020/05/19 08:56:14 Phase context status code: YAML_FILE_ERROR Message: Expected Commands[0] to be of string type: found subkeys instead at line 30, value of the key tag on line 29 might be empty

My buildspec.yaml file:

    version: 0.2

phases:
  install:
    runtime-versions:
      java: openjdk11

    commands:
      - apt-get update -y
      - apt-get install -y maven
      - pip3 install --upgrade awscli

  pre_build:
    commands:
      - sonar_host_url=""
      - sonar_project_key="$REPOSITORY_NAME"
      - sonar_username=$(aws secretsmanager get-secret-value --secret-id $SONARQUBE_USER_CREDENTIAL_SECRET | jq -r '.SecretString' | jq -r '.username')
      - sonar_password=$(aws secretsmanager get-secret-value --secret-id $SONARQUBE_USER_CREDENTIAL_SECRET | jq -r '.SecretString' | jq -r '.password')
      - git checkout $SOURCE_COMMIT

  build:
    commands:
      - builStatus=$(mvn install)
      - result=$(mvn clean sonar:sonar -Dsonar.projectKey=$sonar_project_key -Dsonar.host.url=$sonar_host_url -Dsonar.login=$sonar_username -Dsonar.password=$sonar_password)
      - echo $result

  post_build:
    commands:
      - echo $buildStatus
      - buildComment=$(echo "Status of project build phase : $buildStatus")
      - aws codecommit post-comment-for-pull-request --pull-request-id $PULL_REQUEST_ID --repository-name $REPOSITORY_NAME --before-commit-id $DESTINATION_COMMIT --after-commit-id $SOURCE_COMMIT --content "$buildComment"
      - sonar_link=$(echo $result | egrep -o "you can browse http://[^, ]+")
      - sonar_task_id=$(echo $result | egrep -o "task\?id=[^ ]+" | cut -d'=' -f2)

Based on the comment, the issue was the use of colon in phase: $build .

yaml has some problems when it encounters a space and : as indicated in the following GitHub issue:

I had this error when I put an echo statement in my yml commands which tried to print a series of - characters just to style my output a bit `- echo '--- got here ---'

had to take that out and put underscores _

The error was because of the : in the 30th line inside echo. As @Marcin mentioned YAML does not like colons with spaces in the text.

To handle the ":" and "-" in YML which are both very problematic, I ended up doing the following in codebuild to get dashes and commas working in strings:

- colon=$(python -c "print(chr(58))")
- dash=$(python -c "print(chr(45))")
- field_to_hold="http${colon}//www.example${dash}site.com"

Explanation:

  • python -c - calls python and executes the command print(chr(58))
  • chr(58) - returns ":"
  • chr(45) - returns "-"
  • print(chr(58)) - sends the ":" to the sys.out which is assigned/consumed by colon variable
  • print(chr(45)) - sends the "-" to the sys.out which is assigned/consumed by dash variable
  • ${colon} will substitute the : in the field_to_hold string value
  • ${dash} will substitute the - in the field_to_hold string value

Caveats:

  1. ${colon} will not substitute in a single quoted string, it must be double quoted (ie - " )
  2. for each section that needs the colon or dash, you will have to copy the colon=$(python -c "print(chr(58))") , as the variables do not pass through from section to section (Not Documented, but found out by trial and error).
  3. Requires Python on the codebuild server/template.
  4. IF you do not have python , you might be able to do something similar with linux/java of converting ascii value 58 or 45 to ascii character .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM