name: nightly build

env:
  AWS_ACCESS_KEY_ID: ${{ vars.NIGHTLY_BUILD_AWS_ACCESS_ID }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.NIGHTLY_BUILD_AWS_SECRET_ACCESS_KEY }}
  AWS_BUCKET: ${{ vars.NIGHTLY_BUILD_AWS_BUCKET }}
  AWS_REGION: ${{ vars.NIGHTLY_BUILD_AWS_REGION }}

  RELEASE: ${{ github.ref_type == 'tag' && github.ref_name || 'nightly' }}
  GIT_VERSION_FLAG: ${{ github.ref_type == 'tag' && format('-Dgit_version={0}', github.ref_name) || '' }}

on:
  push:
    tags:
      - '*'
  schedule:
    - cron: "2 2 * * *"

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build-linux-x86_64:
    env:
      ARCH: x86_64
      OS: linux

    runs-on: ubuntu-22.04
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: ./.github/actions/install
        with:
          os: ${{env.OS}}
          arch: ${{env.ARCH}}

      - name: v8 snapshot
        run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin

      - name: zig build
        run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64 -Dgit_commit=$(git rev-parse --short ${{ github.sha }}) ${{ env.GIT_VERSION_FLAG }}

      - name: Rename binary
        run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: upload on s3
        run: |
          export DIR=`git show --no-patch --no-notes --pretty='%cs_%h'`
          aws s3 cp --storage-class=GLACIER_IR lightpanda-${{ env.ARCH }}-${{ env.OS }} s3://lpd-nightly-build/${DIR}/lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: Upload the build
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: lightpanda-${{ env.ARCH }}-${{ env.OS }}
          tag: ${{ env.RELEASE }}
          makeLatest: true

  build-linux-aarch64:
    env:
      ARCH: aarch64
      OS: linux

    runs-on: ubuntu-22.04-arm
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: ./.github/actions/install
        with:
          os: ${{env.OS}}
          arch: ${{env.ARCH}}

      - name: v8 snapshot
        run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin

      - name: zig build
        run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=generic -Dgit_commit=$(git rev-parse --short ${{ github.sha }}) ${{ env.GIT_VERSION_FLAG }}

      - name: Rename binary
        run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: upload on s3
        run: |
          export DIR=`git show --no-patch --no-notes --pretty='%cs_%h'`
          aws s3 cp --storage-class=GLACIER_IR lightpanda-${{ env.ARCH }}-${{ env.OS }} s3://lpd-nightly-build/${DIR}/lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: Upload the build
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: lightpanda-${{ env.ARCH }}-${{ env.OS }}
          tag: ${{ env.RELEASE }}
          makeLatest: true

  build-macos-aarch64:
    env:
      ARCH: aarch64
      OS: macos

    # macos-14 runs on arm CPU. see
    # https://github.com/actions/runner-images?tab=readme-ov-file
    runs-on: macos-14
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: ./.github/actions/install
        with:
          os: ${{env.OS}}
          arch: ${{env.ARCH}}

      - name: v8 snapshot
        run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin

      - name: zig build
        run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dgit_commit=$(git rev-parse --short ${{ github.sha }}) ${{ env.GIT_VERSION_FLAG }}

      - name: Rename binary
        run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: upload on s3
        run: |
          export DIR=`git show --no-patch --no-notes --pretty='%cs_%h'`
          aws s3 cp --storage-class=GLACIER_IR lightpanda-${{ env.ARCH }}-${{ env.OS }} s3://lpd-nightly-build/${DIR}/lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: Upload the build
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: lightpanda-${{ env.ARCH }}-${{ env.OS }}
          tag: ${{ env.RELEASE }}
          makeLatest: true

  build-macos-x86_64:
    env:
      ARCH: x86_64
      OS: macos

    runs-on: macos-14-large
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: ./.github/actions/install
        with:
          os: ${{env.OS}}
          arch: ${{env.ARCH}}

      - name: v8 snapshot
        run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin

      - name: zig build
        run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dgit_commit=$(git rev-parse --short ${{ github.sha }}) ${{ env.GIT_VERSION_FLAG }}

      - name: Rename binary
        run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: upload on s3
        run: |
          export DIR=`git show --no-patch --no-notes --pretty='%cs_%h'`
          aws s3 cp --storage-class=GLACIER_IR lightpanda-${{ env.ARCH }}-${{ env.OS }} s3://lpd-nightly-build/${DIR}/lightpanda-${{ env.ARCH }}-${{ env.OS }}

      - name: Upload the build
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: lightpanda-${{ env.ARCH }}-${{ env.OS }}
          tag: ${{ env.RELEASE }}
          makeLatest: true
