[英]YAML_FILE_ERROR Message: Expected Commands[0] to be of string type:
I'm trying to build my project on AWS using CodeBuild.我正在尝试使用 CodeBuild 在 AWS 上构建我的项目。 I have placed this buildspec file in the root directory.
我已将此构建规范文件放在根目录中。 CodeBuild is able to read the file but it is unable to proceed forward.
CodeBuild 能够读取文件,但无法继续。 But I got the following error on CodeBuild.
但是我在 CodeBuild 上遇到了以下错误。
CodeBuild Log Error: 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
My buildspec.yaml file:我的 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)
Based on the comment, the issue was the use of colon in phase: $build
.根据评论,问题是在
phase: $build
。
yaml
has some problems when it encounters a space and :
as indicated in the following GitHub issue: yaml
在遇到空格和:
时会出现一些问题,如下面的 GitHub 问题所示:
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 ---'当我在我的 yml 命令中放置一个
echo
语句时出现此错误,该命令试图打印一系列-
字符只是为了设置我的 output 的样式 `- echo '--- got here ---'
had to take that out and put underscores _
不得不把它拿出来并加上下划线
_
The error was because of the :
in the 30th line inside echo.错误是因为
:
在 echo 内的第 30 行。 As @Marcin mentioned YAML does not like colons with spaces in the text.正如@Marcin 提到的,YAML 不喜欢文本中带有空格的冒号。
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:为了处理 YML 中的“:”和“-”,这两个问题都非常严重,我最终在代码构建中执行了以下操作,以使破折号和逗号在字符串中工作:
- 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))
python -c
- 调用 python 并执行命令print(chr(58))
chr(58)
- returns ":" chr(58)
- 返回“:”chr(45)
- returns "-" chr(45)
- 返回“-”print(chr(58))
- sends the ":" to the sys.out which is assigned/consumed by colon
variable print(chr(58))
- 将“:”发送到由colon
变量分配/使用的 sys.outprint(chr(45))
- sends the "-" to the sys.out which is assigned/consumed by dash
variable print(chr(45))
- 将“-”发送到由dash
变量分配/使用的 sys.out${colon}
will substitute the :
in the field_to_hold string value ${colon}
将替换 field_to_hold 字符串值中的:
${dash}
will substitute the -
in the field_to_hold string value ${dash}
将替换 field_to_hold 字符串值中的-
Caveats:注意事项:
${colon}
will not substitute in a single quoted string, it must be double quoted (ie - "
) ${colon}
不会替换单引号字符串,它必须是双引号(即 - "
)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).colon=$(python -c "print(chr(58))")
,因为变量不会从一个部分传递到另一个部分(未记录,但通过反复试验发现)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.