CI/CD with GitHub Actions — Deploying to Amazon Elastic Container Service
We all know the importance of CI/CD in the software development process. So, considering this we can use GitHub for both purposes for continuous integration and continuous delivery. In this documentation, we will look at how to build and deploy your application using the GitHub actions platform.
Let’s get started 😻
What is GitHub Action?
GitHub Actions help you automate tasks with your software development lifecycle. The GitHub action configuration file uses the YML format. GitHub Actions are event-driven, which means you can run a series of commands after a specified event has occurred. For example, every time someone creates a release from GitHub on a specified branch then automatic commands get executed.
Deploying to Amazon Elastic Container Service
In this example, we will see how to use GitHub Actions to build a containerized application, push it to Amazon Elastic Container Registry (ECR), and deploy it to Amazon Elastic Container Service (ECS). On every new release in your GitHub repository, the GitHub Actions workflow builds and pushes a new container image to Amazon ECR, and then deploys a new task definition to Amazon ECS.
Prerequisites
Before creating your GitHub Actions workflow configuration file, you will first need to complete the following steps:-
- AWS CLI setup with appropriate credentials.
- Create an Amazon ECR repository to store your images.
- Create an Amazon ECS task definition, cluster, and service.
- Store your Amazon ECS task definition as a JSON file in your GitHub repository.
You can generate the tasks definition template using the following command shell command:
aws ecs register-task-definition --generate-cli-skeleton
GitHub Secretes Configuration
You need to store the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY into the secrets of your repository on GitHub. You can follow this link as a reference.
Creating the GitHub Action workflow
Once you’ve completed the prerequisites and GitHub configuration you can proceed with creating the workflow.
The below example workflow shows how to build a container image and push it to Amazon ECR. It then updates the task definition with the new image ID and deploys the task definition to Amazon ECS.
The following steps are performed in the workflow file.
- checks-out your repository under GitHub Workspace, so your workflow can access it.
- Configure the AWS credentials.
- Login to AWS ECR.
- Build, Tag, and push the image to AWS ECR.
- Update the AWS ECS service with a new task definition.
Note: Ensure that you provide your own values for all the variables in the env key of the workflow.
This is the workflow file for deploying the application on AWS ECS Container. This workflow will trigger when you create the release tag against the main branch in GitHub. Learn more about how to create a release with GitHub here.
Note: Change the path of the docker file under the step “Build, tag, and push image to Amazon ECR.” While building the docker image.
Environment Variables
- AWS_REGION — Operating region of AWS services.
- ECR_REPOSITORY — Name of the ECR repository that you have created.
- ECS_SERVICE — Service name of the ECS Cluster.
- ECS_CLUSTER — Name of the ECS Cluster.
- ECS_TASK_DEFINITION — Path of the ECS task definition in JSON format which is stored in GitHub repository.
- CONTAINER_NAME — Docker container name under the ECS task definition.
At Scalereal We believe in Sharing and Open Source.
So, If you found this helpful please give some claps 👏 and share it with everyone.
Sharing is Caring!
Thank you ;)