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

使用GitHub Action部署DotNet服务到ECS时遇Service is INACTIVE错误,无法重建服务

解决ECS部署Action无法重建INACTIVE服务的问题

aws-actions/amazon-ecs-deploy-task-definition@v1 默认仅负责更新已存在的ACTIVE状态服务,当服务被删除(状态变为INACTIVE)时,它不会自动重建服务。要实现强制创建服务的效果,有以下两种可行方案:

方案一:使用Action原生参数自动创建服务

该Action支持 create-service-if-not-exists: true 参数,开启后会自动检测服务状态——如果服务不存在(或处于INACTIVE状态),就会自动创建新服务。需要注意的是,首次创建服务时必须补充服务所需的基础配置参数(比如启动类型、网络配置、期望运行数等)。

示例配置如下:

- name: 部署到ECS服务
  uses: aws-actions/amazon-ecs-deploy-task-definition@v1
  with:
    cluster: 你的ECS集群名称
    service: 你的ECS服务名称
    task-definition: 你的任务定义ARN
    create-service-if-not-exists: true
    desired-count: 1
    launch-type: FARGATE
    network-configuration: '{"awsvpcConfiguration": {"subnets": ["subnet-xxxxxx"], "securityGroups": ["sg-xxxxxx"], "assignPublicIp": "ENABLED"}}'

方案二:用AWS CLI提前检查并创建服务

如果原生参数无法满足需求,可以在部署步骤前添加一个自定义步骤,通过AWS CLI检查服务状态,若服务不存在或处于INACTIVE状态,则手动调用创建命令。

示例配置如下:

- name: 检查并按需创建ECS服务
  run: |
    SERVICE_STATUS=$(aws ecs describe-services --cluster 你的ECS集群名称 --services 你的ECS服务名称 --query 'services[0].status' --output text 2>/dev/null || echo "NOT_EXISTS")
    if [ "$SERVICE_STATUS" = "INACTIVE" ] || [ "$SERVICE_STATUS" = "NOT_EXISTS" ]; then
      aws ecs create-service \
        --cluster 你的ECS集群名称 \
        --service-name 你的ECS服务名称 \
        --task-definition 你的任务定义ARN \
        --desired-count 1 \
        --launch-type FARGATE \
        --network-configuration "awsvpcConfiguration={subnets=[subnet-xxxxxx],securityGroups=[sg-xxxxxx],assignPublicIp=ENABLED}"
    fi
  env:
    AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
    AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    AWS_REGION: 你的AWS区域

- name: 部署到ECS服务
  uses: aws-actions/amazon-ecs-deploy-task-definition@v1
  with:
    cluster: 你的ECS集群名称
    service: 你的ECS服务名称
    task-definition: 你的任务定义ARN
    desired-count: 1

注意事项

  • 方案一为官方原生支持,配置更简洁,优先推荐使用。
  • 确保GitHub Runner拥有足够的AWS权限:ecs:DescribeServices、ecs:CreateService、ecs:UpdateService 等。
  • 若使用EC2类型的服务,需替换对应配置参数(比如无需network-configuration,可能需要指定placement-constraints)。

内容的提问来源于stack exchange,提问作者Mike

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 12:55:22