crane-cloud/frontend

View on GitHub
.github/workflows/staging.yml

Summary

Maintainability
Test Coverage
name: Staging deployment

on:
  push:
    branches:
      - develop

jobs:
  build_and_deploy_staging:
    outputs:
      image: ${{ steps.export.outputs.image }}
      tag: ${{ steps.export.outputs.tag }}

    runs-on: ubuntu-latest
    env:
      USE_GKE_GCLOUD_AUTH_PLUGIN: true
      GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
      cluster: staging-cluster
      REACT_APP_API_BASE_URL: https://staging-api.cranecloud.io
      namespace: cranecloud
      CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}

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

      - name: Install (Buildx)
        uses: docker/setup-buildx-action@v1

      - name: Set up Docker Buildx
        run: |
          docker buildx create --use
          docker buildx version  # Print information to verify that Buildx is set up
        if: runner.os == 'Linux' # Only run on Linux runners

      - name: Install kubectl
        run: |
          curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
          chmod +x ./kubectl
          sudo mv ./kubectl /usr/local/bin/kubectl

      - name: Login (GCP)
        uses: google-github-actions/auth@v0
        with:
          credentials_json: ${{ secrets.GCLOUD_SERVICE_KEY }}

      - name: Get Kubernetes credentials
        run: |
          gcloud container clusters get-credentials $cluster --zone us-central1-a

      - name: Install (Gcloud)
        uses: google-github-actions/setup-gcloud@v1
        with:
          project_id: ${{ secrets.GCP_PROJECT_ID }}
          install_components: "gke-gcloud-auth-plugin"

      - name: Login (GCR)
        run: gcloud auth configure-docker

      - id: meta
        name: Tag
        uses: docker/metadata-action@v3
        with:
          flavor: |
            latest=true
          images: gcr.io/${{ secrets.GCP_PROJECT_ID }}/cranecloud-frontend
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=sha

      - name: Add Env vars
        env:
          REACT_APP_API_BASE_URL: ${{ secrets.REACT_APP_API_BASE_URL_STAGING }}
          REACT_APP_MIRA_API_URL: ${{ secrets.REACT_APP_MIRA_API_URL }}
          REACT_APP_FLUTTERWAVE_PUBLIC_KEY_TESTING: ${{ secrets.REACT_APP_FLUTTERWAVE_PUBLIC_KEY_TESTING }}
          REACT_APP_GITHUB_CLEINT_ID: ${{ secrets.REACT_APP_GITHUB_CLEINT_ID_STAGING }}
          REACT_APP_EXCHANGE_RATE_KEY: ${{ secrets.REACT_APP_EXCHANGE_RATE_KEY }}
          REACT_APP_DOCS_URL: ${{ secrets.REACT_APP_DOCS_URL_STAGING }}
          REACT_APP_DOCKER_EMAIL: ${{ secrets.REACT_APP_DOCKER_EMAIL }}
          REACT_APP_DOCKER_PASSWORD: ${{ secrets.REACT_APP_DOCKER_PASSWORD }}
          REACT_APP_MONITORING_APP: ${{ secrets.REACT_APP_MONITORING_APP_STAGING }}
        run: | 
          chmod +x ./.github/workflows/bin/create_envs.sh
          ./.github/workflows/bin/create_envs.sh

      - name: Build
        uses: docker/build-push-action@v2
        with:
          cache-from: type=gha
          cache-to: type=gha,mode=max
          context: .
          file: docker/prod/Dockerfile
          labels: ${{ steps.meta.outputs.labels }}
          push: true
          tags: ${{ steps.meta.outputs.tags }}

      - id: export
        name: Export
        uses: actions/github-script@v5
        with:
          script: |
            const metadata = JSON.parse(`${{ steps.meta.outputs.json }}`)
            const fullUrl = metadata.tags.find((t) => t.includes(':sha-'))
            if (fullUrl == null) {
              core.error('Unable to find sha tag of image')
            } else {
              const tag = fullUrl.split(':')[1]
              core.setOutput('image', fullUrl)
              core.setOutput('tag', tag)
            }

      - name: Update deployment image
        run: |
          kubectl set image deployment/cranecloud-frontend cranecloud-frontend=gcr.io/${{ secrets.GCP_PROJECT_ID }}/cranecloud-frontend:${{ steps.export.outputs.tag }} --record -n $namespace