Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure SonarCloud #9

Open
nejch opened this issue Feb 6, 2023 · 7 comments
Open

Configure SonarCloud #9

nejch opened this issue Feb 6, 2023 · 7 comments

Comments

@nejch
Copy link
Member

nejch commented Feb 6, 2023

Suggested in sonarcloud settings:

pom.xml

<properties>
  <sonar.organization>siemens</sonar.organization>
  <sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>

.github/workflows/build.yml or similar:

name: SonarCloud
on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  build:
    name: Build and analyze
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Cache SonarCloud packages
        uses: actions/cache@v1
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      - name: Build and analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=siemens_cmp-ra-component
@ralienpp
Copy link
Collaborator

ralienpp commented Feb 6, 2023

It seems that the SONAR_TOKEN environment variable hasn't been defined, based on this log output:

https://github.com/siemens/cmp-ra-component/actions/runs/4105274548/jobs/7081902882#step:6:2011

@nejch
Copy link
Member Author

nejch commented Feb 6, 2023

It seems that the SONAR_TOKEN environment variable hasn't been defined, based on this log output:

https://github.com/siemens/cmp-ra-component/actions/runs/4105274548/jobs/7081902882#step:6:2011

@ralienpp the token is there, but only available for actions in this project, not forks. This is by design in GitHub Secrets. Sorry, thought you had Collaborator access here - you can still request that in oss-community-management.

GitHub secrets:

Anyone with collaborator access to this repository can use these secrets and variables for actions. They are not passed to workflows that are triggered by a pull request from a fork.

So this will not work for external contributions. I'm not sure if this is still useful to you in that case. See also https://community.sonarsource.com/t/sonar-cannot-be-run-on-pr-from-a-fork/69229 if you'd like to track it upstream in SonarCloud.

image

@ralienpp
Copy link
Collaborator

ralienpp commented Feb 8, 2023

Resolved in #10

@nejch
Copy link
Member Author

nejch commented Feb 11, 2023

@ralienpp @Akretsch just a note - this is not actually solved now, it will only work for PRs from this repo but always fail for external PRs (as you can see with failing dependabot PRs).

I'd say you could either restrict the sonar job to just run on main, configure it to run only on PRs from this project, or use the workaround linked in the comment above if you want to really use this.

@ralienpp
Copy link
Collaborator

You're right, I need to think about it more.

@Akretsch
Copy link
Collaborator

closed as requested by @ralienpp

@Akretsch
Copy link
Collaborator

reopened as requested by @ralienpp

@Akretsch Akretsch reopened this Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants