aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/tomi.science.yml
blob: c8f419d1d18b76c5bd78d839366568f4d2579e6f (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
name: Deploy website to GCP

on:
  push:
    tags:
      - "*.*_*"

env:
  IMAGE: eu.gcr.io/jan-systems/tomi-science
  GCP_PROJECT: jan-systems
  GCR_SERVICE: tomi-science-backend
  GCR_PORT: 4000
  GCR_PLATFORM: managed
  GCR_REGION: europe-north1
  GCR_VPC_CONNECTOR: connector1
  # Env vars comma separated
  GCR_ENV_VARS: "^&^FRONTEND_URL=https://tomi.science\
                   &POSTGRES_CONNECTION_STRING=postgresql://tomi_science_user:${{ secrets.DATABASE_PASSWORD }}@10.24.240.3/tomi_science\
                   &NEXT_PUBLIC_BACKEND_URL=https://api.tomi.science"
  GCS_BUCKET: gs://tomi_science_frontend

jobs:
  website:
    name: Build, Push, and Deploy website
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://tomi.science

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      # Extract Git tag from the ref
      - name: Define image tag
        run: echo "TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - name: Define full image identifier
        run: echo "IMAGE_AND_TAG=${IMAGE}:${TAG}" >> $GITHUB_ENV

      # Define revision suffix
      - name: Define revision suffix
        run: echo "GCR_REVISION_SUFFIX=$(echo ${TAG} | sed 's/\./-/g' | sed 's/_/-rev/g')" >> $GITHUB_ENV

      # Setup gcloud CLI
      - name: Set up gcloud CLI
        uses: google-github-actions/setup-gcloud@v0.2.0
        with:
          service_account_key: ${{ secrets.GCP_SA_KEY }}
          project_id: ${{ env.GCP_PROJECT }}
          export_default_credentials: true

      # Configure Docker to use the gcloud command-line tool as a credential
      # helper for authentication
      - name: Configure Docker authentication
        run: gcloud --quiet auth configure-docker

      # In this step, this action saves a list of existing images,
      # the cache is created without them in the post run.
      # It also restores the cache if it exists.
      - name: Restore Docker cache
        uses: satackey/action-docker-layer-caching@v0.0.11
        # Ignore the failure of a step and avoid terminating the job.
        continue-on-error: true
        with:
          key: tomi-science-docker-cache-{hash}
          restore-keys: |
            tomi-science-docker-cache-

      # Build the backend Docker image
      - name: Build backend image
        run: docker build -t ${IMAGE_AND_TAG} backend

      # Push the backend Docker image to Google Container Registry
      - name: Push backend image to registry
        run: docker push ${IMAGE_AND_TAG}

      # Deploy backend to Google Cloud Run
      - name: Deploy backend to Cloud Run
        run: |-
          gcloud run deploy ${GCR_SERVICE} \
            --region=${GCR_REGION} \
            --platform=${GCR_PLATFORM} \
            --port=${GCR_PORT} \
            --revision-suffix=${GCR_REVISION_SUFFIX} \
            --vpc-connector=${GCR_VPC_CONNECTOR} \
            --set-env-vars=${GCR_ENV_VARS} \
            --image=${IMAGE_AND_TAG}

      # Build frontend
      - name: Build frontend
        run: cd frontend && npm run build && npm run export

      # Deploy frontend to GCS
      - name: Deploy frontend to GCS
        run: |-
          gsutil -h "Cache-Control: no-store, max-age=0" cp -r out/* ${{ env.GCS_BUCKET }}