name: Build Test Push
description: Build, test and push Docker images for a given platform
author: Julien Neuhart

inputs:
  github_token:
    description: The GitHub token
    required: true
    default: ${{ github.token }}
  docker_hub_username:
    description: The Docker Hub username
    required: true
  docker_hub_password:
    description: The Docker Hub password
    required: true
  platform:
    description: linux/amd64, linux/ppc64le, linux/386, linux/arm64, linux/arm/v7
    required: true
  version:
    description: Gotenberg version
    required: true
  skip_integrations_tests:
    description: Define whether to skip integration testing
    default: false
  alternate_repository:
    description: Alternate repository to push the tags to
  dry_run:
    description: Dry run this action

outputs:
  tags:
    description: Comma separated list of full variant tags
    value: ${{ steps.build.outputs.tags }}
  tags_chromium:
    description: Comma separated list of Chromium variant tags
    value: ${{ steps.build.outputs.tags_chromium }}
  tags_libreoffice:
    description: Comma separated list of LibreOffice variant tags
    value: ${{ steps.build.outputs.tags_libreoffice }}
  tags_cloud_run:
    description: Comma separated list of Cloud Run full variant tags (linux/amd64 only)
    value: ${{ steps.build.outputs.tags_cloud_run }}
  tags_cloud_run_chromium:
    description: Comma separated list of Cloud Run Chromium variant tags (linux/amd64 only)
    value: ${{ steps.build.outputs.tags_cloud_run_chromium }}
  tags_cloud_run_libreoffice:
    description: Comma separated list of Cloud Run LibreOffice variant tags (linux/amd64 only)
    value: ${{ steps.build.outputs.tags_cloud_run_libreoffice }}
  tags_aws_lambda:
    description: Comma separated list of AWS Lambda full variant tags (linux/amd64 and linux/arm64 only)
    value: ${{ steps.build.outputs.tags_aws_lambda }}
  tags_aws_lambda_chromium:
    description: Comma separated list of AWS Lambda Chromium variant tags (linux/amd64 and linux/arm64 only)
    value: ${{ steps.build.outputs.tags_aws_lambda_chromium }}
  tags_aws_lambda_libreoffice:
    description: Comma separated list of AWS Lambda LibreOffice variant tags (linux/amd64 and linux/arm64 only)
    value: ${{ steps.build.outputs.tags_aws_lambda_libreoffice }}

runs:
  using: composite
  steps:
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v3

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Check out code
      uses: actions/checkout@v6

    - name: Log in to Docker Hub
      if: inputs.docker_hub_username != ''
      uses: docker/login-action@v3
      with:
        username: ${{ inputs.docker_hub_username }}
        password: ${{ inputs.docker_hub_password }}

    - name: Build ${{ inputs.platform }}
      id: build
      shell: bash
      env:
        INPUT_VERSION: ${{ inputs.version }}
        INPUT_PLATFORM: ${{ inputs.platform }}
        INPUT_ALTERNATE_REPOSITORY: ${{ inputs.alternate_repository }}
        INPUT_DRY_RUN: ${{ inputs.dry_run }}
      run: |
        .github/actions/build-test-push/build.sh \
          --version "$INPUT_VERSION" \
          --platform "$INPUT_PLATFORM" \
          --alternate-repository "$INPUT_ALTERNATE_REPOSITORY" \
          --dry-run "$INPUT_DRY_RUN"

    - name: Run integration tests
      if: inputs.skip_integrations_tests != 'true'
      shell: bash
      env:
        INPUT_VERSION: ${{ inputs.version }}
        INPUT_PLATFORM: ${{ inputs.platform }}
        INPUT_ALTERNATE_REPOSITORY: ${{ inputs.alternate_repository }}
        INPUT_DRY_RUN: ${{ inputs.dry_run }}
      run: |
        .github/actions/build-test-push/test.sh \
          --version "$INPUT_VERSION" \
          --platform "$INPUT_PLATFORM" \
          --alternate-repository "$INPUT_ALTERNATE_REPOSITORY" \
          --dry-run "$INPUT_DRY_RUN"

    - name: Push
      if: inputs.docker_hub_username != ''
      shell: bash
      env:
        INPUT_TAGS: ${{ steps.build.outputs.tags }},${{ steps.build.outputs.tags_chromium }},${{ steps.build.outputs.tags_libreoffice }},${{ steps.build.outputs.tags_cloud_run }},${{ steps.build.outputs.tags_cloud_run_chromium }},${{ steps.build.outputs.tags_cloud_run_libreoffice }},${{ steps.build.outputs.tags_aws_lambda }},${{ steps.build.outputs.tags_aws_lambda_chromium }},${{ steps.build.outputs.tags_aws_lambda_libreoffice }}
        INPUT_DRY_RUN: ${{ inputs.dry_run }}
      run: |
        .github/actions/build-test-push/push.sh \
          --tags "$INPUT_TAGS" \
          --dry-run "$INPUT_DRY_RUN"

    - name: Outputs
      shell: bash
      run: |
        echo "tags=${{ steps.build.outputs.tags }}"
        echo "tags_chromium=${{ steps.build.outputs.tags_chromium }}"
        echo "tags_libreoffice=${{ steps.build.outputs.tags_libreoffice }}"
        echo "tags_cloud_run=${{ steps.build.outputs.tags_cloud_run }}"
        echo "tags_cloud_run_chromium=${{ steps.build.outputs.tags_cloud_run_chromium }}"
        echo "tags_cloud_run_libreoffice=${{ steps.build.outputs.tags_cloud_run_libreoffice }}"
        echo "tags_aws_lambda=${{ steps.build.outputs.tags_aws_lambda }}"
        echo "tags_aws_lambda_chromium=${{ steps.build.outputs.tags_aws_lambda_chromium }}"
        echo "tags_aws_lambda_libreoffice=${{ steps.build.outputs.tags_aws_lambda_libreoffice }}"
