# Workflow that builds and pushes the OpenHands app and enterprise Docker images to ghcr.io.
# Per-image build logic lives in .github/workflows/_build-image.yml.
name: Docker

on:
  push:
    branches:
      - main
      - "saas-rel-*"
      - "oss-rel-*"
  pull_request:
  workflow_dispatch:
    inputs:
      reason:
        description: "Reason for manual trigger"
        required: true
        default: ""

# PR events share a group so pushes supersede each other; each commit on a release branch gets its own group.
concurrency:
  group: ${{ github.workflow }}-${{ (github.head_ref && github.ref) || github.run_id }}
  cancel-in-progress: true

jobs:
  build_app:
    name: App
    if: github.event.pull_request.head.repo.fork != true
    uses: ./.github/workflows/_build-image.yml
    with:
      image: ghcr.io/openhands/openhands
      dockerfile: containers/app/Dockerfile

  build_enterprise:
    name: Enterprise
    if: github.event.pull_request.head.repo.fork != true
    needs: build_app
    uses: ./.github/workflows/_build-image.yml
    with:
      image: ghcr.io/openhands/enterprise-server
      dockerfile: enterprise/Dockerfile
      extra-build-args: OPENHANDS_VERSION=sha-${{ github.event.pull_request.head.sha || github.sha }}
      provenance: true
      sbom: true
      buildx-driver-opts: network=host

  update_pr_description:
    name: Update PR Description
    if: github.event_name == 'pull_request' && !github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]'
    needs: build_app
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Get short SHA
        id: short_sha
        env:
          PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
        run: printf 'SHORT_SHA=%s\n' "${PR_HEAD_SHA:0:7}" >> "$GITHUB_OUTPUT"

      - name: Update PR Description
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          REPO: ${{ github.repository }}
          SHORT_SHA: ${{ steps.short_sha.outputs.SHORT_SHA }}
        shell: bash
        run: |
          echo "Updating PR description with Docker and uvx commands"
          bash "${GITHUB_WORKSPACE}/.github/scripts/update_pr_description.sh"
