name: publish-docker

on:
  push:
    paths-ignore:
      - ".github/**"
      - "docs/**"
      - "resources/**"
      - "service/**"
      - "tests/**"
      - "tools/elena/**"
      - "tools/package_tools/**"
      - "tools/scripts/**"
      - "tools/generate_md_table.py"
      - "**/*.md"
    branches:
      - main
    tags:
      - "v*.*.*"

jobs:
  publish_docker_image:
    runs-on: ubuntu-latest
    environment: 'prod'
    env:
      TAG_PREFIX: openmmlab/mmdeploy:ubuntu20.04-cuda11.8-mmdeploy
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Check disk space
        run: |
          df -h
          ls /opt/hostedtoolcache
          rm -rf ${GITHUB_WORKSPACE}/.git
          rm -rf  /opt/hostedtoolcache/go
          rm -rf  /opt/hostedtoolcache/node
          rm -rf  /opt/hostedtoolcache/Ruby
          rm -rf  /opt/hostedtoolcache/CodeQL
          cat /proc/cpuinfo  | grep -ic proc
          free
          df -h
          df . -h
      - name: Get docker info
        run: |
          docker info
      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push the latest Docker image
        run: |
          export TAG=$TAG_PREFIX
          echo "TAG=${TAG}" >> $GITHUB_ENV
          echo $TAG
          docker build ./docker/Release/ -t ${TAG} --no-cache
          docker push $TAG
      - name: Push docker image with released tag
        if: startsWith(github.ref, 'refs/tags/') == true
        run: |
          export MMDEPLOY_VERSION=$(python3 -c "import sys; sys.path.append('mmdeploy');from version import __version__;print(__version__)")
          echo $MMDEPLOY_VERSION
          export RELEASE_TAG=${TAG_PREFIX}${MMDEPLOY_VERSION}
          echo $RELEASE_TAG
          docker tag $TAG $RELEASE_TAG
          docker push $RELEASE_TAG
