name: Deploy
on:
  repository_dispatch:
    types: [deploy-cmd]
jobs:
  blog:
    if: github.event.client_payload.event_name == 'blog'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          target_url: https://${{ github.event.client_payload.url_prefix }}blog.sentrei.com
          environment: "Blog ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - working-directory: packages/blog
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}blog.sentrei.com
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}blog.sentrei.com
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/blog'],
            })
  dartdoc:
    if: github.event.client_payload.event_name == 'dartdoc'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          target_url: https://${{ github.event.client_payload.url_prefix }}dartdoc.sentrei.com
          environment: "Dartdoc ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - uses: subosito/flutter-action@v1
        with:
          channel: ${{ env.flutter_channel }}
          flutter-version: ${{ env.flutter_version }}
      - working-directory: packages/mobile
        run: |
          flutter pub global activate dartdoc
          flutter pub global run dartdoc:dartdoc --exclude 'dart:async,dart:collection,dart:convert,dart:core,dart:developer,dart:io,dart:isolate,dart:math,dart:typed_data,dart:ui,dart:ffi,dart:html,dart:js,dart:js_util'
      - working-directory: packages/mobile
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}dartdoc.sentrei.com
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}dartdoc.sentrei.com
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/design'],
            })
  design:
    if: github.event.client_payload.event_name == 'design'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          target_url: https://${{ github.event.client_payload.url_prefix }}design.sentrei.com
          environment: "Design ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - working-directory: packages/design
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}design.sentrei.com
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}design.sentrei.com
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/design'],
            })
  fastlane:
    if: github.event.client_payload.event_name == 'fastlane'
    runs-on: macos-latest
    env:
      flutter_channel: "stable"
      flutter_version: "1.12.13+hotfix.9"
    strategy:
      fail-fast: false
      matrix:
        platform: [android, ios]
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          environment: "Fastlane ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }} ${{ matrix.platform }}"
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-ruby@v1.1.1
        with:
          ruby-version: "2.x"
      - uses: actions/setup-java@v1
        with:
          java-version: "12.x"
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - uses: subosito/flutter-action@v1
        with:
          channel: ${{ env.flutter_channel }}
          flutter-version: ${{ env.flutter_version }}
      - working-directory: packages/mobile
        run: |
          ./brew.sh
      - run: |
          gem install bundler
      - working-directory: packages/mobile/${{ matrix.platform }}
        run: |
          bundle update --bundler
          bundle config path ./vendor/bundle
          bundle install --jobs 4 --retry 3
      - run: |
          npm install -g firebase-tools
      - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
      - working-directory: packages/mobile
        run: |
          flutter pub get
      - id: version
        run: |
          version=$(node -pe "require('./package.json').version")
          echo "##[set-output name=version;]$version"
      - id: get_url
        run: |
          api_url="https://api.github.com/repos/$GITHUB_REPOSITORY/releases"
          release_id="$(curl -H "Authorization: token $GITHUB_TOKEN" "$api_url/tags/v$version" | jq -r '.id | select(. != null)')"
          upload_url="https://uploads.github.com/repos/$GITHUB_REPOSITORY/releases/$release_id/assets{?name,label}"
          echo "##[set-output name=upload_url;]$upload_url"
        env:
          version: ${{ steps.version.outputs.version }}
          GITHUB_REPOSITORY: $GITHUB_REPOSITORY
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - id: secret
        working-directory: packages/mobile/${{ matrix.platform }}
        run: |
          ./key.sh
        env:
          APP_SIGN_PWD: ${{ secrets.APP_SIGN_PWD }}
          BRANCH: ${{ github.event.client_payload.base }}
          GITHUB_ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
          SECRET_REPO: ${{ secrets.SECRET_REPO }}
      - if: matrix.platform == 'android'
        working-directory: packages/mobile
        run: |
          flutter build apk --flavor ${{ github.event.client_payload.base }} --target-platform android-arm,android-arm64,android-x64 --split-per-abi
          flutter build appbundle --flavor ${{ github.event.client_payload.base }} --target-platform android-arm,android-arm64,android-x64
      - if: matrix.platform == 'ios'
        working-directory: packages/mobile
        run: |
          flutter build ios --no-codesign --release --flavor ${{ github.event.client_payload.base }}
      - if: github.event.client_payload.environment == 'production'
        working-directory: packages/mobile/${{ matrix.platform }}
        run: |
          bundle exec fastlane ${{ github.event.client_payload.base }} release:${{ github.event.client_payload.release }}
        env:
          FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD: ${{ secrets.FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD }}
          FASTLANE_PASSWORD: ${{ secrets.FASTLANE_PASSWORD }}
          FASTLANE_SESSION: ${{ steps.secret.outputs.secret }}
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
          GITHUB_RUN_NUMBER: $GITHUB_RUN_NUMBER
          GITHUB_ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
          SECRET_REPO: ${{ secrets.SECRET_REPO }}
      - if: matrix.platform == 'android' && github.event.client_payload.environment == 'staging'
        working-directory: packages/mobile/${{ matrix.platform }}
        run: |
          bundle exec fastlane test ${{ github.event.client_payload.base }}:true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.client_payload.pull_request_number }}
          SECRET_REPO: ${{ secrets.SECRET_REPO }}
      - if: matrix.platform == 'android' && github.event.client_payload.environment == 'production' && github.event.client_payload.release == 'false'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.get_url.outputs.upload_url }}
          asset_path: packages/mobile/build/app/outputs/apk/${{ github.event.client_payload.base }}/release/app-${{ github.event.client_payload.base }}-release.apk
          asset_name: ${{ github.event.client_payload.base }}.apk
          asset_content_type: application/vnd.android.package-archive
      - if: matrix.platform == 'android' && github.event.client_payload.environment == 'production' && github.event.client_payload.release == 'false'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.get_url.outputs.upload_url }}
          asset_path: packages/mobile/build/app/outputs/bundle/${{ github.event.client_payload.base }}Release/app-${{ github.event.client_payload.base }}-release.aab
          asset_name: ${{ github.event.client_payload.base }}.aab
          asset_content_type: application/vnd.android.package-archive
      - if: matrix.platform == 'ios' && github.event.client_payload.environment == 'production' && github.event.client_payload.release == 'false'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.get_url.outputs.upload_url }}
          asset_path: packages/mobile/ios/build/Runner/${{ github.event.client_payload.base }}.ipa
          asset_name: ${{ github.event.client_payload.base }}.ipa
          asset_content_type: application/octet-stream
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/fastlane'],
            })
  functions:
    if: github.event.client_payload.event_name == 'functions'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          environment: "Functions ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - run: |
          yarn run typescript
      - id: secrets
        uses: GoogleCloudPlatform/github-actions/get-secretmanager-secrets@master
        with:
          credentials: ${{ secrets.GCP_CREDENTIALS }}
          secrets: |-
            FUNCTIONS_ENV:sentrei-master/functions_${{ github.event.client_payload.base }}_env
      - working-directory: packages/functions
        run: |
          echo '::add-mask::${{ steps.secrets.outputs.FUNCTIONS_ENV }}'
          echo '${{ steps.secrets.outputs.FUNCTIONS_ENV }}' > env.json
      - working-directory: packages/functions
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/functions'],
            })
  pioneer:
    if: github.event.client_payload.event_name == 'pioneer'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          target_url: https://${{ github.event.client_payload.url_prefix }}pioneer.sentrei.com
          environment: "Pioneer ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - working-directory: packages/pioneer
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}pioneer.sentrei.com
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}pioneer.sentrei.com
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/pioneer'],
            })
  typedoc:
    if: github.event.client_payload.event_name == 'typedoc'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          target_url: https://${{ github.event.client_payload.url_prefix }}typedoc.sentrei.com
          environment: "Typedoc ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - working-directory: packages/typedoc
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}typedoc.sentrei.com
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}typedoc.sentrei.com
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/typedoc'],
            })
  ui:
    if: github.event.client_payload.event_name == 'ui'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          target_url: https://${{ github.event.client_payload.url_prefix }}ui.sentrei.com
          environment: "UI ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - run: |
          yarn run typescript
      - working-directory: packages/ui
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}ui.sentrei.com
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}ui.sentrei.com
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/ui'],
            })
  web:
    if: github.event.client_payload.event_name == 'web'
    runs-on: ubuntu-latest
    steps:
      - uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          ref: ${{ github.event.client_payload.branch }}
          target_url: https://${{ github.event.client_payload.url_prefix }}sentrei.com
          environment: "Web ${{ github.event.client_payload.environment }} ${{ github.event.client_payload.branch }}"
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.branch }}
      - uses: actions/setup-node@v2.1.0
        with:
          node-version: "14.4.0"
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - run: |
          yarn run typescript
      - working-directory: packages/web
        run: |
          yarn run ${{ github.event.client_payload.task }}
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
      - id: secrets
        uses: GoogleCloudPlatform/github-actions/get-secretmanager-secrets@master
        with:
          credentials: ${{ secrets.GCP_CREDENTIALS }}
          secrets: |-
            SERVICE_ACCOUNT_EMAIL:sentrei-master/github_${{ github.event.client_payload.base }}_email
            SERVICE_ACCOUNT_KEY:sentrei-master/github_${{ github.event.client_payload.base }}_key
            WEB_ENV:sentrei-master/web_${{ github.event.client_payload.base }}_env
      - id: version
        run: |
          version=$(node -pe "require('./package.json').version")
          echo "::set-output name=release::sentrei@${version}"
          echo "::set-output name=version::${version}"
      - working-directory: packages/web
        run: |
          echo '::add-mask::${{ steps.secrets.outputs.WEB_ENV }}'
          echo '${{ steps.secrets.outputs.WEB_ENV }}' > .env
      - working-directory: packages/web
        run: |
          ./env.sh
        env:
          SENTRY_ENVIRONMENT: ${{ github.event.client_payload.base }}
          SENTRY_RELEASE: ${{ steps.version.outputs.release }}
          SENTREI_VERSION: ${{ steps.version.outputs.version }}
      - working-directory: packages/web
        run: |
          yarn run build
      - uses: docker/build-push-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          repository: ${{ github.repository }}
          tags: ${{ github.event.client_payload.tag }}
          cache_froms: ${{ github.repository }}:${{ github.event.client_payload.tag }}
          dockerfile: packages/web/Dockerfile
          path: "."
          always_pull: true
          add_git_labels: true
          tag_with_ref: true
          tag_with_sha: true
          push: true
      - uses: docker/build-push-action@v1
        with:
          username: _json_key
          password: ${{ steps.secrets.outputs.SERVICE_ACCOUNT_KEY }}
          registry: gcr.io
          repository: sentrei-${{ github.event.client_payload.base }}/sentrei
          tags: ${{ github.event.client_payload.tag }}
          dockerfile: packages/web/Dockerfile
          path: "."
          always_pull: false
          add_git_labels: true
          tag_with_ref: false
          tag_with_sha: false
          push: true
      - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
        with:
          version: "286.0.0"
          service_account_email: ${{ steps.secrets.outputs.SERVICE_ACCOUNT_EMAIL }}
          service_account_key: ${{ steps.secrets.outputs.SERVICE_ACCOUNT_KEY }}
          project_id: "sentrei-${{ github.event.client_payload.base }}"
      - run: |
          gcloud run deploy "sentrei-${{ github.event.client_payload.tag }}-web" \
            --image "gcr.io/sentrei-${{ github.event.client_payload.base }}/sentrei:${{ github.event.client_payload.tag }}" \
            --platform "managed" \
            --region "us-central1" \
            --allow-unauthenticated \
            --quiet
      - if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "success"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}sentrei.com
      - if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          state: "failure"
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
          target_url: https://${{ github.event.client_payload.url_prefix }}sentrei.com
      - if: github.event.client_payload.environment == 'staging'
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          comment-id: ${{ github.event.client_payload.comment_id }}
          reaction-type: hooray
      - if: github.event.client_payload.environment == 'staging'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.removeLabel({
              issue_number: ${{ github.event.client_payload.pull_request_number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: ['packages/web'],
            })
