This project contains the backend code for the News Backend application. The backend provides essential API endpoints for managing and serving news-related data. It is hosted on a Raspberry Pi, with Docker-based deployment to ensure consistency and reliability.
/home/backend/
├── Dockerfile # Docker image configuration
├── docker-compose.yml # Compose file to manage multi-container setup
├── .env.local # Environment variables for the application
├── src/ # Source code for the backend
└── README.md # Project documentation
- Host: Raspberry Pi (running Linux-based OS)
- Proxy Manager: Traefik
- Containerization: Docker and Docker Compose
The CI/CD process automates the deployment of the backend from GitHub to the Raspberry Pi. Here's an overview of the steps involved:
The pipeline is triggered whenever a commit is pushed to the main
branch of the repository.
-
Code Checkout:
- The workflow checks out the latest code from the
main
branch.
- The workflow checks out the latest code from the
-
Docker Build and Push:
- Builds a Docker image locally.
- Tags the image as
your-dockerhub-username/your-image-name:latest
. - Pushes the Docker image to Docker Hub.
-
Raspberry Pi Deployment:
- Uses
sshpass
to securely connect to the Raspberry Pi via SSH. - Verifies if the project directory (
/home/<your-username>/backend/test
) exists. If not, it clones the Git repository. - Creates a
.env.local
file containing environment variables, such asDATABASE_URL
. - Pulls the latest Docker image from Docker Hub.
- Runs the container using
docker-compose
.
- Uses
- Install Docker and Docker Compose.
- Configure Traefik for reverse proxy management.
- Ensure SSH access is enabled on the Raspberry Pi.
The following secrets must be configured in your GitHub repository:
DOCKER_USERNAME
: Docker Hub usernameDOCKER_PASSWORD
: Docker Hub passwordRASPBERRY_PI_HOST
: Raspberry Pi’s hostname or IP addressRASPBERRY_PI_USER
: Username for Raspberry Pi loginRASPBERRY_PI_PASSWORD
: Password for Raspberry Pi loginDATABASE_URL
: Database connection string for the backend
The CI/CD pipeline is defined in the .github/workflows/main.yml
file.
Throughout the process of setting up the CI/CD pipeline for deploying the backend API to a Raspberry Pi, I gained valuable knowledge and experience in the following areas:
-
Automating Backend Deployment: Leveraged GitHub Actions and Docker to automate the entire deployment process, from code changes to production, streamlining and simplifying the workflow.
-
Docker and Docker Compose Expertise: Gained hands-on experience with Docker, including building images and managing containers with Docker Compose, ensuring consistent behavior across both local and production environments.
-
Automating Secure Deployment: Automated remote deployment processes by integrating SSH and
sshpass
, allowing for seamless code updates and deployment to the Raspberry Pi without manual intervention. -
Secure Environment Management: Learned to manage environment variables securely using GitHub Secrets to handle sensitive information, such as database credentials, effectively within the pipeline.
-
Enhanced Debugging Skills: Improved troubleshooting skills by resolving issues related to SSH connections, Git operations, and Docker deployments, utilizing tools such as Docker logs and network configuration analysis.
-
Scheduled News Updates: Set up a cron job to fetch and update fresh news three times daily, ensuring up-to-date content availability.
- Push changes to the
main
branch of your GitHub repository. - The CI/CD pipeline will automatically:
- Build and push a Docker image.
- Deploy the updated backend to your Raspberry Pi.
- SSH Errors: Ensure the Raspberry Pi username, password, and IP address are correct.
- Git Clone Issues: Verify the repository URL and network connectivity.
- Docker Issues: Check Docker logs on the Raspberry Pi for detailed error messages.