name: Publish Docker images

on:
  workflow_dispatch:
  push:
    paths:
      - DOCKER_IMAGE_VERSION
    branches:
      - main

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          driver: docker

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - uses: actions/checkout@v4

      - name: Check if Docker image exists
        id: check-images
        run: |
          IMAGE_TAG=$(cat DOCKER_IMAGE_VERSION)
          if docker manifest inspect asterinas/osdk:${IMAGE_TAG} > /dev/null 2>&1; then
              echo "osdk_image_exists=true" >> $GITHUB_ENV
          else
              echo "osdk_image_exists=false" >> $GITHUB_ENV
          fi
          if docker manifest inspect asterinas/osdk:${IMAGE_TAG}-tdx > /dev/null 2>&1; then
              echo "osdk_tdx_image_exists=true" >> $GITHUB_ENV
          else
              echo "osdk_tdx_image_exists=false" >> $GITHUB_ENV
          fi
          if docker manifest inspect asterinas/asterinas:${IMAGE_TAG} > /dev/null 2>&1; then
              echo "asterinas_image_exists=true" >> $GITHUB_ENV
          else
              echo "asterinas_image_exists=false" >> $GITHUB_ENV
          fi
          if docker manifest inspect asterinas/asterinas:${IMAGE_TAG}-tdx > /dev/null 2>&1; then
              echo "asterinas_tdx_image_exists=true" >> $GITHUB_ENV
          else
              echo "asterinas_tdx_image_exists=false" >> $GITHUB_ENV
          fi

      - name: Fetch versions in the repo
        id: fetch-versions
        run: |
          ASTER_VERSION=$(cat DOCKER_IMAGE_VERSION)
          RUST_VERSION=$(grep -m1 -o 'nightly-[0-9]\+-[0-9]\+-[0-9]\+' rust-toolchain.toml)
          echo "aster_version=$ASTER_VERSION" >> "$GITHUB_OUTPUT"
          echo "rust_version=$RUST_VERSION" >> "$GITHUB_OUTPUT"

      - name: Build and push the OSDK development image
        if: env.osdk_image_exists == 'false'
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./osdk/tools/docker/Dockerfile
          platforms: linux/amd64
          push: true
          load: true
          tags: asterinas/osdk:${{ steps.fetch-versions.outputs.aster_version }}
          build-args: |
            ASTER_RUST_VERSION=${{ steps.fetch-versions.outputs.rust_version }}

      - name: Build and push the OSDK development image for Intel TDX
        if: env.osdk_tdx_image_exists == 'false'
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./osdk/tools/docker/tdx/Dockerfile
          platforms: linux/amd64
          push: true
          tags: asterinas/osdk:${{ steps.fetch-versions.outputs.aster_version }}-tdx
          build-args: |
            BASE_VERSION=${{ steps.fetch-versions.outputs.aster_version }}

      - name: Build and push the Asterinas development image
        if: env.asterinas_image_exists == 'false'
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./tools/docker/Dockerfile
          platforms: linux/amd64
          push: true
          load: true
          tags: asterinas/asterinas:${{ steps.fetch-versions.outputs.aster_version }}
          build-args: |
            BASE_VERSION=${{ steps.fetch-versions.outputs.aster_version }}

      - name: Build and push the Asterinas development image for Intel TDX
        if: env.asterinas_tdx_image_exists == 'false'
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./tools/docker/tdx/Dockerfile
          platforms: linux/amd64
          push: true
          tags: asterinas/asterinas:${{ steps.fetch-versions.outputs.aster_version }}-tdx
          build-args: |
            BASE_VERSION=${{ steps.fetch-versions.outputs.aster_version }}
