name: "build"
on:
  push:
     branches: [ master ]
  pull_request: {}
jobs:
  tests:
    strategy:
      matrix:
        os: [ ubuntu-latest, macos-latest ]
      fail-fast: false
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v3
      with:
        # fetch full history so that git merge-base works
        fetch-depth: 0
        # fetch PR commit, not predicted merge commit
        ref: ${{ github.event.pull_request.head.sha }}
    - uses: cachix/install-nix-action@v22

    # We are using the ic-hs-test cachix cache that is also used by
    # dfinity/ic-hs. This is partly laziness (on need to set up a separate
    # cache), but also to get the ic-ref-test binary without rebuilding
    - uses: cachix/cachix-action@v12
      with:
        name: ic-hs-test
        authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'

    # until https://github.com/cachix/cachix-action/issues/86 is fixed:
    - run: cachix watch-store ic-hs-test &

    - run: nix-env -iA nix-build-uncached -f nix/

    - name: "pre-build `drun`"
      if: startsWith(github.head_ref, 'update/ic-')
      run: |
        nix-build --max-jobs 1 -A drun
        nix-store --gc --print-roots | grep /motoko/

    - name: "nix-build"
      run: nix-build-uncached --max-jobs 4 -A all-systems-go -build-flags -L

    - name: Calculate performance delta
      if: github.actor != 'dependabot[bot]' && runner.os == 'Linux' && github.event_name == 'pull_request'
      run: |
        from="$(git merge-base origin/${{ github.base_ref }} HEAD)"
        to="${{ github.event.pull_request.head.sha }}"
        echo "Comparing changes from $from to $to"
        nix-build --max-jobs 4 perf-delta.nix -o perf-delta.txt \
          --argstr ref HEAD \
          --argstr from "$from" \
          --argstr to "$to"

    - name: Read performance delta
      if: github.actor != 'dependabot[bot]' && runner.os == 'Linux' && github.event_name == 'pull_request'
      id: perf
      uses: juliangruber/read-file-action@v1
      with:
        path: ./perf-delta.txt

    - name: Find performance comment
      if: github.actor != 'dependabot[bot]' && runner.os == 'Linux' && github.event_name == 'pull_request'
      uses: peter-evans/find-comment@v2
      id: fc
      with:
        issue-number: ${{ github.event.pull_request.number }}
        comment-author: 'github-actions[bot]'
        body-includes: '<!-- perf comment -->'

    # Forks can't add comments so this job does not run on forks, see
    # motoko#2864.
    - name: Create or update performance comment
      if: github.actor != 'dependabot[bot]' && runner.os == 'Linux' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
      uses: peter-evans/create-or-update-comment@v2
      with:
        comment-id: ${{ steps.fc.outputs.comment-id }}
        issue-number: ${{ github.event.pull_request.number }}
        body: |
          <!-- perf comment -->
          ${{ steps.perf.outputs.content }}
        edit-mode: replace

  reports:
    if: github.ref == 'refs/heads/master'
    needs: tests
    concurrency: ci-${{ github.ref }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: cachix/install-nix-action@v22
    - uses: cachix/cachix-action@v12
      with:
        name: ic-hs-test
    - name: Fetch report
      run: nix-build -A report-site -o report-site
    - name: Resolve symlinks
      run: cp -rL report-site report-site-copy
    - name: Push report to GitHub pages
      uses: JamesIves/github-pages-deploy-action@v4
      with:
        branch: gh-pages
        folder: report-site-copy
        single-commit: true

  artifacts:
    if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && contains(github.event.pull_request.labels.*.name, 'build_artifacts')
    needs: tests
    concurrency: ci-${{ github.ref }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: cachix/install-nix-action@v22
    - uses: cachix/cachix-action@v12
      with:
        name: ic-hs-test
    - name: nix-build
      run: |
        nix-build --arg officialRelease true -A moc
    # upload-artifact doesn't work for symlink dir
    # https://github.com/actions/upload-artifact/issues/92
    - run: echo "UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV
    - name: upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: moc
        path: ${{ env.UPLOAD_PATH }}
        retention-days: 5

