先决条件
在创建 GitHub Actions 工作流程之前,首先需要对 Amazon ECR 和 ECS 完成以下设置步骤:
- 
创建 Amazon ECR 仓库以存储映像。 例如,使用 AWS CLI: Bash aws ecr create-repository \ --repository-name MY_ECR_REPOSITORY \ --region MY_AWS_REGIONaws ecr create-repository \ --repository-name MY_ECR_REPOSITORY \ --region MY_AWS_REGION对于以下工作流中的变量 ECR_REPOSITORY,务必要使用相同的 Amazon ECR 存储库名称(此处以MY_ECR_REPOSITORY表示)。对于以下工作流中的变量 AWS_REGION,务必要使用相同的亚马逊云科技区域值(此处以MY_AWS_REGION表示)。
- 
创建 Amazon ECS 任务定义、群集和服务。 有关详细信息,请参阅 Amazon ECS 控制台入门向导或 Amazon ECS 文档中的入门指南。 请务必记下你为 Amazon ECS 服务和群集设置的名称,并将其用于以下工作流中的变量 ECS_SERVICE和ECS_CLUSTER。
- 
将 Amazon ECS 任务定义存储为 GitHub 存储库中的 JSON 文件。 该文件的格式应与生成的输出相同: Bash aws ecs register-task-definition --generate-cli-skeleton aws ecs register-task-definition --generate-cli-skeleton务必将以下工作流中的变量 ECS_TASK_DEFINITION设置为 JSON 文件的路径。务必将以下工作流中的变量 CONTAINER_NAME设置为任务定义中containerDefinitions部分的容器名称。
- 
创建名称为 AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY的 GitHub Actions 密钥,以用于存储 Amazon IAM 访问密钥的值。有关为 GitHub Actions 创建机密的详细信息,请参阅“在 GitHub Actions 中使用机密”。 对于下面使用的每项操作,请参阅文档,以了解适用于 IAM 用户的推荐 IAM 策略,以及用于处理访问密钥凭据的方法。 
- 
Optionally, configure a deployment environment. 环境用于描述常规部署目标,例如 production、staging或development。 当 GitHub Actions 工作流部署到某个环境时,该环境将显示在存储库的主页上。 可以使用环境来要求审批作业以继续,限制可以触发工作流的分支,使用自定义部署保护规则控制部署,或限制对机密的访问权限。 有关创建环境的详细信息,请参阅“管理部署环境”。
创建工作流程
完成先决条件后,可以继续创建工作流程。
下面的示例工作流演示了如何生成容器映像并将其推送到 Amazon ECR。 然后会使用新的映像 ID 来更新任务定义,并将任务定义部署到 Amazon ECS。
务必要为工作流中 env 密钥的所有变量提供自己的值。
如果配置了部署环境,请将 environment 的值更改为环境的名称。 如果未配置环境 ,或者如果工作流位于专用存储库中并且你未使用 GitHub Enterprise Cloud,请删除 environment 密钥。
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
name: Deploy to Amazon ECS
on:
  push:
    branches:
      - main
env:
  AWS_REGION: MY_AWS_REGION                   # set this to your preferred AWS region, e.g. us-west-1
  ECR_REPOSITORY: MY_ECR_REPOSITORY           # set this to your Amazon ECR repository name
  ECS_SERVICE: MY_ECS_SERVICE                 # set this to your Amazon ECS service name
  ECS_CLUSTER: MY_ECS_CLUSTER                 # set this to your Amazon ECS cluster name
  ECS_TASK_DEFINITION: MY_ECS_TASK_DEFINITION # set this to the path to your Amazon ECS task definition
                                               # file, e.g. .aws/task-definition.json
  CONTAINER_NAME: MY_CONTAINER_NAME           # set this to the name of the container in the
                                               # containerDefinitions section of your task definition
jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    environment: production
    steps:
      - name: Checkout
        uses: actions/checkout@v5
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ env.AWS_REGION }}
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a
      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          # Build a docker container and
          # push it to ECR so that it can
          # be deployed to ECS.
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc
        with:
          task-definition: ${{ env.ECS_TASK_DEFINITION }}
          container-name: ${{ env.CONTAINER_NAME }}
          image: ${{ steps.build-image.outputs.image }}
      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: ${{ env.ECS_SERVICE }}
          cluster: ${{ env.ECS_CLUSTER }}
          wait-for-service-stability: true
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
name: Deploy to Amazon ECS
on:
  push:
    branches:
      - main
env:
  AWS_REGION: MY_AWS_REGION                   # set this to your preferred AWS region, e.g. us-west-1
  ECR_REPOSITORY: MY_ECR_REPOSITORY           # set this to your Amazon ECR repository name
  ECS_SERVICE: MY_ECS_SERVICE                 # set this to your Amazon ECS service name
  ECS_CLUSTER: MY_ECS_CLUSTER                 # set this to your Amazon ECS cluster name
  ECS_TASK_DEFINITION: MY_ECS_TASK_DEFINITION # set this to the path to your Amazon ECS task definition
                                               # file, e.g. .aws/task-definition.json
  CONTAINER_NAME: MY_CONTAINER_NAME           # set this to the name of the container in the
                                               # containerDefinitions section of your task definition
jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    environment: production
    steps:
      - name: Checkout
        uses: actions/checkout@v5
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ env.AWS_REGION }}
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a
      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          # Build a docker container and
          # push it to ECR so that it can
          # be deployed to ECS.
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc
        with:
          task-definition: ${{ env.ECS_TASK_DEFINITION }}
          container-name: ${{ env.CONTAINER_NAME }}
          image: ${{ steps.build-image.outputs.image }}
      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: ${{ env.ECS_SERVICE }}
          cluster: ${{ env.ECS_CLUSTER }}
          wait-for-service-stability: true
其他阅读材料
对于原始工作流模板,请参阅 GitHub Actions starter-workflows 存储库中的 aws.yml。
有关这些示例中使用的服务的详细信息,请参阅以下文档:
- Amazon AWS 文档中的“IAM 的安全防御最佳实操”。
- 官方 AWS“配置 AWS 凭据”操作。
- 官方 AWS Amazon ECR“登录”操作。
- 官方 AWS Amazon ECS“呈现任务定义”操作。
- 官方 AWS Amazon ECS“部署任务定义”操作。