name: Build'n check
on:
  push:
    branches:
      - main
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
jobs:
  build:
    name: Check Template and Build example
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Setup Nix
        uses: cachix/install-nix-action@v27
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - name: Stylecheck
        run: nix-shell --run "./run-fmt.sh --check src/lib.typ"
      - id: build
        name: Build
        run: |
          nix-shell --run ./run-ci.sh
      - name: Upload artifact
        id: artifact-upload
        uses: actions/upload-artifact@v4
        with:
          name: example-document
          path: ${{ github.workspace }}/example.pdf
          if-no-files-found: error
          retention-days: 1
      - name: Output artifact ID
        run:  echo 'Artifact ID is ${{ steps.artifact-upload.outputs.artifact-id }}'
  release:
    runs-on: ubuntu-latest
    needs: build
    if: startsWith(github.ref, 'refs/tags/')
    permissions:
      contents: write
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Setup go
        uses: actions/setup-go@v4
        with:
          go-version: '>=1.20.1'
      - name: Download Artifcat
        id: download
        uses: actions/download-artifact@v3
        with:
          name: example-document
          path: ${{ github.workspace }}/Example.pdf
      - name: 'Artifact Download Path'
        run: echo ${{ steps.download.outputs.download-path }}
      - name: Prepare Release
        run: echo ${{ github.sha }} > Release.txt
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            Example.pdf
            Release.txt
            LICENSE