name: SonarCloud

on: [push]

env:
  CCACHE_BASEDIR: ${{ github.workspace }}
  CCACHE_COMPRESS: true
  CCACHE_MAXSIZE: 200M
  OS_NAME: linux

jobs:
  pre-check:
    runs-on: ubuntu-latest
    outputs:
      should_skip: ${{ steps.skip-check.outputs.should_skip }}
    steps:
      - id: skip-check
        uses: fkirc/skip-duplicate-actions@master
        with:
          concurrent_skipping: 'same_content'

  sonarcloud:
    needs: pre-check
    if: ${{ needs.pre-check.outputs.should_skip != 'true' }}
    runs-on: ubuntu-latest
    env:
      TEST: sonarcloud
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/cache@v2
        with:
          path: |
            ~/.ccache
            ~/.sonar-cache
          key: ccache-sonarcloud-${{ github.ref }}:${{ github.sha }}
          restore-keys: |
            ccache-sonarcloud-${{ github.ref }}:
            ccache-sonarcloud-
      - run: |
          sudo apt-get install -qq ccache
          echo "PATH=/usr/lib/ccache:$PATH" >> $GITHUB_ENV
          ccache -z
      # using SonarSource/sonarcloud-github-action is currently not recommended
      # for C builds, so we follow the "any CI" instructions
      - name: Install sonar-scanner
        env:
          SONAR_SCANNER_VERSION: 4.4.0.2170
        run: |
          export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-$SONAR_SCANNER_VERSION-linux
          curl --create-dirs -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip
          unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/
          echo "SONAR_SCANNER_OPTS=-server" >> $GITHUB_ENV
          curl --create-dirs -sSLo $HOME/.sonar/build-wrapper-linux-x86.zip https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip
          unzip -o $HOME/.sonar/build-wrapper-linux-x86.zip -d $HOME/.sonar/
          echo "PATH=$HOME/.sonar/build-wrapper-linux-x86:$SONAR_SCANNER_HOME/bin:$PATH" >> $GITHUB_ENV
      - env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BUILD_NUMBER: ${{ github.run_id }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_PROJECT: ${{ secrets.SONAR_PROJECT }}
          SONAR_ORGANIZATION: ${{ secrets.SONAR_ORGANIZATION }}
        uses: ./.github/actions/default
      - run: ccache -s
