name: build

on:
  push:
    tags: ["*.*.*"]

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Install protoc
        run: |
          sudo apt-get update
          sudo apt-get -y install protobuf-compiler

      - name: Run tests
        run: cargo test --verbose

      - name: Install dependencies
        run: |
          sudo apt-get -y install gcc-mingw-w64-x86-64 gcc-mingw-w64-i686
          rustup target add x86_64-pc-windows-gnu
          rustup target add i686-pc-windows-gnu
          rustup target add x86_64-unknown-linux-gnu
          rustup target add x86_64-apple-darwin
          mkdir dist
          mkdir builds
      - name: Build Win64
        run: |
          cargo rustc --bin pucli --release --target=x86_64-pc-windows-gnu -- -C linker=x86_64-w64-mingw32-gcc
          mkdir builds/pucli-win64
          cp target/x86_64-pc-windows-gnu/release/pucli.exe builds/pucli-win64
          tar -C builds -czvf dist/pucli-win64.tar.gz pucli-win64
      - name: Build Linux
        run: |
          cargo rustc --bin pucli --release --target=x86_64-unknown-linux-gnu
          mkdir builds/pucli-linux
          cp target/x86_64-unknown-linux-gnu/release/pucli builds/pucli-linux
          tar -C builds -czvf dist/pucli-linux.tar.gz pucli-linux
      - name: Upload release
        uses: softprops/action-gh-release@v1
        with:
          # body_path: CHANGELOG.md # TBD
          files: |
            dist/pucli-win64.tar.gz
            dist/pucli-linux.tar.gz
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish cargo package
        env:
          CARGO_ACCESS_TOKEN: ${{ secrets.CARGO_ACCESS_TOKEN }}
        run: |
          cargo login "$CARGO_ACCESS_TOKEN"
          cargo publish
