name: "Continuous Build"

on:
  push:
    branches:
      - main

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - macos-latest
          - ubuntu-18.04
        test-java-version:
          - 8
          - 11
          - 16
          - 17
        include:
          - os: ubuntu-18.04
            test-java-version: 11
            coverage: true
    steps:
      - uses: actions/checkout@v2.3.4
        with:
          fetch-depth: 0
      - id: setup-java-8
        name: Setup Java 8 for tests
        uses: actions/setup-java@v2
        with:
          distribution: adopt
          java-version: 8
          java-package: jre
      - id: setup-java-16
        name: Setup Java 16 for tests
        uses: actions/setup-java@v2
        with:
          distribution: adopt
          java-version: 16
      - id: setup-java-17
        name: Setup Java 17 for tests
        uses: actions/setup-java@v2
        with:
          # TODO(anuraaga): Switch to adopt when they release 17-ea
          distribution: zulu
          java-version: 17-ea
      - id: setup-java-11
        name: Setup Java 11
        uses: actions/setup-java@v2
        with:
          distribution: adopt
          java-version: 11
      - uses: burrunan/gradle-cache-action@v1.10
        with:
          remote-build-cache-proxy-enabled: false
          arguments: build --stacktrace ${{ matrix.coverage && 'jacocoTestReport' || '' }}
          properties: |
            testJavaVersion=${{ matrix.test-java-version }}
            org.gradle.java.installations.paths=${{ steps.setup-java-8.outputs.path }},${{ steps.setup-java-11.outputs.path }},${{ steps.setup-java-16.outputs.path }},${{ steps.setup-java-17.outputs.path }}
      - uses: codecov/codecov-action@v1.5.2
        if: ${{ matrix.coverage }}
      - uses: actions/upload-artifact@v2
        if: ${{ matrix.coverage }}
        with:
          name: coverage-report
          path: all/build/reports/jacoco/test/html
  publish-snapshots:
    name: Publish snapshots
    if: ${{ github.event_name == 'push' && github.repository == 'open-telemetry/opentelemetry-java' }}
    needs: build
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2.3.4
        with:
          fetch-depth: 0
      - id: setup-java-11
        name: Setup Java 11
        uses: actions/setup-java@v2
        with:
          distribution: adopt
          java-version: 11
      - uses: burrunan/gradle-cache-action@v1.10
        with:
          remote-build-cache-proxy-enabled: false
          arguments: snapshot --stacktrace
        env:
          SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
          SONATYPE_KEY: ${{ secrets.SONATYPE_KEY }}
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}
