# This is a docker build check and publish job:
# 1. PR Triggered docker image build check
#   - is for image build check
#   - Enable on main/*-dev branch
#   - push: ${{ github.event_name != 'pull_request' }} ==> false
# 2. branches push trigger image publish
#   - is for branch/dev/nightly image
#   - commits are merge into main/*-dev  ==> vllm-ascend:main / vllm-ascend:*-dev
# 3. tags push trigger image publish
#   - is for final release image
#   - Publish when tag with v* (pep440 version)  ===>  vllm-ascend:v1.2.3 / vllm-ascend:v1.2.3rc1
name: Image Build and Push
on:
  pull_request:
    branches:
      - 'main'
      - '*-dev'
    paths:
      - '.github/workflows/pr_tag_image_build_and_push.yaml'
      - 'Dockerfile*'
      - '.github/workflows/_pr_image_build.yml'
      - 'vllm_ascend/**'
      - 'setup.py'
      - 'pyproject.toml'
      - 'requirements.txt'
      - 'cmake/**'
      - 'CMakeLists.txt'
      - 'csrc/**'
      # We should also trigger image build when nightly test related files are changed to ensure the image is valid for nightly tests
      - 'tests/e2e/nightly/'
    types: [ labeled ]
  push:
    # Publish image when tagging, the Dockerfile in tag will be build as tag image
    branches:
      - 'main'
      - '*-dev'
    tags:
      - 'v*'
    paths:
      - '.github/workflows/pr_tag_image_build_and_push.yaml'
      - 'Dockerfile*'
      - '.github/workflows/_pr_image_build.yml'
      - 'vllm_ascend/**'
      - 'setup.py'
      - 'pyproject.toml'
      - 'requirements.txt'
      - 'cmake/**'
      - 'CMakeLists.txt'
      - 'csrc/**'

  workflow_dispatch: 
# only cancel in-progress runs of the same workflow
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  image_build:
    if: ${{ github.event_name == 'push' || (contains(github.event.pull_request.labels.*.name, 'ready') && contains(github.event.pull_request.labels.*.name, 'ready-for-test')) }}
    name: Image Build and Push
    strategy:
      matrix:
        build_meta:
          - name: A2 Ubuntu
            dockerfile: Dockerfile
            suffix: ''
          - name: A2 openeuler
            dockerfile: Dockerfile.openEuler
            suffix: 'openeuler'
          - name: A3 Ubuntu
            dockerfile: Dockerfile.a3
            suffix: 'a3'
          - name: A3 openEuler
            dockerfile: Dockerfile.a3.openEuler
            suffix: 'a3-openeuler'
          # - name: 310P Ubuntu
          #   dockerfile: Dockerfile.310p
          # - name: 310P openEuler
          #   dockerfile: Dockerfile.310p.openEuler
    uses: ./.github/workflows/_pr_image_build.yaml
    with:
      dockerfile: ${{ matrix.build_meta.dockerfile }}
      suffix: ${{ matrix.build_meta.suffix }}
      quay_username: ${{ vars.QUAY_USERNAME }}
      should_push: ${{ github.event_name == 'push' && github.repository_owner == 'vllm-project' }}
    secrets:
      QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }}
