This repository demonstrates how to automatically build and push Docker containers to AWS ECS for each subdirectory in the repository using GitHub Actions.
The repository should be structured as follows:
my-repo/
├── .github/
│ └── workflows/
│ └── build-and-push-docker.yml
├── pipeline1/
│ ├── handler.py
│ └── requirements.txt
├── pipeline2/
│ ├── handler.py
│ └── requirements.txt
└── pipeline3/
├── handler.py
└── requirements.txt
- .github/workflows/build-and-push-docker.yml: The GitHub Actions workflow file.
- pipeline1, pipeline2, pipeline3: Example services. Each service contains a
handler.pyfile and arequirements.txtfile.
- AWS Account: You need an AWS account and an ECR repository set up.
- GitHub Secrets: Configure the following secrets in your GitHub repository settings:
AWS_ACCOUNT_ID: Your AWS account ID.AWS_ACCESS_KEY_ID: Your AWS access key ID.AWS_SECRET_ACCESS_KEY: Your AWS secret access key.
-
Clone the Repository:
git clone https://github.com/your-username/your-repo.git cd your-repo -
Create the GitHub Actions Workflow:
- Create a
.github/workflowsdirectory if it doesn't exist. - Create a file named
build-and-push-docker.ymlinside.github/workflows/with the following content:
name: Build and Push Docker Containers on: push: paths: - "**/handler.py" - "**/requirements.txt" jobs: build-and-push: runs-on: ubuntu-latest env: AWS_REGION: us-east-1 ECR_REPOSITORY: your-ecr-repository # Replace with your ECR repository AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} steps: - name: Checkout code uses: actions/checkout@v2 - name: Configure AWS credentials 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: $AWS_REGION - name: Build and push Docker images run: | for dir in $(find . -type d -maxdepth 1 -mindepth 1); do if [[ -f "$dir/handler.py" && -f "$dir/requirements.txt" ]]; then foldername=$(basename $dir) index_id=$(git rev-parse --short HEAD) cat > $dir/Dockerfile <<EOF FROM python:3.10-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python", "handler.py"] EOF docker build -t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPOSITORY:$foldername-$index_id:latest $dir docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPOSITORY:$foldername-$index_id:latest fi done - name: Log out from Docker Hub run: docker logout
- Create a
-
Add Your Service Directories:
-
Create directories for each service (e.g.,
service1,service2, etc.). -
Each directory should contain:
handler.py: Your Python code.requirements.txt: Your Python dependencies.
Example
handler.py:def main(): print("Hello from service1!") if __name__ == "__main__": main()
Example
requirements.txt:# List your Python dependencies here requests
-
-
Commit and Push:
git add . git commit -m "Set up Docker CI/CD workflow" git push origin main
- The GitHub Actions workflow is triggered on changes to any
handler.pyorrequirements.txtfile. - For each subdirectory containing a
handler.pyandrequirements.txt, a Docker image is built and pushed to your AWS ECR repository. - The Docker images are tagged with the folder name and the latest commit hash.
- Ensure your AWS ECR repository exists and you have the correct permissions to push images.
- Adjust the
AWS_REGIONandECR_REPOSITORYin the workflow file as needed.
That's it! Your services will now be automatically built and pushed to AWS ECS whenever there are changes.