In this blog, we will learn how to set up a workflow to push your Docker image to Amazon ECR using GitHub Actions.
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that helps in automating the build, test, and deployment process. With GitHub Actions, you can create workflows that can build and test every push & pull request in your repository, or deploy merged pull requests to production.
- Working Knowledge of Docker
- Basic knowledge of Github Actions Syntax
- Account on AWS
What is Amazon Elastic Container Registry?
Amazon Elastic Container Registry (ECR) is a fully managed container registry that makes it easy to store, manage, share, and deploy your container images and artifacts.
To create an ECR repository, go to the Amazon Console, then ECR, and then Create Repository.
Once Created, your ECR repository will be present under the “Repositories” section.
Set Amazon Secrets in GitHub Repo
Since we have to deploy the docker image to ECR. So, we have to reference AWS secrets in the GitHub Actions workflow. Therefore, all the required secrets should be present in “repository secrets”.
Go to Settings, then Secrets, and then New repository secret.
- AWS_ACCESS_KEY_ID – The AWS Access Key ID
- AWS_SECRET_ACCESS_KEY – The AWS Secret Access Key
- ECR_REPOSITORY – The name of the AWS ECR repository you created
Workflow in GitHub Actions
- A Workflow is a process that consists of one or multiple jobs.
- Workflow gets triggered by an event or manually.
- An event is a specific activity in a repository that triggers a workflow run. E.g- When someone creates a pull request or pushes a commit to a repository.
- Workflows use a YAML syntax and are present in the .github/workflows directory in your repository.
Creating a Workflow!
Let’s create a workflow to deploy the docker image to the ECR repository that we created a while ago.
The directory structure of the GitHub repository is:
We can create a custom workflow for the repository inside the “.github/workflows” directory, or we can also select pre-configured workflows already present on GitHub.
Workflow to deploy the docker image to ECR is present inside the .github/workflows folder.
This workflow will start when someone pushes on the main branch of the repository. Once the workflow triggers, it will start the “build” job on the “ubuntu” GitHub runner and will run all the series of “steps”.
To Understand the GitHub Actions syntax please refer to this link.
Understanding the flow of workflow
The workflow will start once any commit gets pushed to the “main” branch. Build job will start on ubuntu runner and the following steps will run sequentially:
- Configure AWS credentials: This step uses aws-actions/configure-aws-credentials@v1 action to configure the AWS credentials using the Access key id and secret access key.
- Log in to Amazon ECR: This step uses aws-actions/amazon-ecr-login@v1 to login into the AWS ECR.
- Build, tag, and push image to Amazon ECR: This step builds the image with help of Dockerfile present in the root directory of the repository, tags, and pushes the image to ECR.
- Logout of Amazon ECR: This step simply logs out of the ECR once the image is pushed successfully into the ECR.
Checking the logs!
You can check the logs of your jobs under the Actions tab.
In Conclusion, In this Blog, we learned how to push the docker image to AWS ECR using GitHub Actions.