This document explains how to set up Continuous Deployment (CD) with AWS using GitHub Actions.
Before you start, make sure you have the following:
-
AWS Account: You need an AWS account. Sign up here.
-
Amazon ECR (Elastic Container Registry) Setup:
- Create a new repository in Amazon ECR.
- Note down the repository URI, which will be used in the GitHub Actions workflow.
-
AWS Credentials:
- AWS Access Key ID
- AWS Secret Access Key
- These credentials should have permission to interact with ECR and ECS.
-
Create Environments:
The GitHub Actions workflow will automatically deploy to the correct environment based on the branch being pushed to. The branch
main
will always be linked to theproduction
environment, while other branches will use their own names as the environment. All environments added in GitHub must have the same name as the branches. -
GitHub Repository Setup:
- Environment Secrets: Add the following secrets to your GitHub environments (these are specific to each environment and not set at the repository level):
AWS_ACCESS_KEY_ID
: Your AWS Access Key ID.AWS_SECRET_ACCESS_KEY
: Your AWS Secret Access Key.
- Environment Variables: Add the following variables to your GitHub environments:
AWS_REGION
: The region where your ECR and ECS are set up (e.g.,us-east-1
).ECR_REPOSITORY
: The name of your ECR repository.ECS_TASK_DEFINITION
: The ARN of your ECS task definition.ECS_TASK_DEFINITION_PATH
: The path to your ECS task definition file.CONTAINER_NAME
: The name of the container defined in your ECS task definition.ECS_SERVICE
: The name of your ECS service.ECS_CLUSTER
: The name of your ECS cluster.
- Environment Secrets: Add the following secrets to your GitHub environments (these are specific to each environment and not set at the repository level):
-
GitHub Actions Workflow: To set up the GitHub Actions workflow for continuous deployment to AWS, you need to modify the existing cd.yml file in the .github/workflows directory of your GitHub repository.
Uncomment the branches section under
on: push:
and add the necessary branches to enable automatic deployment. For example:on: push: branches: - main - dev