134 lines
4.4 KiB
YAML
134 lines
4.4 KiB
YAML
name: Build and Deploy
|
|
|
|
on:
|
|
push:
|
|
branches: [master]
|
|
|
|
jobs:
|
|
build-and-deploy:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v3
|
|
|
|
- name: Install Docker CLI
|
|
run: |
|
|
apt-get update
|
|
apt-get install -y ca-certificates curl gnupg
|
|
install -m 0755 -d /etc/apt/keyrings
|
|
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
|
chmod a+r /etc/apt/keyrings/docker.gpg
|
|
|
|
echo \
|
|
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
|
|
bullseye stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
|
|
|
|
apt-get update
|
|
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v2
|
|
|
|
- name: Login to Harbor Registry
|
|
uses: docker/login-action@v2
|
|
with:
|
|
registry: harbor.galpodlipnik.com
|
|
username: ${{ secrets.HARBOR_USERNAME }}
|
|
password: ${{ secrets.HARBOR_PASSWORD }}
|
|
|
|
- name: Build and push Backend image
|
|
uses: docker/build-push-action@v4
|
|
with:
|
|
context: ./backend
|
|
push: true
|
|
tags: harbor.galpodlipnik.com/chat-app/backend:latest
|
|
|
|
- name: Build and push Frontend image
|
|
uses: docker/build-push-action@v4
|
|
with:
|
|
context: ./frontend
|
|
push: true
|
|
tags: harbor.galpodlipnik.com/chat-app/frontend:latest
|
|
|
|
- name: Create docker-compose.yml
|
|
run: |
|
|
cat > docker-compose.yml << 'EOL'
|
|
services:
|
|
backend:
|
|
image: harbor.galpodlipnik.com/chat-app/backend:latest
|
|
restart: always
|
|
# Instead of env_file, define the environment variables directly
|
|
environment:
|
|
- NODE_ENV=production
|
|
- PORT=3000
|
|
- CORS_ORIGIN=http://localhost:5173
|
|
- DATABASE_URL=${DATABASE_URL}
|
|
- JWT_SECRET=${JWT_SECRET}
|
|
networks:
|
|
- chat-network
|
|
healthcheck:
|
|
test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"]
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 5
|
|
|
|
frontend:
|
|
image: harbor.galpodlipnik.com/chat-app/frontend:latest
|
|
restart: always
|
|
ports:
|
|
- "5173:80"
|
|
depends_on:
|
|
backend:
|
|
condition: service_healthy
|
|
networks:
|
|
- chat-network
|
|
|
|
networks:
|
|
chat-network:
|
|
driver: bridge
|
|
EOL
|
|
|
|
- name: Deploy with SSH
|
|
uses: appleboy/ssh-action@master
|
|
with:
|
|
host: ${{ secrets.DEPLOY_HOST }}
|
|
username: ${{ secrets.DEPLOY_USERNAME }}
|
|
key: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
script_stop: true
|
|
script: |
|
|
mkdir -p ~/services/chat-app
|
|
cd ~/services/chat-app
|
|
|
|
- name: Copy docker-compose.yml
|
|
uses: appleboy/scp-action@master
|
|
with:
|
|
host: ${{ secrets.DEPLOY_HOST }}
|
|
username: ${{ secrets.DEPLOY_USERNAME }}
|
|
key: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
source: "docker-compose.yml"
|
|
target: "~/services/chat-app"
|
|
|
|
- name: Complete Deployment
|
|
uses: appleboy/ssh-action@master
|
|
with:
|
|
host: ${{ secrets.DEPLOY_HOST }}
|
|
username: ${{ secrets.DEPLOY_USERNAME }}
|
|
key: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
script: |
|
|
cd ~/services/chat-app
|
|
|
|
# Copy environment file
|
|
echo "${{ secrets.ENV_CONTENT }}" > .env
|
|
|
|
# Login to Harbor registry
|
|
echo "${{ secrets.HARBOR_PASSWORD }}" | docker login harbor.galpodlipnik.com -u "${{ secrets.HARBOR_USERNAME }}" --password-stdin
|
|
|
|
export DATABASE_URL=${{ secrets.DATABASE_URL }}
|
|
export JWT_SECRET=${{ secrets.JWT_SECRET }}
|
|
docker-compose up -d
|
|
|
|
# Pull latest images and deploy
|
|
docker compose pull
|
|
docker compose down
|
|
docker compose up -d |