# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: ci-workflow

on: [push, pull_request]

env:
  DEFAULT_MAVEN_OPTS: >-
    -Xmx3g
    -XX:ReservedCodeCacheSize=1g
    -XX:+UseG1GC
    -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN
    -Dhttp.keepAlive=false
    -Dmaven.wagon.http.pool=false
  MAVEN_BUILD_COMMAND: >-
    mvn package verify
    -V
    -D dir-only
    -D disableXmlReport
    -nsu
    -ntp
    -ff
  MAVEN_BUILD_PROFILES: >-
    -P include-grpc
    -P skip-nifi-bin-assembly
  MAVEN_PROJECTS: >-
    -pl -minifi/minifi-assembly
    -pl -minifi/minifi-c2/minifi-c2-assembly
    -pl -minifi/minifi-toolkit/minifi-toolkit-assembly
    -pl -nifi-registry/nifi-registry-assembly
    -pl -nifi-registry/nifi-registry-toolkit/nifi-registry-toolkit-assembly
    -pl -nifi-stateless/nifi-stateless-assembly
    -pl -nifi-toolkit/nifi-toolkit-assembly

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  static-analysis:
    timeout-minutes: 30
    name: Static Analysis
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Cache Maven Modules
        uses: actions/cache@v3
        with:
          path: |
            ~/.m2/repository
          # Cache Maven modules using a cache key different from setup-java steps
          key: ${{ runner.os }}-maven-static-analysis-${{ hashFiles('**/pom.xml') }}
      - name: Set up Java 17
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '17'
      - name: Maven Build
        run: >
          mvn validate
          --no-snapshot-updates
          --no-transfer-progress
          --fail-fast
          -P contrib-check
          -P include-grpc

  ubuntu-build-en:
    timeout-minutes: 120
    runs-on: ubuntu-latest
    name: Ubuntu Zulu JDK 17 EN
    steps:
      - name: System Information
        run: |
          hostname
          cat /proc/cpuinfo
          cat /proc/meminfo
          df
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Cache Node Modules
        uses: actions/cache@v3
        with:
          path: |
            ~/.npm
            **/node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
      - name: Set up Java 17
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '17'
          cache: 'maven'
      - name: Maven Build
        env:
          NIFI_CI_LOCALE: >-
            -Duser.language=en
            -Duser.country=AU
          SUREFIRE_OPTS: >-
            -Duser.language=en
            -Duser.country=AU
            -Duser.region=AU
            -Duser.timezone=Australia/Melbourne
          MAVEN_OPTS: >-
            ${{ env.DEFAULT_MAVEN_OPTS }}
            -DargLine=${env.SUREFIRE_OPTS}
        run: >
          ${{ env.MAVEN_BUILD_COMMAND }}
          ${{ env.MAVEN_BUILD_PROFILES }}
          ${{ env.MAVEN_PROJECTS }}
      - name: Upload Test Reports
        uses: actions/upload-artifact@v3
        with:
          name: surefire-reports-ubuntu-17
          path: |
            ./**/target/surefire-reports/*.txt
            ./**/target/surefire-reports/*.xml
          retention-days: 3
        if: failure()
      - name: Post Disk Usage
        run: df

  ubuntu-build-hi:
    timeout-minutes: 120
    runs-on: ubuntu-latest
    name: Ubuntu Zulu JDK 11 HI
    steps:
      - name: System Information
        run: |
          hostname
          cat /proc/cpuinfo
          cat /proc/meminfo
          df
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Cache Node Modules
        uses: actions/cache@v3
        with:
          path: |
            ~/.npm
            **/node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
      - name: Set up Java 11
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '11'
          cache: 'maven'
      - name: Maven Build
        env:
          NIFI_CI_LOCALE: >-
            -Duser.language=hi
            -Duser.country=IN
          SUREFIRE_OPTS: >-
            -Duser.language=hi
            -Duser.country=IN
            -Duser.region=IN
            -Duser.timezone=Asia/Kolkata
          MAVEN_OPTS: >-
            ${{ env.DEFAULT_MAVEN_OPTS }}
            -DargLine=${env.SUREFIRE_OPTS}
        run: >
          ${{ env.MAVEN_BUILD_COMMAND }}
          ${{ env.MAVEN_BUILD_PROFILES }}
          ${{ env.MAVEN_PROJECTS }}
      - name: Upload Test Reports
        uses: actions/upload-artifact@v3
        with:
          name: surefire-reports-ubuntu-en
          path: |
            ./**/target/surefire-reports/*.txt
            ./**/target/surefire-reports/*.xml
          retention-days: 3
        if: failure()
      - name: Post Disk Usage
        run: df

  macos-build-jp:
    timeout-minutes: 120
    runs-on: macos-latest
    name: MacOS Temurin JDK 8 JP
    steps:
      - name: System Information
        run: |
          hostname
          top -l 1 | grep PhysMem
          sysctl machdep.cpu
          df
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Cache Node Modules
        uses: actions/cache@v3
        with:
          path: |
            ~/.npm
            **/node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
      - name: Set up Java 8
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '8'
          cache: 'maven'
      - name: Maven Build
        env:
          NIFI_CI_LOCALE: >-
            -Duser.language=ja
            -Duser.country=JP
          SUREFIRE_OPTS: >-
            -Duser.language=ja
            -Duser.country=JP
            -Duser.region=JP
            -Duser.timezone=Asia/Tokyo
          MAVEN_OPTS: >-
            ${{ env.DEFAULT_MAVEN_OPTS }}
            -DargLine=${env.SUREFIRE_OPTS}
        run: >-
          ${{ env.MAVEN_BUILD_COMMAND }}
          ${{ env.MAVEN_BUILD_PROFILES }}
          ${{ env.MAVEN_PROJECTS }}
      - name: Upload Test Reports
        uses: actions/upload-artifact@v3
        with:
          name: surefire-reports-macos-jp
          path: |
            ./**/target/surefire-reports/*.txt
            ./**/target/surefire-reports/*.xml
          retention-days: 3
        if: failure()
      - name: Post Disk Usage
        run: df

  windows-build:
    timeout-minutes: 120
    runs-on: windows-latest
    name: Windows Zulu JDK 8 FR
    steps:
      - name: System Information
        run: |
          systeminfo
          df
      - name: Setup Git
        run:  |
          git config --global core.autocrlf false
          git config --global core.longpaths true
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Get NPM Cache Directory
        id: npm-cache-directory
        run: |
          echo "::set-output name=directory::$(npm config get cache)"
      - name: Cache Node Modules
        uses: actions/cache@v3
        with:
          path: |
            ${{ steps.npm-cache-directory.outputs.directory }}
            **/node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
      - name: Set up Java 8
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '8'
          cache: 'maven'
      - name: Maven Build
        env:
          NIFI_CI_LOCALE: >-
            -Duser.language=fr
            -Duser.country=FR
          SUREFIRE_OPTS: >-
            -Duser.language=fr
            -Duser.country=FR
            -Duser.region=FR
            -Duser.timezone=Europe/Paris
          MAVEN_OPTS: >-
            ${{ env.DEFAULT_MAVEN_OPTS }}
            -DargLine=${env.SUREFIRE_OPTS}
        run: >-
          ${{ env.MAVEN_BUILD_COMMAND }}
          ${{ env.MAVEN_BUILD_PROFILES }}
          ${{ env.MAVEN_PROJECTS }}
      - name: Upload Test Reports
        uses: actions/upload-artifact@v3
        with:
          name: surefire-reports-windows-fr
          path: |
            ./**/target/surefire-reports/*.txt
            ./**/target/surefire-reports/*.xml
          retention-days: 3
        if: failure()
      - name: Post Disk Usage
        run: df