name: workflow

on: push

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-20.04
          - macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      # setup repository
      - name: Checkout
        uses: actions/checkout@v2

      # install dependencies
      - name: Run build
        run: .github/scripts/build.sh
        shell: bash

      # build documentation
      - name: Documentation
        run: .github/scripts/documentation.sh
        shell: bash

      # upload artifacts
      - uses: actions/upload-artifact@v2
        with:
          name: imitator-${{ matrix.os }}
          path: |
            bin/*
            doc/IMITATOR-user-manual.pdf
            doc/IMITATOR-not-developer-manual.pdf

  deploy:
    if: startsWith(github.ref, 'refs/tags/')
    name: Publish - Github Release
    runs-on: ubuntu-latest
    needs: build
    steps:
      # retrieve information
      - name: Retrieve artifacts
        uses: actions/download-artifact@v2

      # deploy
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ github.ref_name }}
          release_name: ${{ github.ref_name }}
          files: |
            imitator-macos-*/bin/*
            imitator-ubuntu-*/bin/*
            imitator-ubuntu-*/doc/IMITATOR-user-manual.pdf
            imitator-ubuntu-*/doc/IMITATOR-not-developer-manual.pdf
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  docker:
    if: startsWith(github.ref, 'refs/tags/') || (github.ref == 'refs/heads/develop')
    name: Publish - Docker Hub
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: imitator/imitator

      - name: Build and push Docker image
        uses: docker/build-push-action@v4
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
