A complete Github Actions deployment pipeline

Django on Amazon ECS

december 2023 - 8 min read

Content

Intro

Continuous Deployment (CD) is a methodology that involves releasing code changes to customers as soon as they are pushed to the main branch. This automated process accelerates the feedback loop with customers, allowing for frequent releases of improved product versions.

Github Actions, a powerful feature integrated into Github, facilitates the seamless setup of CI/CD pipelines alongside your code. The key advantage lies in its ability to trigger workflows based on events such as code pushes, providing an all-in-one platform for code hosting and automation.

Tech stack

In this tutorial, we'll be deploying a Django app to AWS using the following tech stack:

  • Amazon ECS
  • Elastic Container Registry (ECR)
  • Elastic Container Service (ECS)

ECR is a fully-managed Docker container registry that simplifies storing, managing, and deploying Docker container images. ECS, on the other hand, is a scalable container orchestration service that supports Docker containers, making it easy to run and scale containerized applications on AWS.

Pipeline steps

The deployment pipeline steps are sequentially listed in the YAML workflow, it is triggered upon pushing new code to the codebase. We'll go through those steps here.

1. Start: The Github Actions workflow begins by checking out the latest code.

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    environment: development
    steps:
    - name: Checkout
      uses: actions/checkout@v3
              

2.AWS setup: AWS credentials are configured, allowing secure interactions with AWS services.

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

3. Container image: After logging in to ECR, a new Docker image is built with the latest Django app changes and pushed.

- name: Login to Amazon ECR
  id: login-ecr
  uses: aws-actions/amazon-ecr-login@v1
- 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
              

4. ECS task update: The ECS task definition is updated with the latest Docker image tag.

- name: Fill in the new image ID in the Amazon ECS task definition
  id: task-def
  uses: aws-actions/amazon-ecs-render-task-definition@v1.1.2
  with:
    task-definition: ${{ env.ECS_TASK_DEFINITION }}
    container-name: ${{ env.CONTAINER_NAME }}
    image: ${{ steps.build-image.outputs.image }}
              

5. Release: The updated task definition is deployed to ECS, making the new version of the Django app accessible.

- name: Deploy Amazon ECS task definition
  uses: aws-actions/amazon-ecs-deploy-task-definition@v1
  with:
    task-definition: ${{ steps.task-def.outputs.task-definition }}
    service: ${{ env.ECS_SERVICE }}
    cluster: ${{ env.ECS_CLUSTER }}
    wait-for-service-stability: true
              

Conclusion

To view a video format of this tutorial, you can check Github Actions continuous deployment pipeline for Django on Amazon ECS on Youtube. You can also find the code on Github.