name: Publish

on:
  push:
    branches:
      - main
      - publish-test
    paths-ignore:
      - '.gitea/workflows/triggers/*'

jobs:
  publish:
    name: "Build and publish"
    runs-on: ubuntu-latest
    steps:
      - name: "Clone project repository"
        uses: actions/checkout@v3
      - name: "Get build info"
        id: build-info
        run: |
          build_tag=$(./scripts/create_build_tag_file.sh)
          echo "build-tag=v${build_tag}" >> $GITHUB_OUTPUT
      # At present the stock setup-python action fails on Linux/aarch64
      # Conditional steps below workaroud this by using deadsnakes for that case only
      - name: "Install Python for ARM on Linux"
        if: ${{ runner.arch == 'arm64' && runner.os == 'Linux' }}
        uses: deadsnakes/action@v3.0.1
        with:
          python-version: '3.8'
      - name: "Install Python cases other than ARM on Linux"
        if: ${{ ! (runner.arch == 'arm64' && runner.os == 'Linux') }}
        uses: actions/setup-python@v4
        with:
          python-version: '3.8'
      - name: "Print Python version"
        run: python3 --version
      - name: "Install shiv"
        run: pip install shiv
      - name: "Build local shiv package"
        id: build
        run: |
          ./scripts/build_shiv_package.sh
          result_code=$?
          echo "package-file=$(ls ./package/*)" >> $GITHUB_OUTPUT
          exit $result_code
      - name: "Stage artifact file"
        run: |
          cp ${{ steps.build.outputs.package-file }} ./laconic-so
      - name: "Create release"
        uses: https://gitea.com/cerc-io/action-gh-release@gitea-v2
        with:
          tag_name: ${{ steps.build-info.outputs.build-tag }}
          # On the publish test branch, mark our release as a draft
          # Hack using endsWith to workaround Gitea sometimes sending "publish-test" vs "refs/heads/publish-test"
          draft: ${{ endsWith('publish-test', github.ref ) }}
          files: ./laconic-so
      - name: Notify Vulcanize Slack on CI failure
        if: ${{ always() && github.ref_name == 'main' }}
        uses: ravsamhq/notify-slack-action@v2
        with:
          status: ${{ job.status }}
          notify_when: 'failure'
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.VULCANIZE_SLACK_CI_ALERTS }}
      - name: Notify DeepStack Slack on CI failure
        if: ${{ always() && github.ref_name == 'main' }}
        uses: ravsamhq/notify-slack-action@v2
        with:
          status: ${{ job.status }}
          notify_when: 'failure'
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.DEEPSTACK_SLACK_CI_ALERTS }}
