diff --git a/.github/workflows/maven-sonar-analysis-new.yml b/.github/workflows/maven-sonar-analysis-new.yml index 1c4ccd19..bad0b1ab 100644 --- a/.github/workflows/maven-sonar-analysis-new.yml +++ b/.github/workflows/maven-sonar-analysis-new.yml @@ -4,16 +4,13 @@ on: workflow_call: inputs: SERVICE_LOCATION: - required: true + required: false type: string + default: '.' SONAR_URL: required: false type: string default: 'https://sonarcloud.io' - PROJECT_KEY: - required: false - type: string - default: "mosip_${{ github.event.repository.name }}" secrets: SONAR_TOKEN: required: true @@ -33,68 +30,62 @@ on: jobs: maven-sonar-analysis: runs-on: ubuntu-latest + steps: - uses: actions/checkout@v3 + - name: Set up JDK 21 uses: actions/setup-java@v3 with: - distribution: 'temurin' + distribution: temurin java-version: '21' - server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml - settings-path: ${{ github.workspace }} # location for the settings.xml file + server-id: ossrh + settings-path: ${{ github.workspace }} - - name: Cache local Maven repository + - name: Cache Maven uses: actions/cache@v4 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} - restore-keys: | - ${{ runner.os }}-maven- + restore-keys: ${{ runner.os }}-maven- - - name: Setup branch and env + - name: Setup env run: | - # Strip git ref prefix from version echo "BRANCH_NAME=$(echo ${{ github.ref }} | sed -e 's,.*/\(.*\),\1,')" >> $GITHUB_ENV echo "GPG_TTY=$(tty)" >> $GITHUB_ENV - echo "SONAR URL : ${{ inputs.SONAR_URL }}" - - name: Setup branch and GPG public key + - name: Import GPG run: | - # Strip git ref prefix from version - echo ${{ env.BRANCH_NAME }} - echo ${{ env.GPG_TTY }} sudo apt-get --yes install gnupg2 - gpg2 --import ./.github/keys/mosipgpgkey_pub.gpg - gpg2 --quiet --batch --passphrase=${{secrets.GPG_SECRET}} --allow-secret-key-import --import ./.github/keys/mosipgpgkey_sec.gpg + gpg2 --import ./.github/keys/mosipgpgkey_pub.gpg + gpg2 --quiet --batch --passphrase=${{ secrets.GPG_SECRET }} --allow-secret-key-import --import ./.github/keys/mosipgpgkey_sec.gpg - - name: Setup the settings file for ossrh server - run: echo " ossrh ${{secrets.OSSRH_USER}} ${{secrets.OSSRH_SECRET}} ossrh true gpg2 ${{secrets.GPG_SECRET}} allow-snapshots true snapshots-repo https://oss.sonatype.org/content/repositories/snapshots false true releases-repo https://oss.sonatype.org/service/local/staging/deploy/maven2 true false danubetech-maven-public https://repo.danubetech.com/repository/maven-public/ sonar . https://sonarcloud.io false " > $GITHUB_WORKSPACE/settings.xml - - #- name: Build with Maven - # run: cd ${{ inputs.SERVICE_LOCATION }} && mvn -B package --file pom.xml -s $GITHUB_WORKSPACE/settings.xml + - name: Setup Maven settings + run: | + echo " ossrh ${{secrets.OSSRH_USER}} ${{secrets.OSSRH_SECRET}} ossrh true gpg2 ${{secrets.GPG_SECRET}} " > $GITHUB_WORKSPACE/settings.xml - - name: Analyze with SonarCloud + - name: Sonar Analysis run: | - SERVICE_KEY=$(echo "${{ inputs.SERVICE_LOCATION }}" | tr '/' '-') - FULL_PROJECT_KEY="${{ inputs.PROJECT_KEY }}-${SERVICE_KEY}" - echo "Derived Project Key: ${FULL_PROJECT_KEY}" - cd ${{ inputs.SERVICE_LOCATION }} && \ + SERVICE_KEY=$(echo "${{ inputs.SERVICE_LOCATION }}" | sed 's|^\./||; s|^\.$||; s|[^a-zA-Z0-9]|-|g; s|-\\+|-|g; s|^-||; s|-$||') + FINAL_NAME="${{ github.event.repository.name }}${SERVICE_KEY:+-$SERVICE_KEY}" + FULL_PROJECT_KEY="${{ secrets.ORG_KEY }}_${FINAL_NAME}" + cd "${{ inputs.SERVICE_LOCATION || '.' }}" && \ mvn -U -B verify sonar:sonar \ -Dmaven.wagon.http.retryHandler.count=2 \ -Dsonar.projectKey=${FULL_PROJECT_KEY} \ -Dsonar.organization=${{ secrets.ORG_KEY }} \ -Dsonar.host.url=${{ inputs.SONAR_URL }} \ -Dsonar.token=${{ secrets.SONAR_TOKEN }} \ - -Dsonar.projectName=${{ github.event.repository.name }} \ + -Dsonar.projectName=${FINAL_NAME} \ --file pom.xml -s $GITHUB_WORKSPACE/settings.xml env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - # - uses: 8398a7/action-slack@v3 - # with: - # status: ${{ job.status }} - # fields: repo,message,author,commit,workflow,job # selectable (default: repo,message) - # env: - # SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required - # if: "${{ github.event_name != 'pull_request' && failure() }}" # Pick up events even if the job fails or is canceled. + #- uses: 8398a7/action-slack@v3 + # with: + # status: ${{ job.status }} + # fields: repo,message,author,commit,workflow,job + #env: + # SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + #if: "${{ github.event_name != 'pull_request' && failure() }}"