name: 'Build'
description: 'Builds the project, optionally publishing it to a local deployment repository'
inputs:
  develocity-access-key:
    description: 'Access key for authentication with ge.spring.io'
    required: false
  java-distribution:
    description: 'Java distribution to use'
    required: false
    default: 'liberica'
  java-early-access:
    description: 'Whether the Java version is in early access'
    required: false
    default: 'false'
  java-toolchain:
    description: 'Whether a Java toolchain should be used'
    required: false
    default: 'false'
  java-version:
    description: 'Java version to compile and test with'
    required: false
    default: '17'
  opensource-repository-password:
    description: 'Password for authentication with the open-source repository'
    required: false
  opensource-repository-username:
    description: 'Username for authentication with the open-source repository'
    required: false
  publish:
    description: 'Whether to publish artifacts ready for deployment to Artifactory'
    required: false
    default: 'false'
outputs:
  build-scan-url:
    description: 'URL, if any, of the build scan produced by the build'
    value: ${{ (inputs.publish == 'true' && steps.publish.outputs.build-scan-url) || steps.build.outputs.build-scan-url }}
  version:
    description: 'Version that was built'
    value: ${{ steps.read-version.outputs.version }}
runs:
  using: composite
  steps:
    - name: Prepare Gradle Build
      uses: ./.github/actions/prepare-gradle-build
      with:
        develocity-access-key: ${{ inputs.develocity-access-key }}
        java-distribution: ${{ inputs.java-distribution }}
        java-early-access: ${{ inputs.java-early-access }}
        java-toolchain: ${{ inputs.java-toolchain }}
        java-version: ${{ inputs.java-version }}
    - name: Build
      id: build
      if: ${{ inputs.publish == 'false' }}
      shell: bash
      env:
        SPRING_REPOSITORY_PASSWORD: ${{ inputs.opensource-repository-password }}
        SPRING_REPOSITORY_USERNAME: ${{ inputs.opensource-repository-username }}
      run: ./gradlew check
    - name: Publish
      id: publish
      if: ${{ inputs.publish == 'true' }}
      shell: bash
      env:
        SPRING_REPOSITORY_PASSWORD: ${{ inputs.opensource-repository-password }}
        SPRING_REPOSITORY_USERNAME: ${{ inputs.opensource-repository-username }}
      run: ./gradlew -PdeploymentRepository=$(pwd)/deployment-repository build dist publishAllPublicationsToDeploymentRepository
    - name: Read Version From gradle.properties
      id: read-version
      shell: bash
      run: |
        version=$(sed -n 's/version=\(.*\)/\1/p' gradle.properties)
        echo "Version is $version"
        echo "version=$version" >> $GITHUB_OUTPUT
