You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows EC2部署失败求助:如何配置appspec.yml(Bitbucket Pipeline)

Windows EC2部署的appspec.yml正确配置指南

针对你遇到的ERROR: Deployment failed. Fetching deployment informat$APPLICATION_NAME错误,大概率是变量引用格式错误或appspec.yml的Windows适配配置缺失导致的,下面是针对Windows EC2环境的标准appspec.yml配置方案及注意事项:

完整示例配置

version: 0.0
os: windows
files:
  - source: ./build-output
    destination: C:/inetpub/wwwroot/your-application
hooks:
  BeforeInstall:
    - location: scripts/stop-services.ps1
      timeout: 300
      runas: Administrator
  AfterInstall:
    - location: scripts/configure-app.ps1
      timeout: 300
      runas: Administrator
  ApplicationStart:
    - location: scripts/start-services.ps1
      timeout: 300
      runas: Administrator

关键字段说明

  • version: 固定写0.0,是CodeDeploy要求的必填格式
  • os: 必须指定为windows,不指定会默认按Linux环境处理,直接导致路径、脚本执行逻辑出错
  • files:定义代码/构建产物的部署映射
    • source: 对应Bitbucket Pipeline中构建完成后要上传的本地目录(比如你的前端dist目录、后端编译输出目录)
    • destination: Windows EC2实例上的绝对目标路径,推荐用正斜杠(/)避免YAML转义问题,比如C:/inetpub/wwwroot/your-app
  • hooks: 部署流程中的触发脚本,Windows环境下仅支持PowerShell(.ps1)脚本,核心参数:
    • location: 脚本相对于appspec.yml的路径(比如放在scripts子目录下)
    • timeout: 脚本执行超时时间(单位:秒),根据脚本复杂度调整,避免因超时触发部署失败
    • runas: Windows下操作服务、IIS等资源需要管理员权限,必须指定Administrator

针对你的错误的额外修复点

  1. 变量引用规范: 如果要使用$APPLICATION_NAME这类环境变量,在PowerShell脚本里必须写成$env:APPLICATION_NAME,不能直接用$APPLICATION_NAME;同时要确认Bitbucket Pipeline或CodeDeploy是否正确传递了该变量
  2. 目标路径检查: 确保EC2实例上的目标部署路径已经存在,或者在BeforeInstall脚本中添加创建路径的逻辑(比如New-Item -Path "C:/inetpub/wwwroot/your-app" -ItemType Directory -Force)
  3. PowerShell执行策略: 在EC2实例上提前设置允许执行脚本的策略,执行Set-ExecutionPolicy RemoteSigned(管理员权限下运行),否则CodeDeploy Agent无法执行你的.ps1脚本

钩子脚本示例

stop-services.ps1(停止服务)

# 停止IIS网站和服务
Stop-Website -Name "YourAppSite"
Stop-Service W3SVC -Force

start-services.ps1(启动服务)

Start-Service W3SVC
Start-Website -Name "YourAppSite"

内容的提问来源于stack exchange,提问作者Abu Bakir Uralov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 02:35:22