name: 'Docker Run Action'
description: 'Run a command in the web container inside a full environment'
inputs:
  version:
    description: 'The version of the image to run. '
    required: true
    default: 'local'
  digest:
    description: 'The build digest of the image to run. Overrides version.'
    required: true
    default: ''
  run:
    description: 'Run command in container'
    required: true
  logs:
    description: 'Show logs'
    required: false
  target:
    description: 'Docker target to run (development|production)'
    required: false
    default: 'production'
  deps:
    description: 'Which dependencies to install at runtime? (development|production)'
    required: false
    default: 'production'

runs:
  using: 'composite'
  steps:
    - name: Run Docker Container
      id: run
      continue-on-error: true
      shell: bash
      env:
        version: ${{ inputs.version }}
        digest: ${{ inputs.digest }}
        target: ${{ inputs.target }}
        deps: ${{ inputs.deps }}
        run: ${{ inputs.run }}
      run: |
        # Start the specified services
        make up \
          DOCKER_VERSION="${version}" \
          DOCKER_DIGEST="${digest}" \
          DOCKER_TARGET="${target}" \
          OLYMPIA_UID="$(id -u)" \
          OLYMPIA_DEPS="${deps}" \
          SKIP_DATA_SEED="true" \
          DOCKER_WAIT="true"
        docker compose exec --user olympia web bash -c "${run}"

    - name: Post Run (logs and exit code)
      if: always()
      shell: bash
      env:
        logs: ${{ inputs.logs }}
        outcome: ${{ steps.run.outcome }}
      run: |
        if [[ "$logs" == "true" ]]; then
          docker compose logs
        fi

        # If the run command failed, exit with a non-zero exit code
        if [ "$outcome" != 'success' ]; then
          exit 1
        fi
