name: Publish Expo Web
on:
  workflow_dispatch: {}
  # pull_request:
  #   paths:
  #     - .github/workflows/web.yml
  #     - yarn.lock
  #     - apps/**
  #     - packages/**

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Get cache key of git lfs files
        id: git-lfs
        run: echo "::set-output name=sha256::$(git lfs ls-files | openssl dgst -sha256)"
      - uses: actions/cache@v2
        with:
          path: .git/lfs
          key: ${{ steps.git-lfs.outputs.sha256 }}
      - run: git lfs pull
      - uses: actions/setup-node@v2
        with:
          node-version: '14.17'
      - run: yarn global add expo-cli@3
      - name: Cache Node.js modules
        uses: actions/cache@v2
        with:
          path: ${{ github.workspace }}/node_modules
          key: ${{ runner.OS }}-node_modules-${{ hashFiles('yarn.lock') }}
      #  Setup Expo CLI action
      # - name: Setup Expo
      #   uses: expo/expo-github-action@v5
      #   with:
      #     expo-version: 3.x
      #     expo-username: ${{ secrets.EXPO_CLI_USERNAME }}
      #     expo-password: ${{ secrets.EXPO_CLI_PASSWORD }}
      #     expo-cache: true

      - run: yarn --frozen-lockfile

      # - name: Publish Expo app
      #   working-directory: ./apps/native-component-list
      #   run: expo publish --release-channel=pr-${{ github.event.number }}
      # # Get Expo link for the comment
      # - name: Get expo link
      #   id: expo
      #   run: echo "::set-output name=path::@community/native-component-list?release-channel=pr-${{ github.event.number }}"
      # Build the Expo website
      - name: Build Website
        working-directory: ./apps/native-component-list
        run: yarn build:web
      # Deploy Expo web to now
      - name: Publish Website
        if: ${{ secrets.EXPO_BOT_GITHUB_TOKEN }}
        working-directory: ./apps/native-component-list
        env:
          ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }}
          BUILD_ID: ${{ github.sha }}
        run: npx now deploy web-build --env NODE_ENV=production --confirm --public --no-clipboard --token=${ZEIT_TOKEN} > deploy-url.txt
      # Save variable
      - name: Set deploy url to env variable
        if: ${{ secrets.EXPO_BOT_GITHUB_TOKEN }}
        run: echo "ZEIT_DEPLOY=$(cat deploy-url.txt)" >> $GITHUB_ENV
      # Alias the now deployment URL
      - name: Alias with PR number
        if: ${{ secrets.EXPO_BOT_GITHUB_TOKEN }}
        working-directory: ./apps/native-component-list
        env:
          ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }}
          SITE_ALIAS: expo-web-${{ github.event.number }}.now.sh
        run: npx now alias --token=${ZEIT_TOKEN} `cat deploy-url.txt` $SITE_ALIAS
      # Send comment to the thread
      - name: Add comment with deploy url
        if: ${{ secrets.EXPO_BOT_GITHUB_TOKEN }}
        uses: unsplash/comment-on-pr@master
        env:
          GITHUB_TOKEN: ${{ secrets.EXPO_BOT_GITHUB_TOKEN }}
        with:
          msg: Native Component List for this branch is ready<br><a aria-label="Try the web preview of this PR" href="https://expo-web-${{ github.event.number }}.now.sh/"><img longdesc="Try the web preview of this PR" src="https://img.shields.io/badge/Web-Run_Now-4630EB.svg?style=for-the-badge&logo=GOOGLE-CHROME&labelColor=000&logoColor=fff" target="_blank" /></a>
          # msg: Native Component List for this branch is ready<br><a aria-label="Try the web preview of this PR" href="https://expo-web-${{ github.event.number }}.now.sh/"><img longdesc="Try the web preview of this PR" src="https://img.shields.io/badge/Web-Run_Now-4630EB.svg?style=for-the-badge&logo=GOOGLE-CHROME&labelColor=000&logoColor=fff" target="_blank" /></a><br><a aria-label="Try the native preview of this PR" href="https://expo.io/${{ steps.expo.outputs.path }}"><img longdesc="Try the native preview of this PR" src="https://img.shields.io/badge/Native-Run_Now-4630EB.svg?style=for-the-badge&logo=EXPO&labelColor=000&logoColor=fff" target="_blank" /></a><br><br><a href="https://exp.host/${{ steps.expo.outputs.path }}"><img src="https://api.qrserver.com/v1/create-qr-code/?size=400x400&data=exp://exp.host/${{ steps.expo.outputs.path }}" height="200px" width="200px"></a>
