name: Deployment pipeline

on: [push, pull_request]

jobs:
    lint:
        name: Lint
        timeout-minutes: 4
        runs-on: ubuntu-latest
        env:
            HUSKY_SKIP_INSTALL: 1
        steps:
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v2
              with:
                  node-version: 16
            - run: npm ci
            - run: npm run lint

    test:
        name: Test
        timeout-minutes: 4
        runs-on: ubuntu-latest
        env:
            HUSKY_SKIP_INSTALL: 1
        steps:
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v2
              with:
                  node-version: 16
            - run: npm ci
            - run: npm test

    build:
        name: Compile
        timeout-minutes: 6
        runs-on: ubuntu-latest
        env:
            HUSKY_SKIP_INSTALL: 1
        steps:
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v2
              with:
                  node-version: 16
            - run: npm ci
            - run: npm run build
              env:
                  DATOCMS_API_TOKEN: ${{ secrets.DATOCMS_API_TOKEN }}
                  QUIRREL_BASE_URL: doesntmatter.shellphone.app

    deploy_dev:
        if: github.ref == 'refs/heads/master'
        needs: [lint, test, build]
        name: Deploy dev.shellphone.app
        runs-on: ubuntu-latest
        env:
            HUSKY_SKIP_INSTALL: 1
        steps:
            - uses: actions/checkout@v2
            - uses: superfly/flyctl-actions@master
              env:
                  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
              with:
                  args: "deploy -c ./fly.dev.toml --build-arg PANELBEAR_SITE_ID=${{ secrets.PANELBEAR_SITE_ID_DEV }} --build-arg DATOCMS_API_TOKEN=${{ secrets.DATOCMS_API_TOKEN }}"
            - uses: appleboy/discord-action@master
              with:
                  webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
                  webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
                  args: "https://dev.shellphone.app deployed with commit `${{ github.event.head_commit.message }}` (`${{ github.sha }}`) from branch `${{ github.ref }}`"

    deploy_prod:
        if: github.ref == 'refs/heads/production'
        needs: [lint, test, build]
        name: Deploy www.shellphone.app
        runs-on: ubuntu-latest
        env:
            HUSKY_SKIP_INSTALL: 1
        steps:
            - uses: actions/checkout@v2
            - uses: superfly/flyctl-actions@master
              env:
                  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
              with:
                  args: "deploy -c ./fly.prod.toml --build-arg PANELBEAR_SITE_ID=${{ secrets.PANELBEAR_SITE_ID_PROD }} --build-arg DATOCMS_API_TOKEN=${{ secrets.DATOCMS_API_TOKEN }}"
            - uses: appleboy/discord-action@master
              with:
                  webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
                  webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
                  args: "https://www.shellphone.app deployed with commit `${{ github.event.head_commit.message }}` (`${{ github.sha }}`) from branch `${{ github.ref }}`"
# TODO: on pull_request, deploy 24hour-long deployment at {commit_short_hash}.shellphone.app, provision db and seed data
