This content originally appeared on DEV Community and was authored by Eddie Gulay
A Comprehensive Guide
Deploying web applications on shared hosting platforms like cPanel can be challenging, especially when trying to automate the process with CI/CD (Continuous Integration and Continuous Deployment). In this guide, we'll walk you through setting up a CI/CD pipeline using GitHub Actions to deploy your web application to a shared hosting environment via FTP.
Table of Contents
- Introduction to CI/CD and GitHub Actions
- Preparing Your Environment
- Creating the Deployment Script
- Setting Up GitHub Actions Workflow
- Troubleshooting and Optimization
- Conclusion
1. Introduction to CI/CD and GitHub Actions
Continuous Integration (CI) is a development practice where developers integrate code into a shared repository frequently, and Continuous Deployment (CD) automates the deployment of applications to production environments. GitHub Actions is a powerful tool that allows you to automate, customize, and execute your software development workflows right in your GitHub repository.
2. Preparing Your Environment
Before diving into the setup, ensure you have the following:
- A GitHub repository for your web application.
- FTP credentials for your cPanel hosting account.
- Basic knowledge of shell scripting and GitHub Actions.
3. Creating the Deployment Script
We'll create a script that uses lftp
to transfer files from your local repository to your cPanel server. This script ensures that only newer and updated files are uploaded, optimizing the deployment process.
Step-by-Step Deployment Script
-
Create the
deploy.sh
script in the root of your project repository:
#!/bin/bash # Exit immediately if a command exits with a non-zero status set -e # FTP server details FTP_HOST=$1 FTP_USERNAME=$2 FTP_PASSWORD=$3 LOCAL_DIR=$4 REMOTE_DIR=$5 # Print debug information echo "FTP_HOST: $FTP_HOST" echo "FTP_USERNAME: $FTP_USERNAME" echo "LOCAL_DIR: $LOCAL_DIR" echo "REMOTE_DIR: $REMOTE_DIR" # Use lftp to mirror the local directory to the remote directory, skipping SSL verification lftp -d -c " set ssl:verify-certificate no; open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_HOST; mirror -R --verbose --only-newer --parallel=10 $LOCAL_DIR $REMOTE_DIR; bye; "
This script:
- Connects to your FTP server.
- Mirrors the local directory to the remote directory.
- Skips SSL certificate verification (useful for self-signed certificates).
- Uploads only newer and updated files.
-
Make the script executable:
chmod +x deploy.sh
4. Setting Up GitHub Actions Workflow
Next, we'll create a GitHub Actions workflow to automate the deployment process whenever changes are pushed to the main branch.
GitHub Actions Workflow Configuration
-
Create the workflow file
.github/workflows/deploy.yml
in your repository:
name: Deploy on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up LFTP run: sudo apt-get install -y lftp - name: Deploy to FTP run: | chmod +x deploy.sh ./deploy.sh ${{ secrets.FTP_HOST }} ${{ secrets.FTP_USERNAME }} ${{ secrets.FTP_PASSWORD }} ./ /your.domain.com/src
This workflow:
- Runs on every push to the
main
branch. - Checks out the code.
- Installs
lftp
. - Executes the
deploy.sh
script with the FTP credentials and directories.
- Runs on every push to the
-
Store FTP Credentials as GitHub Secrets:
- Go to your GitHub repository.
- Navigate to Settings > Secrets and variables > Actions.
- Add the following secrets:
-
FTP_HOST
: Your FTP server’s hostname or IP address. -
FTP_USERNAME
: Your FTP username. -
FTP_PASSWORD
: Your FTP password.
-
5. Troubleshooting and Optimization
Debugging Long Running Jobs
If your workflow is stuck or taking too long, add debugging information to the script and check the workflow logs in GitHub Actions.
# Use lftp with debug flag
lftp -d -c "
set ssl:verify-certificate no;
open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_HOST;
mirror -R --verbose --only-newer --parallel=10 $LOCAL_DIR $REMOTE_DIR;
bye;
"
Common Issues and Solutions
- Network Issues: Ensure stable internet connectivity and correct FTP credentials.
- Server Overload: Check cPanel server resource usage (CPU, RAM, disk space).
- Retry Workflow: Sometimes, workflows can get stuck due to temporary issues. Retry the workflow from the Actions tab.
6. Conclusion
By setting up CI/CD with GitHub Actions and deploying to a shared hosting cPanel environment using FTP, you can automate your deployment process, ensuring that your web application is always up-to-date with the latest changes.
This guide provide a comprehensive guide though i found this approach not very reliable than using the actual FTP Softwares like filezila.
Thanks for reading, hope you got an idea 💡
This content originally appeared on DEV Community and was authored by Eddie Gulay
Eddie Gulay | Sciencx (2024-07-20T19:22:48+00:00) Setting Up CI/CD with GitHub Actions on Shared Hosting cPanel. Retrieved from https://www.scien.cx/2024/07/20/setting-up-ci-cd-with-github-actions-on-shared-hosting-cpanel/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.