# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Release

on:
  release:
    types: [ released, prereleased ]
    branches: [ '3.x' ]
    tags:
      - 'v3.*.*'   

jobs:
  build:

    runs-on: ubuntu-latest
    env:
      CI_BUILD_NUMBER: ${{ github.run_number }}
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 8
      uses: actions/setup-java@v2
      with:
        distribution: 'adopt'
        java-version: '8'
    - name: Cache Gradle packages
      uses: actions/cache@v2.1.5
      with:
        path: ~/.gradle/caches
        key: ${{ runner.os }}-gradle-${{ secrets.CACHE_VERSION }}-${{ hashFiles('**/*.gradle') }}
        restore-keys: ${{ runner.os }}-gradle-${{ secrets.CACHE_VERSION }}
    - name: Grant execute permission for gradlew
      run: chmod +x gradlew
    - name: Grant execute permission for push
      run: chmod +x push_javadoc.sh
    - name: Extract version tag
      run: echo "BUILD_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
    - name: Build and Release
      run: ./gradlew -PreleaseMode=full build --stacktrace --no-daemon
    - name: Upload to Codecov  
      uses: codecov/codecov-action@v1
    - name: Upload release
      run: ./gradlew -PreleaseMode=full javadocCleanup uploadArchives --no-daemon --no-parallel --stacktrace
      env:
        # Define secrets at https://github.com/ReactiveX/RxJava/settings/secrets/actions
        # ------------------------------------------------------------------------------ 
        ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_USER }}
        ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_PASSWORD }}
        ORG_GRADLE_PROJECT_SIGNING_PRIVATE_KEY: ${{ secrets.SIGNING_PRIVATE_KEY }}
        ORG_GRADLE_PROJECT_SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}
    - name: Publish release
      run: ./gradlew -PreleaseMode=full closeAndReleaseRepository --no-daemon --no-parallel --stacktrace
      env:
        # Define secrets at https://github.com/ReactiveX/RxJava/settings/secrets/actions
        # ------------------------------------------------------------------------------ 
        ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_USER }}
        ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_PASSWORD }}
    - name: Push Javadocs
      run: ./push_javadoc.sh
      env:
        # Define secrets at https://github.com/ReactiveX/RxJava/settings/secrets/actions
        # ------------------------------------------------------------------------------ 
        JAVADOCS_TOKEN: ${{ secrets.JAVADOCS_TOKEN }}
