Skip to content

Instantly share code, notes, and snippets.

@buddhdev-harsh
Last active October 7, 2023 16:07
Show Gist options
  • Save buddhdev-harsh/f41943f0bcfaff69536a21e5f0e886d6 to your computer and use it in GitHub Desktop.
Save buddhdev-harsh/f41943f0bcfaff69536a21e5f0e886d6 to your computer and use it in GitHub Desktop.
this gist is to create github action flow which will build multiple folders docker image if it detects any changes in the folders and push it to ECR.

This GitHub repository is designed to automate the process of building Docker images and pushing them to Amazon Elastic Container Registry (ECR) based on changes made to specific folders within your project. The primary goal is to optimize the workflow by triggering Docker image builds only when modifications occur in either folder_1 or folder_2.

The repository includes configuration files and scripts that facilitate this workflow, allowing you to maintain Docker images efficiently. Here's an overview of how it works:

Monitoring for Changes: The repository sets up a mechanism to constantly monitor folder_1 and folder_2 for any updates or changes.

Change Detection: When changes are detected in either folder_1 or folder_2, a trigger is initiated.

Docker Image Build: A script or automation process is employed to build Docker images corresponding to the changes made in the detected folders. This ensures that you only build images that are affected by the modifications.

ECR Push: After successfully building the Docker images, they are automatically pushed to Amazon Elastic Container Registry (ECR), making them available for use in your containerized applications.

D:.
├───.github
│   └───workflows
├───folder_1
│   └───another_folder
│   └───Dockerfile
└───folder_2
    └───another_folder_2
    └───Dockerfile

This repository aims to streamline your development workflow by reducing unnecessary Docker image builds and ensuring that your ECR repository stays up-to-date with the latest changes in folder_1 and folder_2. It's an efficient solution for managing Docker containers in a development environment where changes are frequent and need to be reflected in the container images.

name: push folder docker image to ECR
on:
push:
branches:
- 'main'
jobs:
changes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v2
id: changes
with:
filters: |
folder_1:
- 'folder_1/**'
folder_2:
- 'folder_2/**'
- name: check for changes
if: ${{ steps.changes.outputs.folder_1 == 'true' || steps.changes.outputs.folder_2 == 'true'}}
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1
- name: Login to Amazon ECR
if: ${{ steps.changes.outputs.folder_1 == 'true' || steps.changes.outputs.folder_2 == 'true'}}
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
if: ${{ steps.changes.outputs.folder_1 == 'true'}}
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: Name_of_ecr_repo
IMAGE_TAG: ImageTag
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG folder_1/.
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Build, tag, and push image to Amazon ECR
if: ${{ steps.changes.outputs.folder_2 == 'true'}}
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: Name_of_ecr_repo
IMAGE_TAG: ImageTag
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG folder_2/.
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment