name: CI

on:
  workflow_dispatch:
  push:
    paths-ignore:
      - '.github/**'

jobs:
  build:
    timeout-minutes: 15
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - nickname: win
            os: windows-latest
            java: 17
          - nickname: macos
            os: macos-latest
            java: 17
          - nickname: linux
            os: ubuntu-latest
            java: 17
    name: CI Build ${{ matrix.nickname }}
    steps:
      - uses: actions/checkout@v4
      - uses: jvalkeal/setup-maven@v1
        with:
          maven-version: 3.6.3
      - uses: actions/setup-java@v4
        with:
          distribution: liberica
          java-version: ${{ matrix.java }}
# this takes minutes and stretches build time
#      - name: Setup docker (missing on MacOS)
#        if: runner.os == 'macos'
#        run: |
#          brew install docker
#          colima start
#
#          # For testcontainers to find the Colima socket
#          # https://github.com/abiosoft/colima/blob/main/docs/FAQ.md#cannot-connect-to-the-docker-daemon-at-unixvarrundockersock-is-the-docker-daemon-running
#          sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
      - name: Verify formatting
        run:  ./mvnw  -B -Pfunctional-tests spring-javaformat:validate
      - name: Build (Windows and MacOS, no functional tests)
        if: runner.os != 'Linux'
        run: ./mvnw -B clean install
      - name: Build (Linux)
        if: runner.os == 'Linux'
        run: ./mvnw -B -Pfunctional-tests clean install
  deploy:
    needs: build
    if: |
      github.ref_name == 'main' &&
      github.repository == 'spring-projects/spring-rewrite-commons'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: liberica
          java-version: 17
          cache: maven
      - uses: jfrog/setup-jfrog-cli@v3
        env:
          JF_ENV_SPRING: ${{ secrets.JF_ARTIFACTORY_SPRING }}

      - name: Deploy Snapshot
        run: |
          
          jf mvnc \
          --server-id-deploy repo.spring.io \
          --repo-deploy-releases release \
          --repo-deploy-snapshots snapshot \
          --exclude-patterns="*-example*.*,*-tests-*.*,*-docs-*.*"
          echo JFROG_CLI_BUILD_NAME=spring-rewrite-commons-main >> $GITHUB_ENV
          echo JFROG_CLI_BUILD_NUMBER=$GITHUB_RUN_NUMBER >> $GITHUB_ENV

          jf mvn clean -f ./pom.xml install -B -Pjavadoc -DskipTests
          jf rt build-publish