name: Build and Deploy to Dev Server
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# Job 1: Build and Push Docker Image
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image_tag: ${{ steps.meta.outputs.tags }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver: docker
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha,prefix=
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Job 2: Deploy to Dev Server (only on push, not PR)
deploy:
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy to Dev Server via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.DEV_SERVER_HOST }}
username: ${{ secrets.DEV_SERVER_USER }}
key: ${{ secrets.DEV_SERVER_SSH_KEY }}
port: ${{ secrets.DEV_SERVER_PORT }}
script: |
# Navigate to app directory
cd /home/devjc/lpdp-mcp || mkdir -p /home/devjc/lpdp-mcp && cd /home/devjc/lpdp-mcp
# Login to GitHub Container Registry
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# Pull latest image
docker pull ghcr.io/${{ github.repository }}:latest
# Create .env file if not exists
cat > .env << 'ENVEOF'
GOOGLE_API_KEY=${{ secrets.GOOGLE_API_KEY }}
PINECONE_API_KEY=${{ secrets.PINECONE_API_KEY }}
PINECONE_INDEX_NAME=lpdp-pencairan
ENVEOF
# Create docker-compose.yml
cat > docker-compose.yml << 'COMPOSEEOF'
version: '3.8'
services:
lpdp-mcp:
image: ghcr.io/${{ github.repository }}:latest
container_name: lpdp-mcp-server
env_file:
- .env
restart: unless-stopped
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
COMPOSEEOF
# Stop existing container (if any)
docker-compose down || true
# Start new container
docker-compose up -d
# Cleanup old images
docker image prune -f
# Show status
docker ps | grep lpdp-mcp
- name: Deployment Status
if: success()
run: |
echo "✅ Deployment successful!"
echo "Image: ghcr.io/${{ github.repository }}:latest"
echo "Server: ${{ secrets.DEV_SERVER_HOST }}:${{ secrets.DEV_SERVER_PORT }}"
- name: Notify on Failure
if: failure()
run: |
echo "❌ Deployment failed!"
echo "Please check the logs above for details."