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

如何通过Azure DevOps Pipeline将Angular应用发布至AWS ECR/ECS

Azure DevOps Pipeline 部署Angular应用到AWS ECR/ECS 配置指南

问题描述

我是Azure Pipelines新手,尝试通过Azure DevOps Pipeline将Angular应用发布至AWS ECR,目前已编写部分YAML代码,但需要完成以下步骤:

  • 配置AWS凭证
  • 登录AWS Amazon ECR
  • 构建、标记并推送Docker镜像至ECR
  • 在AWS ECS任务定义中填入新镜像ID
  • 部署Amazon ECS任务定义

以下是我已编写的代码:

trigger:
- staging-release

resources:
- repo: self

variables:
  tag: '$(Build.BuildId)'
  AWS_REGION: eu-west-1                                       # set this to your preferred AWS region, e.g. us-west-1
  ECR_REPOSITORY: ********                      # set this to your Amazon ECR repository name
  ECS_SERVICE: ********                          # set this to your Amazon ECS service name
  ECS_CLUSTER: ********                                     # set this to your Amazon ECS cluster name
  ECS_TASK_DEFINITION: ********      # set this to the path to your Amazon ECS task definition
                                                            # file, e.g. .aws/task-definition.json
  CONTAINER_NAME: ********                       # set this to the name of the container in the
                                                            # containerDefinitions section of your task definition


stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: ubuntu-latest
    steps:
    - task: Docker@2
      displayName: Build an image
      inputs:
        command: build
        dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
        tags: |
          $(tag)

    #Amazon Credentials
    - task: AWSCLI@1
      inputs: 
        awsCommand: 


    - script: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile.staging .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"

完整配置方案

下面是补全后的YAML配置,包含所有需要的步骤:

trigger:
- staging-release

resources:
- repo: self

variables:
  tag: '$(Build.BuildId)'
  AWS_REGION: eu-west-1
  ECR_REPOSITORY: ********
  ECS_SERVICE: ********
  ECS_CLUSTER: ********
  ECS_TASK_DEFINITION: ********
  CONTAINER_NAME: ********
  # 自动生成ECR注册表地址,需替换为你的AWS账户ID
  ECR_REGISTRY: 你的AWS账户ID.dkr.ecr.$(AWS_REGION).amazonaws.com

stages:
- stage: Build
  displayName: 构建并推送镜像至ECR
  jobs:
  - job: Build
    displayName: 构建镜像
    pool:
      vmImage: ubuntu-latest
    steps:
    # 1. 配置AWS凭证
    - task: AmazonWebServices.aws-vsts-tools.AWS Credentials.AWSCredentials@1
      displayName: '配置AWS凭证'
      inputs:
        awsCredentials: '你的AWS服务连接名称' # 需提前在Azure DevOps创建AWS服务连接
        regionName: $(AWS_REGION)

    # 2. 登录AWS ECR
    - task: AmazonWebServices.aws-vsts-tools.AWSCLI.AWSCLI@1
      displayName: '登录ECR'
      inputs:
        awsCommand: ecr
        awsSubCommand: get-login-password
        awsArguments: --region $(AWS_REGION) | docker login --username AWS --password-stdin $(ECR_REGISTRY)
        workingDirectory: '$(Build.SourcesDirectory)'

    # 3. 构建、标记并推送Docker镜像
    - task: Docker@2
      displayName: '构建并推送镜像'
      inputs:
        command: buildAndPush
        dockerfile: '$(Build.SourcesDirectory)/Dockerfile.staging'
        repository: $(ECR_REPOSITORY)
        tags: |
          $(tag)

- stage: Deploy
  displayName: 部署至ECS
  dependsOn: Build
  jobs:
  - job: Deploy
    displayName: 更新ECS任务定义并部署
    pool:
      vmImage: ubuntu-latest
    steps:
    # 重新配置AWS凭证
    - task: AmazonWebServices.aws-vsts-tools.AWS Credentials.AWSCredentials@1
      displayName: '配置AWS凭证'
      inputs:
        awsCredentials: '你的AWS服务连接名称'
        regionName: $(AWS_REGION)

    # 4. 更新ECS任务定义中的镜像地址
    - task: AmazonWebServices.aws-vsts-tools.ECSTaskDefinitionModifier.ECSTaskDefinitionModifier@1
      displayName: '更新ECS任务定义镜像'
      inputs:
        taskDefinition: $(ECS_TASK_DEFINITION)
        containerName: $(CONTAINER_NAME)
        imageUri: $(ECR_REGISTRY)/$(ECR_REPOSITORY):$(tag)

    # 5. 部署更新后的任务定义到ECS服务
    - task: AmazonWebServices.aws-vsts-tools.ECSServiceDeploy.ECSServiceDeploy@1
      displayName: '部署至ECS服务'
      inputs:
        clusterName: $(ECS_CLUSTER)
        serviceName: $(ECS_SERVICE)
        taskDefinition: $(ECS_TASK_DEFINITION)

关键注意事项

  • AWS服务连接创建:在Azure DevOps项目的「项目设置」→「服务连接」中创建AWS类型连接,填入你的AWS Access Key和Secret Key,后续在任务中引用该连接名称。
  • ECR_REGISTRY变量:格式为AWS账户ID.dkr.ecr.区域.amazonaws.com,可通过AWS CLI命令aws sts get-caller-identity --query Account --output text获取账户ID。
  • Dockerfile选择:确保Dockerfile.staging是对应staging环境的构建文件,若使用默认Dockerfile可修改为Dockerfile。
  • 任务定义文件:本地需存在对应的ECS任务定义JSON文件,路径要与变量ECS_TASK_DEFINITION一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 02:20:55