Introduction
GitHub Actions is a powerful continuous integration and deployment (CI/CD) tool provided by GitHub. It allows developers to automate software workflows and build, test, and deploy code changes from within the GitHub platform.
AWS ECR (Amazon Elastic Container Registry) is a fully-managed Docker container registry service provided by Amazon Web Services (AWS). It allows developers to store, manage, and deploy Docker container images securely. Here are some key features and functionalities of AWS ECR
Docker image is a lightweight, standalone, and executable package that contains everything needed to run a software application, including the code, runtime environment, system tools, libraries, and settings. Docker images are built from a set of instructions called a Dockerfile, which defines the steps needed to create the image.
Prerequisites
- Required GitHub repository.
- Basic knowledge of YAML language.
- AWS account.
In this post, We will show you how to create GitHub action’s workflow to build and push docker image to AWS ECR using GitHub Action.
Step 1: Creating Dockerfile
We need to create simple Dockerfile
to build docker image and push on AWS ECR, Use the following command for the same.
To create Dockerfile.
nano Dockerfile
Paste the following set of commands.
FROM ubuntu:16.04
RUN apt-get update && apt-get upgrade -y
RUN apt-get install apache2 -y
RUN rm /var/www/html/index.html
RUN echo "Hello DevOpstricks" > /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Save and push in Git repository.
Step 2: Configure AWS Secret Variables
We need to the following secrets from the Docker hub that we will use the GitHub action’s secrets variable.
Collect the following Creds from AWS ECR creds.
- AWS_ECR_ACCESS_KEY_ID
- AWS_ECR_SECRET_ACCESS_KEY
- AWS_ECR_REGION_NAME
- AWS_ECR_REPO_NAME
Go to the Setting tab and click on Secrets and variables button on left side, and the click on Action button.
and Create and update the Docker hub Creds over their.
Step 3: Creating Build and Push Workflow
We are good to create build and push the Docker image to the AWS ECR using GitHub Action workflow.
Go to the Action tab.
Click on set up a workflow yourself button.
Now we need to the paste the following yaml configuration.
on:
push:
branches:
- main # Change this to your desired branch
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup AWS ECR Details
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ECR_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_ECR_SECRET_ACCESS_KEY }}
aws-region: ${{secrets.AWS_ECR_REGION_NAME}}
- name: Login to Amazon ECR
id: login-pf-aws-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build and push the tagged docker image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-pf-aws-ecr.outputs.registry }}
ECR_REPOSITORY: ${{secrets.AWS_ECR_REPO_NAME}}
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
Click on Commit Changes button.
Step 4: Validate Docker Image in AWS ECR
We need to go to the AWS Container repository to see the docker images.
Conclusion
We have successfully Build and push the Docker image to AWS ECR with GitHub Action , If you still have questions, please post them in the comments section below.