# yamllint disable

name: Release
on:
  release:
    types: [ published ]
env:
  CARGO_TERM_COLOR: always
jobs:
  test:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: bazelbuild/setup-bazelisk@v2
      - name: Install Protobuf Headers
        run: sudo apt-get install -y protobuf-compiler libprotobuf-dev
      - name: Mount bazel cache
        uses: actions/cache@v3
        with:
          path: "~/.cache/bazel"
          key: ${{ runner.os }}-bazel
      - run: bazel test -c opt //tests
  cargo-publish:
    needs: test
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Install Protoc
        uses: arduino/setup-protoc@v1
      - uses: actions/cache@v2.1.6
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - run: cargo login ${CRATES_IO_TOKEN}
        env:
          CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
      - uses: actions-rs/cargo@v1.0.3
        with:
          command: publish
          args: --verbose
  push-to-docker:
    needs: test
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: bazelbuild/setup-bazelisk@v2
      - name: Mount bazel cache
        uses: actions/cache@v3
        with:
          path: "~/.cache/bazel"
          key: ${{ runner.os }}-bazel
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - run: DOCKER_CONFIG=$HOME/.docker bazel run -c opt //:push-summa-server
  twine-upload:
    needs: test
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: bazelbuild/setup-bazelisk@v2
      - name: Mount bazel cache
        uses: actions/cache@v3
        with:
          path: "~/.cache/bazel"
          key: ${{ runner.os }}-bazel
      - uses: actions/setup-python@v4
      - uses: BSFishy/pip-action@v1
        with:
          packages: |
            twine
      - run: bazel build -c opt //aiosumma:aiosumma-wheel && TWINE_USERNAME=${{ secrets.TWINE_USERNAME }} TWINE_PASSWORD=${{ secrets.TWINE_PASSWORD }} twine upload bazel-bin/aiosumma/*.whl || /bin/true