
name: docs-pr-deploy

defaults:
  run:
    shell: bash
    working-directory: docs

on:
  workflow_dispatch: {}
  push:
    paths:
      - 'docs/**'
      - '.github/workflows/docs-pr-deploy.yml'
      - '.github/workflows/docs-pr-destroy.yml'
  pull_request:
    paths:
      - 'docs/**'
      - '.github/workflows/docs.yml'
      - '.github/workflows/docs-pr-deploy.yml'
      - '.github/workflows/docs-pr-destroy.yml'
    types:
      - labeled
      - synchronize

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

jobs:
  docs-pr-deploy:
    if: contains(github.event.pull_request.labels.*.name, 'preview')
    runs-on: ubuntu-20.04
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
      - name: ⬢ Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 16.x
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-docs: 'true'
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: 🪣 Set up docs preview bucket
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: 'us-east-1'
        run: |
          # Create bucket
          aws s3api create-bucket --bucket docs.expo.dev-pr-${{ github.event.pull_request.number }}

          # Set "block public access" to off
          aws s3api put-public-access-block --bucket docs.expo.dev-pr-${{ github.event.pull_request.number }} --public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"

          # Set bucket policy to public
          aws s3api put-bucket-policy --bucket docs.expo.dev-pr-${{ github.event.pull_request.number }} --policy "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::docs.expo.dev-pr-${{ github.event.pull_request.number }}/*\"}]}"

          # Set up static website hosting
          aws s3 website s3://docs.expo.dev-pr-${{ github.event.pull_request.number }}/ --index-document index.html
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-docs-hit != 'true'
        run: yarn install --frozen-lockfile
      - run: yarn danger ci
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: 🏗️ Build Docs website for deploy
        run: yarn export
        timeout-minutes: 20
        env:
          AWS_BUCKET: 'docs.expo.dev-pr-${{ github.event.pull_request.number }}'
      - name: 🚀 Deploy Docs website
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: 'us-east-1'
          AWS_BUCKET: docs.expo.dev-pr-${{ github.event.pull_request.number }}
        run: ./deploy.sh
      - name: 🔍 Find old comment if it exists
        uses: peter-evans/find-comment@v2
        id: old_comment
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: 'expo-bot'
          body-includes: 📘 Your docs
      - name: 💬 Add comment with preview URL
        if: steps.old_comment.outputs.comment-id == ''
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.EXPO_BOT_GITHUB_TOKEN }}
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '📘 Your docs [preview website](http://docs.expo.dev-pr-${{ github.event.pull_request.number }}.s3-website-us-east-1.amazonaws.com/) is ready!'
            });
      - name: 💬 Update comment with preview URL
        if: steps.old_comment.outputs.comment-id != ''
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.EXPO_BOT_GITHUB_TOKEN }}
          script: |
            github.rest.issues.updateComment({
              issue_number: context.issue.number,
              comment_id: '${{ steps.old_comment.outputs.comment-id }}',
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '📘 Your docs [preview website](http://docs.expo.dev-pr-${{ github.event.pull_request.number }}.s3-website-us-east-1.amazonaws.com/) is ready!'
            });
