eletrotupi / tcc/ .github/workflows/deploy.ymlbcb065f3b6607260c5a5caeae1a32324b723b7d7
2.3 KB | 87 lines Raw
1
name: Deploy
2

3
on:
4
  push:
5
    branches:
6
      - master # goes for production now, will become staging later on
7
      - production
8
    paths:
9
      - 'api/**'
10
      - '.github/workflows/deploy.yml'
11

12
env:
13
  REGISTRY: ghcr.io
14
  IMAGE_NAME: ${{ github.repository }}
15

16
jobs:
17
  build-and-push:
18
    name: Build & push image
19
    runs-on: ubuntu-latest
20
    permissions:
21
      contents: read
22
      packages: write
23

24
    outputs:
25
      image_tag: ${{ steps.meta.outputs.version }}
26

27
    steps:
28
      - name: Checkout
29
        uses: actions/checkout@v4
30

31
      - name: Log in to GHCR
32
        uses: docker/login-action@v3
33
        with:
34
          registry: ${{ env.REGISTRY }}
35
          username: ${{ github.actor }}
36
          password: ${{ secrets.GHCR_TOKEN }}
37

38
      - name: Docker metadata
39
        id: meta
40
        uses: docker/metadata-action@v5
41
        with:
42
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
43
          tags: |
44
            # main branch => tag "latest"
45
            type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
46
            # production branch => tag "production"
47
            # type=raw,value=production,enable=${{ github.ref == 'refs/heads/production' }}
48
            # always tag with short SHA for traceability
49
            type=sha,prefix=,format=short
50

51
      - name: Build and push
52
        uses: docker/build-push-action@v5
53
        with:
54
          context: ./api
55
          file: ./api/Dockerfile.production
56
          push: true
57
          tags: ${{ steps.meta.outputs.tags }}
58
          labels: ${{ steps.meta.outputs.labels }}
59

60

61
  deploy-production:
62
    name: Deploy to production
63
    needs: build-and-push
64
    runs-on: ubuntu-latest
65
    if: github.ref == 'refs/heads/master'
66
    environment: production
67

68
    steps:
69
      - name: Deploy via SSH
70
        uses: appleboy/ssh-action@v1
71
        with:
72
          host: ${{ secrets.VPS_HOST }}
73
          username: ${{ secrets.VPS_USER }}
74
          key: ${{ secrets.VPS_SSH_KEY }}
75
          script: |
76
            set -e
77
            cd /srv/app/production
78

79
            docker pull ghcr.io/${{ github.repository }}:latest
80

81
            sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=latest/" .env
82

83
            docker compose up -d --no-deps --force-recreate api
84

85
            sleep 5
86
            # Dump the logs for a while
87
            docker compose logs --tail=50 api