fix(ci): update docker context #2
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
| name: Build and Deploy Todo API | |
| on: | |
| push: | |
| branches: [main] | |
| workflow_dispatch: | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Log in to GitHub Container Registry | |
| 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=sha,prefix={{branch}}- | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./multi_container_application | |
| file: ./multi_container_application/Dockerfile | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| deploy: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Deploy to remote server | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| # Create project directory | |
| mkdir -p ~/todo-app | |
| cd ~/todo-app | |
| # Login to GitHub Container Registry | |
| echo ${{ secrets.GITHUB_TOKEN }} | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin | |
| # Stop and remove existing containers | |
| docker-compose down || true | |
| # Pull latest image | |
| docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest | |
| # Create docker-compose.yml | |
| cat > docker-compose.yml << 'EOF' | |
| version: '3.8' | |
| services: | |
| mongodb: | |
| image: mongo:7-jammy | |
| container_name: todo-mongodb | |
| restart: unless-stopped | |
| environment: | |
| - MONGO_INITDB_DATABASE=todos | |
| volumes: | |
| - mongodb_data:/data/db | |
| networks: | |
| - todo-network | |
| healthcheck: | |
| test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"] | |
| interval: 10s | |
| timeout: 5s | |
| retries: 5 | |
| api: | |
| image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest | |
| container_name: todo-api | |
| restart: unless-stopped | |
| ports: | |
| - "3000:3000" | |
| environment: | |
| - PORT=3000 | |
| - MONGODB_URI=mongodb://mongodb:27017/todos | |
| depends_on: | |
| mongodb: | |
| condition: service_healthy | |
| networks: | |
| - todo-network | |
| nginx: | |
| image: nginx:alpine | |
| container_name: todo-nginx | |
| restart: unless-stopped | |
| ports: | |
| - "80:80" | |
| volumes: | |
| - ./nginx.conf:/etc/nginx/nginx.conf:ro | |
| depends_on: | |
| - api | |
| networks: | |
| - todo-network | |
| volumes: | |
| mongodb_data: | |
| driver: local | |
| networks: | |
| todo-network: | |
| driver: bridge | |
| EOF | |
| # Create nginx.conf | |
| cat > nginx.conf << 'EOF' | |
| events { | |
| worker_connections 1024; | |
| } | |
| http { | |
| upstream api { | |
| server api:3000; | |
| } | |
| server { | |
| listen 80; | |
| server_name _; | |
| client_max_body_size 10M; | |
| location / { | |
| proxy_pass http://api; | |
| proxy_http_version 1.1; | |
| proxy_set_header Upgrade $http_upgrade; | |
| proxy_set_header Connection 'upgrade'; | |
| proxy_set_header Host $host; | |
| proxy_set_header X-Real-IP $remote_addr; | |
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
| proxy_set_header X-Forwarded-Proto $scheme; | |
| proxy_cache_bypass $http_upgrade; | |
| } | |
| location /health { | |
| access_log off; | |
| return 200 "healthy\n"; | |
| add_header Content-Type text/plain; | |
| } | |
| } | |
| } | |
| EOF | |
| # Start containers | |
| docker-compose up -d | |
| # Clean up old images | |
| docker image prune -af |