---
parameters:
  - name: job_name_suffix
    type: string
  - name: container_name
    type: string
  - name: build_scenario_name
    type: string
  - name: python_version
    type: string
    default: 3.x

jobs:
- job: BuildTestImage${{ parameters.job_name_suffix }}
  displayName: Build ${{ parameters.container_name }} test container
  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '${{ parameters.python_version }}'

  - script: python -m pip install --upgrade pip setuptools wheel ansible
    retryCountOnTaskFailure: 5
    displayName: Install tools

  - script: pip install molecule-plugins[docker] "requests<2.29"
    retryCountOnTaskFailure: 5
    displayName: Install molecule

  - script: molecule create -s ${{ parameters.build_scenario_name }}
    retryCountOnTaskFailure: 5
    displayName: Create test container
    env:
      ANSIBLE_LIBRARY: ./molecule

  - script: |
      docker stop ${{ parameters.build_scenario_name }}
      docker commit ${{ parameters.build_scenario_name }} quay.io/ansible-freeipa/upstream-tests:${{ parameters.container_name }}
      docker login -u="$QUAY_ROBOT_USERNAME" -p="$QUAY_ROBOT_TOKEN" quay.io
      docker push quay.io/ansible-freeipa/upstream-tests:${{ parameters.container_name }}
    displayName: Save image and upload
    env:
      # Secrets needs to be mapped as env vars to work properly
      QUAY_ROBOT_TOKEN: $(QUAY_ROBOT_TOKEN)
