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" GCS_BUCKET: gs://tomi_science_frontend NEXT_PUBLIC_BACKEND_URL: https://api.tomi.science 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} # Build frontend - name: Build frontend run: cd frontend && npm ci && npm run build && npm run export # 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} # Deploy frontend to GCS - name: Deploy frontend to GCS run: |- cd frontend && gsutil -h "Cache-Control: no-store, max-age=0" cp -r out/* ${{ env.GCS_BUCKET }}