name: Build

on:
  push:
    branches:
      - main
      - release/*
  pull_request:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK for running Gradle
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - name: Build
        uses: gradle/gradle-build-action@v2
        with:
          arguments: build
          cache-read-only: ${{ github.event_name == 'pull_request' }}

      - name: Save unit test results
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: test-results
          path: jmx-metrics/build/reports/tests/test

  integration-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK for running Gradle
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - name: Integration test
        uses: gradle/gradle-build-action@v2
        with:
          arguments: integrationTest
          cache-read-only: ${{ github.event_name == 'pull_request' }}

      - name: Save integration test results
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: integration-test-results
          path: jmx-metrics/build/reports/tests/integrationTest

  markdown-link-check:
    # release branches are excluded to avoid unnecessary maintenance if external links break
    if: ${{ !startsWith(github.ref_name, 'release/') }}
    uses: ./.github/workflows/reusable-markdown-link-check.yml

  misspell-check:
    # release branches are excluded to avoid unnecessary maintenance if new misspellings are added
    # to the misspell dictionary
    if: ${{ !startsWith(github.ref_name, 'release/') }}
    uses: ./.github/workflows/reusable-misspell-check.yml

  publish-snapshots:
    # the condition is on the steps below instead of here on the job, because skipping the job
    # causes the job to show up as canceled in the GitHub UI which prevents the build section from
    # collapsing when everything (else) is green
    #
    # and the name is updated when the steps below are skipped which makes what's happening clearer
    # in the GitHub UI
    name: publish-snapshots${{ github.ref_name == 'main' && github.repository == 'open-telemetry/opentelemetry-java-contrib' && '' || ' (skipped)' }}
    needs:
      # intentionally not blocking snapshot publishing on markdown-link-check or misspell-check
      - build
      - integration-test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK for running Gradle
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - name: Build and publish snapshots
        uses: gradle/gradle-build-action@v2
        # skipping release branches because the versions in those branches are not snapshots
        # (also this skips pull requests)
        if: ${{ github.ref_name == 'main' && github.repository == 'open-telemetry/opentelemetry-java-contrib' }}
        with:
          arguments: assemble publishToSonatype
        env:
          SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
          SONATYPE_KEY: ${{ secrets.SONATYPE_KEY }}
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}

  required-status-check:
    needs:
        # markdown-link-check is not required so that pull requests will not be blocked if external
        # links break
        # similarly misspell-check is not required so that pull requests will not be blocked if new
        # misspellings are added to the misspell dictionary
      - build
      - integration-test
    runs-on: ubuntu-latest
    if: always()
    steps:
      - if: |
          needs.build.result != 'success' ||
          needs.integration-test.result != 'success'
        run: exit 1
