# Run a build step in a container or directly on the Actions runner
name: Download or Build Container
description: Download a container from the package registry, or build it if it's not found

inputs:
  container:
    description: Container name
    type: string
    required: true
  dockerfile:
    description: Dockerfile
    type: string
  base:
    description: Container base
    type: string
  registry:
    description: Docker registry to read and publish to
    type: string
    default: ghcr.io
  config-path:
    description: Path to Dockerfiles
    type: string
  github_token:
    description: GitHub Token
    type: string

runs:
  using: 'composite'
  steps:
  - name: Download container
    run: |
      IMAGE_NAME="${{ inputs.container }}"
      DOCKERFILE_PATH="${{ inputs.dockerfile }}"
      DOCKER_REGISTRY="${{ inputs.registry }}"
      DOCKERFILE_ROOT="${{ inputs.config-path }}"

      if [ "${DOCKERFILE_PATH}" = "" ]; then
        DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${IMAGE_NAME}"
      else
        DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${DOCKERFILE_PATH}"
      fi

      GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel)
      echo "::: git worktree is ${GIT_WORKTREE}"
      cd "${GIT_WORKTREE}"

      DOCKER_CONTAINER="${GITHUB_REPOSITORY}/${IMAGE_NAME}"
      DOCKER_REGISTRY_CONTAINER="${DOCKER_REGISTRY}/${DOCKER_CONTAINER}"

      echo "dockerfile=${DOCKERFILE_PATH}" >> $GITHUB_ENV
      echo "docker-container=${DOCKER_CONTAINER}" >> $GITHUB_ENV
      echo "docker-registry-container=${DOCKER_REGISTRY_CONTAINER}" >> $GITHUB_ENV

      # Identify the last git commit that touched the Dockerfiles
      # Use this as a hash to identify the resulting docker containers
      echo "::: dockerfile path is ${DOCKERFILE_PATH}"

      DOCKER_SHA=$(git log -1 --pretty=format:"%h" -- "${DOCKERFILE_PATH}")
      echo "docker-sha=${DOCKER_SHA}" >> $GITHUB_ENV

      echo "::: docker sha is ${DOCKER_SHA}"

      DOCKER_REGISTRY_CONTAINER_SHA="${DOCKER_REGISTRY_CONTAINER}:${DOCKER_SHA}"

      echo "docker-registry-container-sha=${DOCKER_REGISTRY_CONTAINER_SHA}" >> $GITHUB_ENV
      echo "docker-registry-container-latest=${DOCKER_REGISTRY_CONTAINER}:latest" >> $GITHUB_ENV

      echo "::: logging in to ${DOCKER_REGISTRY} as ${GITHUB_ACTOR}"

      exists="true"
      docker login https://${DOCKER_REGISTRY} -u ${GITHUB_ACTOR} -p ${GITHUB_TOKEN} || exists="false"

      echo "::: pulling ${DOCKER_REGISTRY_CONTAINER_SHA}"

      if [ "${exists}" != "false" ]; then
        docker pull ${DOCKER_REGISTRY_CONTAINER_SHA} || exists="false"
      fi

      if [ "${exists}" = "true" ]; then
        echo "::: docker container exists in registry"
        echo "docker-container-exists=true" >> $GITHUB_ENV
      else
        echo "::: docker container does not exist in registry"
        echo "docker-container-exists=false" >> $GITHUB_ENV
      fi
    shell: bash
    env:
      GITHUB_TOKEN: ${{ inputs.github_token }}
  - name: Create container
    run: |
      if [ "${{ inputs.base }}" != "" ]; then
        BASE_ARG="--build-arg BASE=${{ inputs.base }}"
      fi

      GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel)
      echo "::: git worktree is ${GIT_WORKTREE}"
      cd "${GIT_WORKTREE}"

      docker build -t ${{ env.docker-registry-container-sha }} --build-arg UID=$(id -u) --build-arg GID=$(id -g) ${BASE_ARG} -f ${{ env.dockerfile }} .
      docker tag ${{ env.docker-registry-container-sha }} ${{ env.docker-registry-container-latest }}
    shell: bash
    working-directory: source/${{ inputs.config-path }}
    if: env.docker-container-exists != 'true'
  - name: Publish container
    run: |
      docker push ${{ env.docker-registry-container-sha }}
      docker push ${{ env.docker-registry-container-latest }}
    shell: bash
    if: env.docker-container-exists != 'true' && github.event_name != 'pull_request'
