name: Build and publish

on:
  push:
    tags:
      - "v*"

jobs:
  build:
    name: "Build"
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macOS-11
            release_name: combc-mac
          - os: ubuntu-latest
            release_name: combc-ubuntu
          - os: windows-latest
            release_name: combc-windows.exe      
        ocaml-compiler:
          - 4.13.1     
        os: 
          - macOS-11
          - ubuntu-latest
          - windows-latest


    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Use OCaml ${{ matrix.ocaml-compiler }}
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}

      - name: Install deps
        run: opam install . --deps-only

      - name: Build 
        run: opam exec -- dune build

      - name: Copy executable
        run: cp _build/release-unix/src/main.exe ${{ matrix.release_name }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.release_name }}
          path: ${{ matrix.release_name }}

  publish:
    name: Publish
    needs: build
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Download artifacts
        uses: actions/download-artifact@v3

      - name: Publish 
        uses: softprops/action-gh-release@v1
        with:
          name: "Executables"
          fail_on_unmatched_files: true
          files: |
           combc-ubuntu/combc-ubuntu
           combc-mac/combc-mac
           combc-windows.exe/combc-windows.exe


