Deployment Automation with GitHub Action
π Automating Deployment with GitHub Actions: A Complete Guideπͺπ¬
In this blog, I’ll Walk you through setting up GitHub Actions to automate deployment for a full-stack application. We’ll cover how to upload frontend, backend, and APK files to a remote server and restart services—completely hands-free after pushing code to the main branch.
π§ 1. Prerequisites Before diving in, make sure you have:
A remote Linux server (e.g., Ubuntu on AWS)
deployuser withe right set of permissions for use of commands like rm, zip, unzip, mv, cp, etc.
Nginx or Apache configured
SSH access set up using an SSH key
A GitHub repository with:
frontend build zipped
backend JAR file
admin panel build zipped
Optional: APK file for mobile apps
⚙️ 2. Directory Structure on the Server/usr/share/nginx/
├── xyz.com/
│ ├── admin/
│ ├── app/
│ └── [static frontend homepage files]
└── abcd.in/
π 3. GitHub Secrets Configuration In your repository, go to Settings > Secrets and Variables > Actions and add:
Name Description
SERVER_IP Public IP of your server
DEPLOY_USERSSH user (e.g., deployuser)
SSH_PRIVATE_KEY Your SSH private key
π 4. GitHub Actions Workflow (.github/workflows/deploy.yml)
name: Auto Deploy xyz to Server
on:
push:
paths:
- 'deploy/**'
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- name: Upload frontend zip
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "deploy/xyz.zip"
target: "/tmp/deploy/"
- name: Upload admin panel zip
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "deploy/xyzdmin.zip"
target: "/tmp/deploy/"
- name: Upload backend JAR
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "deploy/xyz-0.0.1-SNAPSHOT.jar"
target: "/tmp/deploy/"
- name: Upload APK
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "app/xyz.apk"
target: "/tmp/deploy/"
overwrite: true
- name: SSH Deploy to Server
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
set -ex
ZIP_PATH="/tmp/deploy/xyz.zip"
ADMIN_ZIP_PATH="/tmp/deploy/xyzadmin.zip"
JAR_PATH="/tmp/deploy/xyz-0.0.1-SNAPSHOT.jar"
APK_PATH="/tmp/deploy/xyz.apk"
BUILD_DIR="/tmp/deploy/tmpbuild"
echo "Cleaning frontend..."
sudo rm -rf /usr/share/nginx/xyz.com/static
sudo rm -f /usr/share/nginx/xyz.com/*.json
sudo rm -f /usr/share/nginx/xyz.com/*.html
sudo rm -f /usr/share/nginx/xyz.com/*.png
echo "Deploying frontend..."
sudo unzip -o "$ZIP_PATH" -d "$BUILD_DIR"
sudo cp -r "$BUILD_DIR/build/"* /usr/share/nginx/xyz.com/
sudo rm -rf "$BUILD_DIR"
echo "Cleaning admin panel..."
sudo rm -rf /usr/share/nginx/xyz.com/admin/*
echo "Deploying admin panel..."
sudo unzip -o "$ADMIN_ZIP_PATH" -d "$BUILD_DIR"
sudo cp -r "$BUILD_DIR/build/"* /usr/share/nginx/xyz.com/admin/
sudo rm -rf "$BUILD_DIR"
echo "Updating APK..."
sudo cp "$APK_PATH" /usr/share/nginx/xyz.com/app/xyz.apk
echo "Deploying backend..."
sudo mv "$JAR_PATH" /backend/xyz-0.0.1-SNAPSHOT.jar
sudo systemctl restart xyz.service
echo "Cleaning up..."
sudo rm -f "$ZIP_PATH" "$ADMIN_ZIP_PATH" "$JAR_PATH" "$APK_PATH"
✅ 5. How It Works Whenever you push a change to the main branch inside the deploy/ directory, GitHub will:
Upload zipped frontend, admin, APK, and JAR files to the server
Extract and move them into correct locations
Restart your backend service
Clean up the temp files
πͺ 6. Testing the Workflow
To test the workflow:
touch deploy/trigger.txt
git add .
git commit -m "Trigger deploy"
git pushThen check the Actions tab in GitHub for deployment logs.
✍️ Conclusion
With GitHub Actions, you can fully automate deployment and eliminate manual steps. It’s scalable, auditable, and integrates tightly with your development flow.Now, your team can focus on building features while GitHub Actions handles deployments in the background.
Comments
Post a Comment