|
4 | 4 |
|
5 | 5 | name: SonarCloud Analysis |
6 | 6 |
|
7 | | -on: push |
| 7 | +on: |
| 8 | + workflow_run: |
| 9 | + workflows: [SonarCloud Build] |
| 10 | + types: [completed] |
8 | 11 |
|
9 | 12 | jobs: |
10 | | - build: |
11 | | - name: Build |
| 13 | + sonarcloud: |
| 14 | + name: SonarCloud |
12 | 15 | runs-on: ubuntu-latest |
| 16 | + if: github.event.workflow_run.conclusion == 'success' |
13 | 17 | timeout-minutes: 15 |
14 | | - |
15 | | - if: ${{ (github.event_name != 'pull_request_target' && github.actor != 'dependabot[bot]') || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }} |
16 | 18 | steps: |
17 | | - - name: Checkout |
18 | | - uses: actions/checkout@v3 |
19 | | - with: |
20 | | - fetch-depth: 0 |
21 | | - |
22 | | - - name: Cache SonarCloud packages |
23 | | - uses: actions/cache@v3 |
24 | | - with: |
25 | | - path: ~/.sonar/cache |
26 | | - key: ${{ runner.os }}-sonar |
27 | | - restore-keys: ${{ runner.os }}-sonar |
28 | | - |
29 | | - - name: Set up JDK 17 |
30 | | - uses: actions/setup-java@v3 |
31 | | - with: |
32 | | - distribution: 'zulu' |
33 | | - java-version: '17' |
34 | | - cache: 'maven' |
| 19 | + - name: echo event |
| 20 | + run: cat $GITHUB_EVENT_PATH |
| 21 | + - name: Download PR number artifact |
| 22 | + if: github.event.workflow_run.event == 'pull_request' |
| 23 | + uses: dawidd6/action-download-artifact@v2 |
| 24 | + with: |
| 25 | + workflow: SonarCloud Build |
| 26 | + run_id: ${{ github.event.workflow_run.id }} |
| 27 | + name: PR_NUMBER |
| 28 | + - name: Read PR_NUMBER.txt |
| 29 | + if: github.event.workflow_run.event == 'pull_request' |
| 30 | + id: pr_number |
| 31 | + uses: juliangruber/read-file-action@v1 |
| 32 | + with: |
| 33 | + path: ./PR_NUMBER.txt |
| 34 | + - name: Request GitHub API for PR data |
| 35 | + if: github.event.workflow_run.event == 'pull_request' |
| 36 | + |
| 37 | + id: get_pr_data |
| 38 | + with: |
| 39 | + route: GET /repos/{full_name}/pulls/{number} |
| 40 | + number: ${{ steps.pr_number.outputs.content }} |
| 41 | + full_name: ${{ github.event.repository.full_name }} |
| 42 | + env: |
| 43 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 44 | + - uses: actions/checkout@v3 |
| 45 | + with: |
| 46 | + repository: ${{ github.event.workflow_run.head_repository.full_name }} |
| 47 | + ref: ${{ github.event.workflow_run.head_branch }} |
| 48 | + fetch-depth: 0 |
| 49 | + - name: Checkout base branch |
| 50 | + if: github.event.workflow_run.event == 'pull_request' |
| 51 | + run: | |
| 52 | + git remote add upstream ${{ github.event.repository.clone_url }} |
| 53 | + git fetch upstream |
| 54 | + git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} |
| 55 | + git checkout ${{ github.event.workflow_run.head_branch }} |
| 56 | + git clean -ffdx && git reset --hard HEAD |
| 57 | + - name: Cache SonarCloud packages |
| 58 | + uses: actions/cache@v3 |
| 59 | + with: |
| 60 | + path: ~/.sonar/cache |
| 61 | + key: ${{ runner.os }}-sonar |
| 62 | + restore-keys: ${{ runner.os }}-sonar |
35 | 63 |
|
36 | | - - name: Set Common Sonar Variables |
37 | | - id: sonar_env |
38 | | - run: | |
39 | | - echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \ |
40 | | - -Dsonar.projectKey=com-pas_compas-sct \ |
41 | | - -Dsonar.organization=com-pas )" |
42 | | - - name: Create custom Maven Settings.xml |
43 | | - uses: whelk-io/maven-settings-xml-action@v21 |
44 | | - with: |
45 | | - output_file: custom_maven_settings.xml |
46 | | - servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]' |
| 64 | + - name: Set up JDK 17 |
| 65 | + uses: actions/setup-java@v3 |
| 66 | + with: |
| 67 | + distribution: 'zulu' |
| 68 | + java-version: '17' |
| 69 | + cache: 'maven' |
47 | 70 |
|
48 | | - - name: Build and analyze (Pull Request) |
49 | | - if: ${{ github.event_name == 'pull_request' || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }} |
50 | | - env: |
51 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
52 | | - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
53 | | - run: | |
54 | | - mvn -B -s custom_maven_settings.xml \ |
55 | | - ${{ steps.sonar_env.outputs.sonar_opts }} \ |
56 | | - -Dsonar.pullrequest.branch=${{ github.ref_name }} \ |
57 | | - -Dsonar.pullrequest.key=${{ github.event.pull_request.number }} \ |
58 | | - -Dsonar.pullrequest.base=${{ github.base_ref }} \ |
59 | | - -Dsonar.scm.revision=${{ github.event.pull_request.head.sha }} \ |
60 | | - clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar |
61 | | - - name: Build and analyze (Push) |
62 | | - if: ${{ github.event_name == 'push' }} |
63 | | - env: |
64 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
65 | | - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
66 | | - run: | |
67 | | - mvn -B -s custom_maven_settings.xml \ |
68 | | - ${{ steps.sonar_env.outputs.sonar_opts }} \ |
69 | | - -Dsonar.branch.name=${{ github.ref_name }} \ |
70 | | - clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar |
| 71 | + - name: Set Common Sonar Variables |
| 72 | + id: sonar_env |
| 73 | + run: | |
| 74 | + echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \ |
| 75 | + -Dsonar.projectKey=com-pas_compas-sct \ |
| 76 | + -Dsonar.organization=com-pas )" |
| 77 | + - name: Create custom Maven Settings.xml |
| 78 | + uses: whelk-io/maven-settings-xml-action@v21 |
| 79 | + with: |
| 80 | + output_file: custom_maven_settings.xml |
| 81 | + servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]' |
| 82 | + - name: Build and analyze (Pull Request) |
| 83 | + if: ${{ github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target') }} |
| 84 | + env: |
| 85 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 86 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 87 | + run: | |
| 88 | + mvn -B -s custom_maven_settings.xml \ |
| 89 | + ${{ steps.sonar_env.outputs.sonar_opts }} \ |
| 90 | + -Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} \ |
| 91 | + -Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} \ |
| 92 | + -Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} \ |
| 93 | + -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \ |
| 94 | + clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar |
| 95 | + - name: Build and analyze (Push) |
| 96 | + if: ${{ github.event.workflow_run.event == 'push' }} |
| 97 | + env: |
| 98 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 100 | + run: | |
| 101 | + mvn -B -s custom_maven_settings.xml \ |
| 102 | + ${{ steps.sonar_env.outputs.sonar_opts }} \ |
| 103 | + -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \ |
| 104 | + -Dsonar.branch.name=${{ github.event.workflow_run.head_branch }} \ |
| 105 | + clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar |
0 commit comments