name: Website
on:
  push:
    branches:
      - main
    paths:
      - 'packages/website/**'
      - '.github/workflows/website.yml'
      - 'package-lock.json'
  pull_request:
    paths:
      - 'packages/website/**'
      - '.github/workflows/website.yml'
      - 'package-lock.json'
jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - uses: bahmutov/npm-install@v1
      - run: npm run lint -w packages/website

  test-e2e:
    name: "Test e2e - ${{ matrix.browser }}"
    strategy:
      fail-fast: false
      matrix:
        browser:
          - firefox
          - chromium
        os:
          - ubuntu-18.04
        node-version:
          - 16
        test_results_path:
          # corresponds to playwright invocation/configuration
          - packages/website/test-results
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
      - uses: bahmutov/npm-install@v1
        env:
          PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
      - run: npm run build -w packages/website
        env:
          NEXT_PUBLIC_API: http://localhost:8787
          NEXT_PUBLIC_ENV: test-e2e
          NEXT_PUBLIC_MAGIC: ${{ secrets.STAGING_MAGIC_PUBLIC_KEY }}
      - run: npx playwright install --with-deps ${{ matrix.browser }} chromium
        working-directory: packages/website
      - run: touch .env
      - run: xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24" -- npm run -w @web3-storage/website test:e2e -- --project=${{ matrix.browser }}
        env:
          NEXT_PUBLIC_API: http://localhost:8787
          NEXT_PUBLIC_ENV: dev
          NEXT_PUBLIC_MAGIC: ${{ secrets.STAGING_MAGIC_PUBLIC_KEY }}
      - name: Check for website test results
        id: check_website_test_results
        uses: andstor/file-existence-action@v1
        with:
          files: ${{ matrix.test_results_path }}
      - if: steps.check_website_test_results.outputs.files_exists == 'true'
        uses: actions/upload-artifact@v1
        with:
          name: ${{ matrix.browser }}-${{ matrix.os }}-test-results
          path: ${{ matrix.test_results_path }}

  preview:
    name: Preview
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - uses: bahmutov/npm-install@v1
      - run: npm run build -w packages/client
      - run: npm run build -w packages/website
        env:
          NEXT_PUBLIC_ENV: staging
          NEXT_PUBLIC_API: https://api-staging.web3.storage
          NEXT_PUBLIC_MAGIC: ${{ secrets.STAGING_MAGIC_PUBLIC_KEY }}
          NEXT_PUBLIC_COUNTLY_URL: ${{ secrets.COUNTLY_URL }}
          NEXT_PUBLIC_COUNTLY_KEY: ${{ secrets.COUNTLY_KEY }}
      - name: Add to web3.storage
        uses: web3-storage/add-to-web3@v2
        id: ipfs
        with:
          path_to_add: packages/website/out
          web3_token: ${{ secrets.WEB3_TOKEN }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Update IPFS DNSLink https://staging.web3.storage
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        run: npx dnslink-cloudflare --record staging --domain web3.storage --link /ipfs/${{ steps.ipfs.outputs.cid }}
        env:
          CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
      - name: Deploy preview build to Cloudflare Pages
        uses: mathiasvr/command-output@v1.1.0
        id: cloudflare
        with:
          run: npx wrangler pages publish --project-name web3-storage-staging --branch "$GITHUB_REF_NAME" --commit-hash "$GITHUB_SHA" ./packages/website/out
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_PAGES_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
      - name: Find Cloudflare Pages preview URL
        uses: mathiasvr/command-output@v1.1.0
        id: cloudflare_url
        with:
          run: egrep -o 'https?://[^ ]+' <(echo "${{ steps.cloudflare.outputs.stdout }}")
      - name: Find Current Pull Request
        uses: jwalton/gh-find-current-pr@v1.3.0
        id: finder
      - name: Update Preview URLs PR Comment
        uses: marocchino/sticky-pull-request-comment@v2.2.0
        with:
          number: ${{ steps.finder.outputs.pr }}
          message: |
            ### Website preview 🔗✨
            - 🙂 https://w3s.link/ipfs/${{ steps.ipfs.outputs.cid }}
            - ⛅️ ${{ steps.cloudflare_url.outputs.stdout }}

            [build log](/${{ github.repository }}/actions/runs/${{ github.run_id }})
      - name: Add Preview URLs as Job Summary
        run: |
          echo "
          ### Website preview 🔗✨
            - 🙂 https://w3s.link/ipfs/${{ steps.ipfs.outputs.cid }}
            - ⛅️ ${{ steps.cloudflare_url.outputs.stdout }}

            [build log](/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> $GITHUB_STEP_SUMMARY

  changelog:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    name: Changelog
    runs-on: ubuntu-latest
    outputs:
      releases_created: ${{ steps.tag-release.outputs.releases_created }}
    steps:
      - uses: GoogleCloudPlatform/release-please-action@v3
        id: tag-release
        with:
          path: packages/website
          token: ${{ secrets.GITHUB_TOKEN }}
          release-type: node
          monorepo-tags: true
          package-name: website
          changelog-types: '[{"type":"feat","section":"Features","hidden":false},{"type":"fix","section":"Bug Fixes","hidden":false},{"type":"chore","section":"Other Changes","hidden":false}]'

  release:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' && needs.changelog.outputs.releases_created
    name: Release
    runs-on: ubuntu-latest
    needs:
      - test
      - test-e2e
      - preview
      - changelog
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16'
          registry-url: https://registry.npmjs.org/
      - uses: bahmutov/npm-install@v1
      - run: npm run build -w packages/client
      - run: npm run build -w packages/website
        env:
          NEXT_PUBLIC_ENV: production
          NEXT_PUBLIC_API: https://api.web3.storage
          NEXT_PUBLIC_MAGIC: ${{ secrets.PROD_MAGIC_PUBLIC_KEY }}
          NEXT_PUBLIC_COUNTLY_URL: ${{ secrets.COUNTLY_URL }}
          NEXT_PUBLIC_COUNTLY_KEY: ${{ secrets.COUNTLY_KEY }}
      - name: Add to web3.storage
        uses: web3-storage/add-to-web3@v2
        id: ipfs
        with:
          path_to_add: packages/website/out
          web3_token: ${{ secrets.WEB3_TOKEN }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Update IPFS DNSLink https://web3.storage
        run: npx dnslink-cloudflare --record _dnslink --domain web3.storage --link /ipfs/${{ steps.ipfs.outputs.cid }}
        env:
          CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
      - name: Deploy https://web3.storage to Cloudflare Pages
        run: npx wrangler pages publish --project-name web3-storage --branch "main" --commit-hash "$GITHUB_SHA" ./packages/website/out
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_PAGES_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
