使用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
相关产品推荐
相关产品推荐

