aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/deploy.yml
blob: cf885026ffe992d1622648825501112d055ccffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
name: Deploy to production

on:
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  detect-changes:
    runs-on: ubuntu-latest
    name: Detect changed components
    permissions:
      contents: read
    outputs:
      frontend: ${{ steps.filter.outputs.frontend }}
      backend: ${{ steps.filter.outputs.backend }}
      stops-script: ${{ steps.filter.outputs.stops-script }}
    steps:
      - uses: actions/checkout@v6
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            frontend:
              - 'src/frontend/**'
            backend:
              - 'src/Enmarcha.Backend/**'
              - 'src/common/**'
            stops-script:
              - 'src/gtfs_perstop_report/**'

  build-frontend:
    runs-on: ubuntu-latest
    name: Build frontend artifact
    needs: detect-changes
    if: needs.detect-changes.outputs.frontend == 'true'
    environment: EnMarcha Prod
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: true
          lfs: false
      - uses: actions/setup-node@v6
        with:
          node-version: lts/*
          cache: "npm"
          cache-dependency-path: src/frontend/package-lock.json
      - name: Install frontend dependencies
        working-directory: src/frontend
        run: npm ci
      - name: Build frontend
        working-directory: src/frontend
        run: npm run build
      - name: Prepare artifact directory
        run: |
          rm -rf dist
          mkdir -p dist/frontend
          cp -R src/frontend/build/client/. dist/frontend/
      - name: Archive Production Artifact
        uses: actions/upload-artifact@v5
        with:
          name: production
          path: dist
          retention-days: 7

  build-backend:
    runs-on: ubuntu-latest
    name: Build backend artifact
    needs: detect-changes
    if: needs.detect-changes.outputs.backend == 'true'
    environment: EnMarcha Prod
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: true
          lfs: false
      - uses: actions/setup-dotnet@v5
        with:
          dotnet-version: "9.0.x"
      - name: Build backend
        run: dotnet publish -c Release -r linux-arm64 --self-contained false src/Enmarcha.Backend/Enmarcha.Backend.csproj -o dist/backend
      - name: Install dotnet-ef tool
        run: dotnet tool install --global dotnet-ef
      - name: Build EF migrations bundle
        run: dotnet ef migrations bundle --project src/Enmarcha.Backend/Enmarcha.Backend.csproj -r linux-arm64 --self-contained -o dist/backend/efbundle
      - name: Archive Backend Artifact
        uses: actions/upload-artifact@v5
        with:
          name: backend
          path: dist/backend
          retention-days: 7

  deploy:
    runs-on: ubuntu-latest
    needs: [detect-changes, build-frontend, build-backend]
    if: |
      always() &&
      needs.detect-changes.result == 'success' &&
      (needs.build-frontend.result == 'success' ||
       needs.build-backend.result == 'success')
    name: Deploy to production server
    environment: EnMarcha Prod
    steps:
      - name: Download Frontend Artifact
        if: needs.detect-changes.outputs.frontend == 'true'
        uses: actions/download-artifact@v6
        with:
          name: production
          path: dist
      - name: Download Backend Artifact
        if: needs.detect-changes.outputs.backend == 'true'
        uses: actions/download-artifact@v6
        with:
          name: backend
          path: dist/backend
      - name: Connect to tailnet
        uses: tailscale/github-action@v4
        with:
          oauth-client-id: ${{ secrets.TAILSCALE_CLIENT_ID }}
          oauth-secret: ${{ secrets.TAILSCALE_CLIENT_SECRET }}
          tags: tag:ci
      - name: Wait for reachability
        run: |
          until tailscale ping --until-direct=false ${{ secrets.TARGET_HOST }}; do
            echo "Waiting for Tailscale to connect..."
            sleep 2
          done
      - name: Add SSH Key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -H ${{ secrets.TARGET_HOST }} >> ~/.ssh/known_hosts

      - name: Deploy frontend
        if: needs.detect-changes.outputs.frontend == 'true'
        run: |
          rsync -avz dist/frontend/ ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }}:/var/www/enmarcha/

      - name: Stop service
        if: needs.detect-changes.outputs.backend == 'true'
        run: ssh ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }} "echo ${{ secrets.TARGET_PASSWORD }} | sudo -S /usr/bin/systemctl stop enmarcha"

      - name: Upload backend
        if: needs.detect-changes.outputs.backend == 'true'
        run: rsync -avz dist/backend/ ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }}:/opt/enmarcha/

      - name: Allow execution
        if: needs.detect-changes.outputs.backend == 'true'
        run: ssh ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }} "chmod +x /opt/enmarcha/Enmarcha.Backend /opt/enmarcha/efbundle"

      - name: Apply database migrations
        if: needs.detect-changes.outputs.backend == 'true'
        run: ssh ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }} "cd /opt/enmarcha && ASPNETCORE_ENVIRONMENT=Production ./efbundle"

      - name: Start service
        if: needs.detect-changes.outputs.backend == 'true'
        run: ssh ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }} "echo ${{ secrets.TARGET_PASSWORD }} | sudo -S /usr/bin/systemctl start enmarcha"