Part 2: Automating Deployment with GitHub Actions and Docker
In Part 1 of our journey, we successfully set up a robust data pipeline between Paystack and Zoho CRM using Python scripts. Now, let's automate the deployment process using GitHub Actions and Docker to enhance our development workflow.
Setting Up GitHub Actions Workflow (workflow.yml
)
GitHub Actions allow us to automate tasks such as testing and deployment directly from our repository. Here’s how to set up a workflow to deploy our Python application to Amazon ECR (Elastic Container Registry).
Step-by-Step Guide:
- Create
workflow.yml
in Your Repository:
Create a .github/workflows/workflow.yml
file in your project repository with the following content:
name: Deploy to Amazon ECR
on:
push:
branches:
- main
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
IMAGE_TAG: ${{ github.sha }}
ECR_REPOSITORY: paystacktozoho # Update with your ECR repository name
jobs:
deploy:
name: Deploy to Amazon ECR
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- 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: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
Explanation:
- This workflow triggers on any push to the
main
branch. - It sets up environment variables for AWS Region, Image Tag (based on commit SHA), and ECR Repository name.
- The workflow uses AWS credentials stored in GitHub Secrets to authenticate with AWS.
- It logs into Amazon ECR, builds the Docker image using the
Dockerfile
, and pushes it to the specified ECR repository.
- Commit and Push Changes:
Commit the workflow.yml
file to your repository and push it to GitHub.
- Monitor Workflow Execution:
Go to the "Actions" tab in your GitHub repository to monitor the workflow execution. You should see the workflow running and deploying your application to Amazon ECR whenever you push changes to the main
branch.
Dockerfile Configuration (Dockerfile
)
The Dockerfile
specifies the environment and dependencies required to run your Python application in a Docker container.
Step-by-Step Guide:
- Create
Dockerfile
in Your Project Directory:
Create a Dockerfile
in the root of your project directory with the following content:
FROM python:3.8-slim-buster
WORKDIR /python-docker
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY .env .env
COPY paystack_zoho_pipeline.py paystack_zoho_pipeline.py
EXPOSE 5000
ENV FLASK_APP=paystack_zoho_pipeline.py
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
Explanation:
- This
Dockerfile
sets up a Python 3.8 environment with necessary dependencies installed. - Copies
requirements.txt
and installs Python dependencies. - Copies
.env
file for environment configuration. - Copies
paystack_zoho_pipeline.py
which contains your Flask application. - Exposes port
5000
for Flask to run. - Defines the command to start the Flask application when the container starts.
- Build and Test Locally:
Build the Docker image locally to ensure everything is set up correctly:
docker build -t paystack_zoho_app .
Run the Docker container locally to test your application:
docker run -p 5000:5000 paystack_zoho_app
Access your application at http://localhost:5000
to verify that it functions as expected.
Testing ngrok Webhook Setup for Paystack
During development, testing webhooks locally with ngrok ensures that your Paystack webhook integration works before deployment.
Step-by-Step Guide:
- Download and Install ngrok:
Download ngrok from ngrok.com and install it on your local machine.
- Start ngrok:
Start ngrok to expose your local server to the internet:
ngrok http 5000
This will provide you with a temporary public URL (https://abcd1234.ngrok.io
) that tunnels requests to your local Flask server running on port 5000
.
- Configure Paystack Webhook Endpoint:
- Log in to your Paystack dashboard.
- Navigate to Settings > Webhooks & API.
- Add the ngrok URL (
https://abcd1234.ngrok.io
) as the webhook endpoint URL. - Ensure that webhook events sent from Paystack reach your local Flask server via ngrok.
Conclusion
You have now automated the deployment of your Paystack-Zoho CRM pipeline using GitHub Actions and Docker. This setup allows you to efficiently manage and deploy your application, ensuring seamless integration and scalability. By testing your ngrok setup with Paystack, you verify that webhook events are correctly received before moving to production. Stay tuned for more cloud automation insights as we continue our 100 Days of Cloud journey! Happy coding and cloud exploring!
Keep Clouding! ☁️
Top comments (0)