# This template creates and uploads a Docker image from the current environment
# It uses `docker commit` to do so, which can provide more accurate images than the standard template
# It requires the following conditions:
# - Job must be run inside a Docker container
# - The container's external name must be the same as the container's internal hostname
# - Docker must be installed inside said container and given sufficient permissions
# Currently, it is only usable for test jobs run on our self-managed systems
# Jobs run on Azure VMs will not be able to use this template (most if not all build jobs)

parameters:
- name: gpuTarget
  type: string
  default: ''
- name: environment
  type: string
  default: build
  values:
    - build
    - test
    - combined
    - amd
    - nvidia
- name: extraPaths
  type: string
  default: ''
- name: extraEnvVars
  type: object
  default: []
- name: forceDockerCreation
  type: boolean
  default: false

steps:
  - task: Bash@3
    displayName: Commit container and initialize Dockerfile
    condition: or(and(failed(), not(contains(variables['DOCKER_SKIP_GFX'], variables['JOB_GPU_TARGET']))), ${{ eq(parameters.forceDockerCreation, true) }})
    inputs:
      workingDirectory: $(Pipeline.Workspace)
      targetType: inline
      script: |
        docker commit $(hostname) az-ci-temp-image:latest
        echo "FROM az-ci-temp-image:latest" > Dockerfile
        echo "RUN sudo groupmod -g $(getent group render | awk -F: '{print $3}') render" >> Dockerfile
        echo "RUN sudo groupmod -g $(getent group docker | awk -F: '{print $3}') docker" >> Dockerfile
        echo "ENV PATH='$PATH:${{ parameters.extraPaths }}'" >> Dockerfile
        echo "ENTRYPOINT [\"/bin/bash\"]" >> Dockerfile
  - ${{ each extraEnvVar in parameters.extraEnvVars }}:
    - task: Bash@3
      displayName: Add extra environment variables
      condition: or(and(failed(), not(contains(variables['DOCKER_SKIP_GFX'], variables['JOB_GPU_TARGET']))), ${{ eq(parameters.forceDockerCreation, true) }})
      inputs:
        workingDirectory: $(Pipeline.Workspace)
        targetType: inline
        script: echo "ENV ${{ split(extraEnvVar, ':::')[0] }}='${{ split(extraEnvVar, ':::')[1] }}'" >> Dockerfile
  - task: Bash@3
    displayName: Print Dockerfile
    condition: or(and(failed(), not(contains(variables['DOCKER_SKIP_GFX'], variables['JOB_GPU_TARGET']))), ${{ eq(parameters.forceDockerCreation, true) }})
    inputs:
      workingDirectory: $(Pipeline.Workspace)
      targetType: inline
      script: cat Dockerfile
  - task: Docker@2
    displayName: Build and upload Docker image
    condition: or(and(failed(), not(contains(variables['DOCKER_SKIP_GFX'], variables['JOB_GPU_TARGET']))), ${{ eq(parameters.forceDockerCreation, true) }})
    inputs:
      containerRegistry: 'ContainerService3'
      ${{ if ne(parameters.gpuTarget, '') }}:
        repository: '$(Build.DefinitionName)-${{ parameters.environment }}-${{ parameters.gpuTarget }}'
      ${{ else }}:
        repository: '$(Build.DefinitionName)-${{ parameters.environment }}'
      Dockerfile: '$(Pipeline.Workspace)/Dockerfile'
      buildContext: '$(Pipeline.Workspace)'
  - task: Bash@3
    condition: or(and(failed(), not(contains(variables['DOCKER_SKIP_GFX'], variables['JOB_GPU_TARGET']))), ${{ eq(parameters.forceDockerCreation, true) }})
    displayName: "!! Docker Image URL !!"
    inputs:
      workingDirectory: $(Pipeline.Workspace)
      targetType: inline
      ${{ if ne(parameters.gpuTarget, '') }}:
        script: echo "rocmexternalcicd.azurecr.io/$(Build.DefinitionName)-${{ parameters.environment }}-${{ parameters.gpuTarget }}:$(Build.BuildId)" | tr '[:upper:]' '[:lower:]'
      ${{ else }}:
        script: echo "rocmexternalcicd.azurecr.io/$(Build.DefinitionName)-${{ parameters.environment }}:$(Build.BuildId)" | tr '[:upper:]' '[:lower:]'
