name: CI build

on:
  push:
    branches:
      - main
      - v[0-9]+.[0-9]+.x
  workflow_dispatch:

jobs:
  build:
    uses: ./.github/workflows/build.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/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 }}

  testLatestDeps:
    # 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, 'v') }}
    uses: ./.github/workflows/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/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, 'v') }}
    uses: ./.github/workflows/muzzle.yml

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

  markdown-link-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Check markdown links
        # using retry because of sporadic external site failures
        uses: nick-invision/retry@v2.6.0
        with:
          # timing out has not been a problem
          timeout_minutes: 15
          # give external site some time to hopefully recover
          retry_wait_seconds: 120
          max_attempts: 5
          command: |
            npm install -g markdown-link-check
            find . -type f \
                   -name '*.md' \
                   -not -path './.github/*' \
                   -not -path './node_modules/*' \
                   -print0 \
              | xargs -0 -n1 markdown-link-check --config .github/scripts/markdown_link_check_config.json

  markdown-misspell-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Check markdown for common misspellings
        run: |
          curl -L -o ./install-misspell.sh https://git.io/misspell
          sh ./install-misspell.sh
          ./bin/misspell -error ./**/*

  snapshot:
    runs-on: ubuntu-latest
    # intentionally not blocking snapshot publishing on testLatestDeps
    # because any time a new library version is released to maven central
    # it can fail due to test code incompatibility with the new library version,
    # or due to slight changes in emitted telemetry
    #
    # also not blocking snapshot publishing on markdown-link-check because links to external urls
    # can break at any time
    needs: [ build, test, smoke-test, examples, muzzle, markdown-misspell-check ]
    if: ${{ github.ref_name == 'main' && github.repository == 'open-telemetry/opentelemetry-java-instrumentation' }}
    steps:
      - uses: actions/checkout@v3

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

      - 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

      - 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:
          arguments: build publishToSonatype
          build-root-directory: gradle-plugins
