Deploy Django to cPanel via SSH #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GitHub Action for deploying the Django project to cPanel | |
| name: Deploy to cPanel | |
| on: | |
| # Run on pushes to the main branch | |
| push: | |
| branches: [ "main" ] | |
| # Allow running this workflow manually from the Actions tab | |
| workflow_dispatch: | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Checkout the repository code | |
| - name: Checkout Code | |
| uses: actions/checkout@v4 | |
| # Step 2: Use a dedicated action to set up the SSH agent | |
| # This is cleaner and more secure than manually creating the key file. | |
| - name: Setup SSH | |
| uses: webfactory/ssh-agent@v0.8.0 | |
| with: | |
| ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| # Step 3: Rsync code to the server | |
| # Your rsync command was already perfect! | |
| - name: Sync Files to Server | |
| run: | | |
| rsync -avz --delete \ | |
| -e "ssh -o StrictHostKeyChecking=no" \ | |
| --exclude=".git" \ | |
| --exclude=".github" \ | |
| --exclude="venv" \ | |
| --exclude="**/__pycache__" \ | |
| --exclude="*.pyc" \ | |
| ./ ${{ secrets.CPANEL_USER }}@${{ secrets.CPANEL_HOST }}:${{ secrets.REMOTE_APP_DIR }}/ | |
| # Step 4: Run remote commands (install, migrate, restart) | |
| # Your remote script is excellent, especially with 'set -e'. No changes needed here. | |
| - name: Run Remote Commands | |
| env: | |
| REMOTE_APP_DIR: ${{ secrets.REMOTE_APP_DIR }} | |
| REMOTE_VENV_ACTIVATE: ${{ secrets.REMOTE_VENV_ACTIVATE }} | |
| run: | | |
| ssh -o StrictHostKeyChecking=no ${{ secrets.CPANEL_USER }}@${{ secrets.CPANEL_HOST }} 'bash -s' <<'ENDSSH' | |
| # Exit immediately if a command exits with a non-zero status. | |
| set -e | |
| # Navigate to the application directory | |
| cd $REMOTE_APP_DIR | |
| # Activate the virtual environment | |
| source $REMOTE_VENV_ACTIVATE | |
| echo "--- Virtual environment activated ---" | |
| # Install/update dependencies | |
| echo "--- Installing Python dependencies ---" | |
| pip install -r requirements.txt | |
| # Run Django management commands | |
| echo "--- Running database migrations ---" | |
| python manage.py migrate --noinput | |
| echo "--- Collecting static files ---" | |
| python manage.py collectstatic --noinput | |
| # Restart the Phusion Passenger app | |
| echo "--- Restarting application ---" | |
| mkdir -p tmp | |
| touch tmp/restart.txt | |
| echo "--- Deployment successful! ---" | |
| ENDSSH |