name: aws-zig mach nominated build
on:
  workflow_dispatch:
  schedule:
    - cron: '0 12 * * *' # noon UTC, 4AM Pacific
  push:
    branches:
      - 'zig-mach'
env:
  PKG_PREFIX: nominated-zig
jobs:
  build-zig-nominated-mach-latest:
    container:
      # We need CAP_SYS_PTRACE for stack traces due to a regression in 0.14.0
      # TODO: Remove this after https://github.com/ziglang/zig/issues/21815 is
      # addressed
      options: --cap-add CAP_SYS_PTRACE
    runs-on: ubuntu-latest
    # Need to use the default container with node and all that, so we can
    # use JS-based actions like actions/checkout@v3...
    # container:
    #   image: alpine:3.15.0
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          ref: zig-mach
      - name: Setup Zig
        uses: mlugg/setup-zig@v1.2.1
        with:
          version: mach-latest
      - name: Run gen
        run: zig build gen --verbose
      - name: Run smoke test
        run: zig build smoke-test --verbose
      - name: Run full tests
        run: zig build test --verbose
      # Zig package manager expects everything to be inside a directory in the archive,
      # which it then strips out on download. So we need to shove everything inside a directory
      # the way GitHub/Gitea does for repo archives
      #
      # Also, zig tar process doesn't handle gnu format for long names, nor does it seam to
      # handle posix long name semantics cleanly either. ustar works. This
      # should be using git archive, but we need our generated code to be part of it
      - name: Package source code with generated models
        run: |
          tar -czf ${{ runner.temp }}/${{ github.sha }}${{ env.PKG_PREFIX }}-with-models.tar.gz \
            --format ustar \
            --exclude 'zig-*' \
            --transform 's,^,${{ github.sha }}/,' *
      # - name: Sign
      #   id: sign
      #   uses: https://git.lerch.org/lobo/action-hsm-sign@v1
      #   with:
      #     pin: ${{ secrets.HSM_USER_PIN }}
      #     files: ???
      #     public_key: 'https://emil.lerch.org/serverpublic.pem'
      # - run: |
      #     echo "Source 0 should be ./bar: ${{ steps.sign.outputs.SOURCE_0 }}"
      # - run: |
      #     echo "Signature 0 should be ./bar.sig: ${{ steps.sign.outputs.SIG_0 }}"
      # - run: echo "URL of bar (0) is ${{ steps.sign.outputs.URL_0 }}"
      # - run: |
      #     echo "Source 1 should be ./foo: ${{ steps.sign.outputs.SOURCE_1 }}"
      # - run: |
      #     echo "Signature 1 should be ./foo.sig: ${{ steps.sign.outputs.SIG_1 }}"
      # - run: echo "URL of foo (1) is ${{ steps.sign.outputs.URL_1 }}"
      - name: Publish source code with generated models
        run: |
          curl --user ${{ github.actor }}:${{ secrets.PACKAGE_PUSH }} \
            --upload-file ${{ runner.temp }}/${{ github.sha }}${{ env.PKG_PREFIX }}-with-models.tar.gz \
            https://git.lerch.org/api/packages/lobo/generic/aws-sdk-with-models/${{ github.sha }}/${{ github.sha }}${{ env.PKG_PREFIX }}-with-models.tar.gz
      - name: Build example
        run: ( cd example && zig build ) # Make sure example builds
      - name: Notify
        uses: https://git.lerch.org/lobo/action-notify-ntfy@v2
        if: always()
        with:
          host: ${{ secrets.NTFY_HOST }}
          topic: ${{ secrets.NTFY_TOPIC }}
          user: ${{ secrets.NTFY_USER }}
          password: ${{ secrets.NTFY_PASSWORD }}