name: Compiler Explorer

on: [push, pull_request]

jobs:
  build-and-test:
    runs-on: ubuntu-20.04

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Use Node.js 16.x
        uses: actions/setup-node@v1
        with:
          node-version: 16.x
      - name: Get npm cache directory
        id: npm-cache
        run: |
          echo "::set-output name=dir::$(npm config get cache)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.npm-cache.outputs.dir }}
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - name: Install prerequisites
        run: make prereqs
      - name: Build and test
        run: |
          npm run lint
          npm run ci-test
      - name: Code coverage
        run: npm run codecov

  build-dist-and-deploy:
    if: github.repository_owner == 'compiler-explorer'
    needs: [build-and-test]
    runs-on: ubuntu-20.04

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Use Node.js 16.x
        uses: actions/setup-node@v1
        with:
          node-version: 16.x
      - name: Get npm cache directory
        id: npm-cache
        run: |
          echo "::set-output name=dir::$(npm config get cache)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.npm-cache.outputs.dir }}
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - name: Build distribution
        id: build_dist
        run: |
          export SENTRY_AUTH_TOKEN='${{ secrets.SENTRY_AUTH_TOKEN }}'
          export SENTRY_DSN='${{ secrets.SENTRY_DSN }}'
          export SENTRY_ORG=compiler-explorer
          export SENTRY_PROJECT=compiler-explorer
          etc/scripts/build-dist.sh
      - name: Deploy
        uses: jakejarvis/s3-sync-action@master
        with:
          args: --acl public-read --follow-symlinks
        env:
          AWS_S3_BUCKET: compiler-explorer
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          SOURCE_DIR: out/dist-bin
          DEST_DIR: dist/gh/${{ steps.build_dist.outputs.branch }}
      - name: Tag the commit
        uses: tvdias/github-tagger@v0.0.2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          tag: gh-${{ github.run_number }}
