name: Build TRL Docker image

on:
  push:
    branches:
      - main
  workflow_dispatch:

concurrency:
  group: docker-image-builds
  cancel-in-progress: false

jobs:
  trl:
    name: "Build and push TRL Docker image"
    runs-on:
      group: aws-general-8-plus
    steps:
      - name: Checkout code
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1  # v7.0.1

      - name: Get TRL version from PyPI
        run: |
          VERSION=$(curl -s https://pypi.org/pypi/trl/json | jq -r .info.version)
          if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+([a-zA-Z0-9._-]*)?$ ]]; then
            echo "Invalid version format: $VERSION"
            exit 1
          fi
          if [[ ${#VERSION} -gt 50 ]]; then
            echo "Version string too long: $VERSION"
            exit 1
          fi
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c  # v4.2.0

      - name: Login to DockerHub
        uses: docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0  # v4.4.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Build and Push
        uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a  # v7.3.0
        env:
          VERSION: ${{ env.VERSION }}
        with:
          context: docker/trl
          push: true
          tags: |
            huggingface/trl:${{ env.VERSION }}
            huggingface/trl

      - name: Post to Slack
        if: always()
        uses: huggingface/hf-workflows/.github/actions/post-slack@a88e7fa2eaee28de5a4d6142381b1fb792349b67  # main
        with:
          slack_channel: ${{ secrets.CI_DOCKER_CHANNEL }}
          title: 🤗 Results of the TRL Dev Docker Image build
          status: ${{ job.status }}
          slack_token: ${{ secrets.SLACK_CIFEEDBACK_BOT_TOKEN }}

  trl-dev:
    name: "Build and push TRL Dev Docker image"
    runs-on:
      group: aws-general-8-plus
    steps:
      - name: Checkout code
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1  # v7.0.1

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c  # v4.2.0

      - name: Login to DockerHub
        uses: docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0  # v4.4.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Build and Push
        uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a  # v7.3.0
        with:
          context: docker/trl-dev
          push: true
          tags: |
            huggingface/trl:dev

      - name: Post to Slack
        if: always()
        uses: huggingface/hf-workflows/.github/actions/post-slack@a88e7fa2eaee28de5a4d6142381b1fb792349b67  # main
        with:
          slack_channel: ${{ secrets.CI_DOCKER_CHANNEL }}
          title: 🤗 Results of the TRL Dev Docker Image build
          status: ${{ job.status }}
          slack_token: ${{ secrets.SLACK_CIFEEDBACK_BOT_TOKEN }}
