AWS CodeBuild执行dotnet publish命令报错如何处理?
问题原因
你遇到的报错根本原因是当前AWS CodeBuild构建环境未安装.NET 5.0 SDK,你的buildspec配置中仅指定了docker运行时,没有包含dotnet相关依赖,因此执行dotnet restore、dotnet publish等命令时会返回非0退出码,和输出路径参数无关。
解决方法
方案1:通过runtime-versions直接指定.NET版本(推荐)
AWS CodeBuild的标准Amazon Linux 2镜像(如aws/codebuild/standard:5.0及以上版本)支持直接在runtime-versions中声明.NET 5环境,修改install阶段配置即可:
phases: install: runtime-versions: docker: 18 dotnet: 5.0 # 新增该行配置
方案2:手动安装.NET 5.0 SDK
如果你使用的构建镜像不支持runtime-versions声明dotnet,可在install阶段的commands中添加手动安装命令:
phases: install: runtime-versions: docker: 18 commands: # 安装.NET 5 SDK - wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh - chmod +x ./dotnet-install.sh - ./dotnet-install.sh --channel 5.0 --install-dir /usr/share/dotnet - ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
可选优化(降低报错概率)
- 执行dotnet命令前先验证环境是否正常:在build阶段开头添加
dotnet --info,确认SDK版本加载正常 - 提前创建输出目录避免路径不存在报错:在
dotnet publish前添加mkdir -p ../../../docker/build/outputs/Host - 发布失败时输出详细调试日志:将发布命令改为
dotnet publish -c release -o ../../../docker/build/outputs/Host -v d,可打印完整错误信息方便排查
修改后完整buildspec参考
version: 0.2 phases: install: runtime-versions: docker: 18 dotnet: 5.0 pre_build: commands: - echo pre-build started on `date` - echo Logging in to Amazon ECR... - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com build: commands: - echo Building dotnet API... - cd aspnet-core/src/SITR.Web.Host - echo Verify dotnet environment... - dotnet --info - echo Executing dotnet restore... - dotnet restore - echo Create output directory... - mkdir -p ../../../docker/build/outputs/Host - echo Executing dotnet publish... - dotnet publish -c release -o ../../../docker/build/outputs/Host post_build: commands: - echo Pushing the Docker image... - docker-compose --file=docker-compose-qa.yml push - echo Build completed on `date`
内容的提问来源于stack exchange,提问作者George Wurthmann
相关产品推荐
相关产品推荐

