繁体   English   中英

YAML_FILE_ERROR 消息:预期命令 [0] 为字符串类型:

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

我正在尝试使用 CodeBuild 在 AWS 上构建我的项目。 我已将此构建规范文件放在根目录中。 CodeBuild 能够读取文件,但无法继续。 但是我在 CodeBuild 上遇到了以下错误。

CodeBuild 日志错误:

> [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

我的 buildspec.yaml 文件:

    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)

根据评论,问题是在phase: $build

yaml在遇到空格和:时会出现一些问题,如下面的 GitHub 问题所示:

当我在我的 yml 命令中放置一个echo语句时出现此错误,该命令试图打印一系列-字符只是为了设置我的 output 的样式 `- echo '--- got here ---'

不得不把它拿出来并加上下划线_

错误是因为:在 echo 内的第 30 行。 正如@Marcin 提到的,YAML 不喜欢文本中带有空格的冒号。

为了处理 YML 中的“:”和“-”,这两个问题都非常严重,我最终在代码构建中执行了以下操作,以使破折号和逗号在字符串中工作:

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

解释:

  • python -c - 调用 python 并执行命令print(chr(58))
  • chr(58) - 返回“:”
  • chr(45) - 返回“-”
  • print(chr(58)) - 将“:”发送到由colon变量分配/使用的 sys.out
  • print(chr(45)) - 将“-”发送到由dash变量分配/使用的 sys.out
  • ${colon}将替换 field_to_hold 字符串值中的:
  • ${dash}将替换 field_to_hold 字符串值中的-

注意事项:

  1. ${colon}不会替换单引号字符串,它必须是双引号(即 - "
  2. 对于需要冒号或破折号的每个部分,您必须复制colon=$(python -c "print(chr(58))") ,因为变量不会从一个部分传递到另一个部分(未记录,但通过反复试验发现)。
  3. 代码构建服务器/模板上需要Python
  4. 如果您没有python ,您可以使用 linux/java 执行类似的操作,将ascii 值58 或 45 转换为ascii 字符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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