|
| 1 | +name: Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + build-and-push: |
| 10 | + name: Build and Push Docker Image |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up Docker Buildx |
| 18 | + uses: docker/setup-buildx-action@v3 |
| 19 | + |
| 20 | + - name: Log in to GitHub Container Registry |
| 21 | + uses: docker/login-action@v3 |
| 22 | + with: |
| 23 | + registry: ghcr.io |
| 24 | + username: ${{ github.actor }} |
| 25 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + |
| 27 | + - name: Extract metadata |
| 28 | + id: meta |
| 29 | + uses: docker/metadata-action@v5 |
| 30 | + with: |
| 31 | + images: ghcr.io/${{ github.repository }} |
| 32 | + tags: | |
| 33 | + type=ref,event=branch |
| 34 | + type=sha,prefix={{branch}}- |
| 35 | + type=raw,value=latest,enable={{is_default_branch}} |
| 36 | +
|
| 37 | + - name: Build and push Docker image |
| 38 | + uses: docker/build-push-action@v5 |
| 39 | + with: |
| 40 | + context: . |
| 41 | + push: true |
| 42 | + tags: ${{ steps.meta.outputs.tags }} |
| 43 | + labels: ${{ steps.meta.outputs.labels }} |
| 44 | + cache-from: type=gha |
| 45 | + cache-to: type=gha,mode=max |
| 46 | + target: production |
| 47 | + |
| 48 | + deploy: |
| 49 | + name: Deploy to Server |
| 50 | + runs-on: ubuntu-latest |
| 51 | + needs: build-and-push |
| 52 | + environment: |
| 53 | + name: production |
| 54 | + url: https://${{ vars.DOMAIN }} |
| 55 | + |
| 56 | + steps: |
| 57 | + - name: Checkout code |
| 58 | + uses: actions/checkout@v4 |
| 59 | + |
| 60 | + - name: Deploy to server via SSH |
| 61 | + uses: appleboy/ssh-action@v1.0.0 |
| 62 | + with: |
| 63 | + host: ${{ secrets.DEPLOY_HOST }} |
| 64 | + username: ${{ secrets.DEPLOY_USER }} |
| 65 | + key: ${{ secrets.DEPLOY_SSH_KEY }} |
| 66 | + port: ${{ secrets.DEPLOY_PORT || 22 }} |
| 67 | + script: | |
| 68 | + set -e |
| 69 | +
|
| 70 | + # Navigate to application directory |
| 71 | + cd ${{ secrets.DEPLOY_PATH || '/opt/vicinae-store' }} |
| 72 | +
|
| 73 | + # Log in to GitHub Container Registry |
| 74 | + echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin |
| 75 | +
|
| 76 | + # Pull latest image |
| 77 | + docker-compose pull app |
| 78 | +
|
| 79 | + # Run migrations (if any) |
| 80 | + docker-compose run --rm app bun prisma migrate deploy || true |
| 81 | +
|
| 82 | + # Recreate and restart app container |
| 83 | + docker-compose up -d --force-recreate app |
| 84 | +
|
| 85 | + # Wait for health check |
| 86 | + echo "Waiting for application to be healthy..." |
| 87 | + sleep 10 |
| 88 | +
|
| 89 | + # Verify deployment |
| 90 | + docker-compose ps app |
| 91 | +
|
| 92 | + # Clean up old images |
| 93 | + docker image prune -f |
| 94 | +
|
| 95 | + echo "Deployment completed successfully!" |
| 96 | +
|
| 97 | + - name: Notify deployment status |
| 98 | + if: always() |
| 99 | + run: | |
| 100 | + if [ "${{ job.status }}" == "success" ]; then |
| 101 | + echo "✅ Deployment successful!" |
| 102 | + else |
| 103 | + echo "❌ Deployment failed!" |
| 104 | + exit 1 |
| 105 | + fi |
0 commit comments