AWS CodeBuild构建Angular项目失败仍返回成功状态问题咨询
解决AWS CodeBuild识别Angular项目构建失败的配置方法
根因说明
CodeBuild执行buildspec命令时默认使用普通bash模式,不会因单个命令执行失败就终止整个阶段的流程,会以阶段内最后一条命令的退出码作为整个阶段成功/失败的判断标准。
你当前配置中,npm run nx -- build命令执行失败返回了非0退出码,但后续执行的echo successfully finished build phase...命令返回了0退出码,因此整个build阶段被判定为成功。
修复方案
方法1:全局启用错误立即终止机制(推荐)
在build和post_build阶段的开头添加set -e命令,只要阶段内任意命令返回非0退出码,就会立即终止阶段执行并标记为失败。
修改后的完整buildspec示例如下:
version: 0.2 phases: install: runtime-versions: nodejs: 10 pre_build: commands: - echo Building staging profile build: commands: - set -e - echo Entered the build phase for https://s3.console.aws.amazon.com/s3/buckets/<mycoolbucket> ... - cd db - npm install # Build in production mode - npm run nx -- build db-app-desktop --prod --outputHashing=all - echo successfully finished build phase... post_build: commands: - set -e - echo Entered the post_build phase... - aws s3 sync dist/apps/db-app-desktop s3://<mycoolbucket> --delete; - echo Successfully finished post_build phase
方法2:单独校验关键命令的退出码
如果你不想全局启用set -e,可以在关键构建命令后手动判断退出码,失败时主动终止流程:
# Build in production mode - npm run nx -- build db-app-desktop --prod --outputHashing=all - | if [ $? -ne 0 ]; then echo "构建失败,终止流程" exit 1 fi - echo successfully finished build phase...
额外说明
修复后构建阶段一旦出现错误会立即终止,不会执行后续的产物同步到S3的操作,同时CodeBuild和CodePipeline都会正确标记为FAILED状态,和你的Java后端项目行为保持一致。
内容的提问来源于stack exchange,提问作者Boerger
相关产品推荐
相关产品推荐

