name: publish-docker

on:
  push:
    paths-ignore:
      - "!.github/workflows/docker.yml"
      - ".github/**"
      - "docs/**"
      - "resources/**"
      - "benchmark/**"
      - "tests/**"
      - "**/*.md"
    branches:
      - main
    tags:
      - "v*.*.*"

jobs:
  publish_docker_image:
    runs-on: ubuntu-latest
    environment: 'prod'
    env:
      TAG_PREFIX: "openmmlab/lmdeploy"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Free disk space
        uses: jlumbroso/free-disk-space@main
        with:
          # This might remove tools that are actually needed, if set to "true" but frees about 6 GB
          tool-cache: false
          docker-images: false
          # All of these default to true, but feel free to set to "false" if necessary for your workflow
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          swap-storage: false
      - 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:latest
          echo $TAG
          docker build docker/ -t ${TAG} --no-cache
          docker push $TAG
          echo "TAG=${TAG}" >> $GITHUB_ENV
      - name: Push docker image with released tag
        if: startsWith(github.ref, 'refs/tags/') == true
        run: |
          export LMDEPLOY_VERSION=$(python3 -c "import sys; sys.path.append('lmdeploy');from version import __version__;print(__version__)")
          echo $LMDEPLOY_VERSION
          export RELEASE_TAG=${TAG_PREFIX}:v${LMDEPLOY_VERSION}
          echo $RELEASE_TAG
          docker tag $TAG $RELEASE_TAG
          docker push $RELEASE_TAG
