---
name: Build and Push Images

on:
  workflow_dispatch:
  push:
    branches:
      - main
      - master
    paths:
      - 'build/**'
  pull_request:
    paths:
      - 'build/**'

jobs:
  build_and_push:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        openshift_version: [
          # 4.17.x (Latest minor)
          4.17.7, 4.17.3, 4.17.1, 4.17.0,
          # 4.16.x
          4.16.7, 4.16.4, 4.16.0,
          # 4.15.x
          4.15.17, 4.15.14, 4.15.12, 4.15.10, 4.15.3,
          # 4.14.x
          4.14.12, 4.14.8, 4.14.7, 4.14.3, 4.14.1,
          # Latest
          latest
        ]
    steps:
    - uses: actions/checkout@v3
    - name: Build image
      id: build-image
      uses: redhat-actions/buildah-build@v2
      with:
        tags: quay.io/openpipe/oc-local-runner:${{ matrix.openshift_version }}
        context: build
        containerfiles: |
          build/Containerfile
        build-args: |
          VERSION=${{ matrix.openshift_version }}
          PULL_SECRET=${{ secrets.PULL_SECRET }}
    - name: Login to Quay.io
      uses: docker/login-action@v3
      with:
        registry: quay.io
        username: ${{ secrets.REGISTRY_USER }}
        password: ${{ secrets.REGISTRY_PASSWORD }}

    - name: Push image
      id: push-to-quay
      uses: redhat-actions/push-to-registry@v2
      with:
        image: ${{ steps.build-image.outputs.image }}
        tags: ${{ steps.build-image.outputs.tags }}
        registry: quay.io/openpipe/oc-local-runner

    - name: Print image url
      run: echo "Image pushed to ${{ steps.push-to-quay.outputs.registry-paths }}"
