使用GitHub Actions在AWS ECS上部署Docker Compose。

6

我最近开始尝试使用ECS、Docker Compose和context技术,感觉非常有趣。通过我的终端使用docker compose up和ecs-context已经成功部署并托管了compose文件,但我还想通过类似Github Actions的工具来实现自动化。

我不知道如何设置这个过程,也没有找到相关指南。

有没有好的资源可以进一步研究这方面的内容?在AWS上通过Github进行CI/CD的备选方案是什么,可能会更好一些呢?


1
https://docs.github.com/en/actions/deployment/deploying-to-your-cloud-provider/deploying-to-amazon-elastic-container-service - rethab
4个回答

3

1

我放弃了使用Docker Compose,转而手动编写CloudFormation模板。 Docker Compose仍然存在一些限制,需要奇怪的解决方法。

但是,对于任何想知道我在放弃之前如何处理它的人(包括GHA缓存):

name: Deploy with Docker Compose

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Setup Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1

      - name: Build and push <YOUR_IMAGE>
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          cache-from: type=gha,scope=<YOUR_IMAGE>
          cache-to: type=gha,scope=<YOUR_IMAGE>
          tags: |
            ${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPOSITORY }}:<YOUR_IMAGE>-${{ github.sha }}
            ${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPOSITORY }}:<YOUR_IMAGE>-latest

      - name: Install Docker Compose
        run: curl -L https://raw.githubusercontent.com/docker/compose-cli/main/scripts/install/install_linux.sh | sh

      - name: Docker context
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: ${{ secrets.AWS_REGION }}
          ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
          GITHUB_SHA: ${{ github.sha }}
        run: |
          docker context create ecs ecs-context --from-env
          docker --context ecs-context compose up

1
这是 Docker 的一个全新特性:docker/compose-ecs。然而,docker-compose 中的一些功能受到限制,例如 dns 或 networks,但在某些情况下可能会适用。

0
这个问题有点开放式,不过这篇博客文章会通过一个示例向您展示如何使用AWS本地CI/CD工具通过docker compose集成部署到ECS。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接