name: Build

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

jobs:
  assemble:
    uses: ./.github/workflows/reusable-assemble.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  test:
    uses: ./.github/workflows/reusable-test.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  test-latest-deps:
    # release branches are excluded
    # because any time a new library version is released to maven central it can fail
    # which requires unnecessary release branch maintenance, especially for patches
    if: ${{ !startsWith(github.ref_name, 'release/') }}
    uses: ./.github/workflows/reusable-test-latest-deps.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  smoke-test:
    uses: ./.github/workflows/reusable-smoke-test.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  muzzle:
    # release branches are excluded
    # because any time a new library version is released to maven central it can fail
    # which requires unnecessary release branch maintenance, especially for patches
    if: ${{ !startsWith(github.ref_name, 'release/') }}
    uses: ./.github/workflows/reusable-muzzle.yml

  gradle-plugins:
    uses: ./.github/workflows/reusable-gradle-plugins.yml

  examples:
    uses: ./.github/workflows/reusable-examples.yml

  markdown-link-check:
    # release branches are excluded to avoid unnecessary maintenance if external links break
    # (and also because the README.md javaagent download link has to be updated on release branches
    #  before the release download has been published)
    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:
    needs:
      # intentionally not blocking snapshot publishing on test-latest-deps, muzzle,
      # markdown-link-check, or misspell-check
      - assemble
      - test
      - smoke-test
      - gradle-plugins
      - examples
    runs-on: ubuntu-latest
    # skipping release branches because the versions in those branches are not snapshots
    if: ${{ github.ref_name == 'main' && github.repository == 'open-telemetry/opentelemetry-java-instrumentation' }}
    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 artifact snapshots
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
          SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
          SONATYPE_KEY: ${{ secrets.SONATYPE_KEY }}
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}
        uses: gradle/gradle-build-action@v2
        with:
          arguments: assemble publishToSonatype
          # gradle enterprise is used for the build cache
          gradle-home-cache-excludes: caches/build-cache-1

      - name: Build and publish gradle plugin snapshots
        env:
          GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
          SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
          SONATYPE_KEY: ${{ secrets.SONATYPE_KEY }}
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}
        uses: gradle/gradle-build-action@v2
        with:
          build-root-directory: gradle-plugins
          arguments: build publishToSonatype
          # gradle enterprise is used for the build cache
          gradle-home-cache-excludes: caches/build-cache-1
