Deploy with Docker Compose on AWS ECS with Github actions

Viewed 1095

I recently started experimenting with ECS, Docker Compose, and context and it's really interesting. I have managed to deploy and host a compose-file through my terminal using docker compose up and ecs-context, but I would also like to automate this through something like Github actions.

I'm struggling to see how one would set that up, and I have yet to find a guide for it.

Are there any good resources for researching this further? What would be the alternate or maybe even better way of doing CI/CD on AWS through Github?

3 Answers

A bit open ended for a StackOverflow question but this blog post walk you through an example of how to use AWS native CI/CD tools to deploy to ECS via the docker compose integration.

I moved away from using Docker Compose and just wrote the CloudFormation templates manually. Docker Compose still has some limitations that require quirky workarounds.

But for anyone wondering how I approached this before moving away from it (including GHA caching):

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
Related