name: Image

on:
  push:
    branches: [ master ]

jobs:
  build:
    name: Image build and push
    runs-on: ubuntu-20.04

    steps:
    - name: Set Up Environment Variables
      env:
        REPO: quay.io/${{ secrets.QUAY_ACCOUNT_NAME }}
        IMG: gatekeeper-operator
      run: |
        echo "REPO=${REPO}" >> ${GITHUB_ENV}
        WORKFLOW_IMG="${REPO}/${IMG}"
        echo "WORKFLOW_IMG=${WORKFLOW_IMG}" >> ${GITHUB_ENV}
        echo "WORKFLOW_BUNDLE_IMG=${WORKFLOW_IMG}-bundle" >> ${GITHUB_ENV}
        echo "WORKFLOW_BUNDLE_INDEX_IMG=${WORKFLOW_IMG}-bundle-index" >> ${GITHUB_ENV}

    - uses: actions/checkout@v2
      with:
        fetch-depth: 0 # Fetch all history for all tags and branches
    - uses: docker/setup-qemu-action@v1
    - uses: docker/setup-buildx-action@v1
      with:
        driver-opts: image=moby/buildkit:v0.8-beta
    - uses: docker/login-action@v1
      with:
        registry: quay.io
        username: ${{ secrets.QUAY_USERNAME }}
        password: ${{ secrets.QUAY_TOKEN }}

    - name: Set Up Go to Install OPM
      uses: actions/setup-go@v2
      with:
        go-version: '1.16'

    - name: Build and Push Operator Image
      run: |
        make docker-build VERSION=master
        make docker-push IMG=${WORKFLOW_IMG}:master

    - name: Build and Push Bundle Image
      run: |
        make bundle-build VERSION=master
        make docker-push IMG=${WORKFLOW_BUNDLE_IMG}:master

    - name: Build and Push Bundle Index Image
      run: |
        PREV_BUNDLE_INDEX_IMG_VERSION=$(make prev-bundle-index-image-version)
        make bundle-index-build VERSION=master REPLACES_VERSION=${PREV_BUNDLE_INDEX_IMG_VERSION}
        make docker-push IMG=${WORKFLOW_BUNDLE_INDEX_IMG}:master
