Implmenting SonarScanner in Swift project with GitHub action

Must-share information (formatted with Markdown):

  • which versions are you using: SonarQube (Enterprise Edition Version 9.9.1 (build 69595))
  • How is SonarQube deployed: zip, Docker, Helm: Not confirmed
  • what are you trying to achieve: Implement sonarQube to analyze Swift project through GitHubAction
  • what have you tried so far to achieve this:
  1. added sonar-project.properties to the root project (with project key and few additional keys)
  2. added build.yml with the below script:
name: Build

on:
  push:
    branches:
      - universal-develop
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  build:
    name: Build
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis

      - name: Download and install the build wrapper
        run: |
          mkdir $HOME/.sonar
          curl -sSLo $HOME/.sonar/build-wrapper-macosx-x86.zip ${{ secrets.SONAR_HOST_URL }}/static/cpp/build-wrapper-macosx-x86.zip
          unzip -o $HOME/.sonar/build-wrapper-macosx-x86.zip -d $HOME/.sonar/
        env:
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

      - name: Download and install the SonarScanner
        run: |
          curl -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472-macosx.zip
          unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/

      - name: Build and analyse the project
        run: |
          # Potential improvement : add these paths to the PATH env var.
          pod install
          $HOME/.sonar/build-wrapper-macosx-x86/build-wrapper-macosx-x86 --out-dir bw-output xcodebuild -scheme <scheme_name> -workspace <workspace_name>.xcworkspace -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.2' clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
          $HOME/.sonar/sonar-scanner-4.6.2.2472-macosx/bin/sonar-scanner -Dsonar.cfamily.build-wrapper-output=bw-output
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

The script failed with message: Process completed with exit code 65.
logs_58.zip (813.2 KB)
** logs are too long, so I am attaching the log files.

I tried to implement sonar scanner in my Swift project through GitHub Action, but couldn’t make it work with lots of hit and trial. On lots of threads I got to know that with Swift only project I do not need to implement a build wrapper, but coudl’t find any specific documentation on how to do it for Swift.

Any help in this regard will be highly appriciated.

Hi,

First, you don’t need the build-wrapper to analyze Swift.

Second, I’m having a hard time finding the actual analysis in the logs you’ve sent. Can you check that?

 
Thx,
Ann

Hi Ann,

Then what should I update my workflow with?
And yes, exactly my point, I don’t see any error in the logs, yet the workflow fails with exit code 65.

Hi,

Digging deeper into your log zip, yes, it fails with an exit code 65:

2024-01-30T06:55:08.6856890Z The following build commands failed:
2024-01-30T06:55:08.6880960Z 	SwiftCompile normal x86_64 Compiling\ FeedDataTransformer.swift,\ EventType+extension.swift,\ ContentTranslation.swift,\ ScheduledDirectAwardInfoModel.swift,\ FeedPoll.swift,\ ProfileSettingsDataProvider.swift,\ ProfileSettingsManager.swift,\ SurveyQuestionUIModel.swift,\ HarnessTargetInfo.swift,\ ListenerSuiteDataProvider.swift,\ DashboardEnums.swift,\ MemberModel.swift,\ SFAppConfig.swift,\ SFPrivateSiteRequestDetail.swift,\ NewBaseRequestFactory.swift,\ RecognitionHubDataModel.swift,\ SlackInfo.swift,\ NewsLetterDataProvider.swift,\ AppSecretKeys.swift,\ Mention.swift,\ AppUpdateType.swift,\ RecoHubRequestFactory.swift,\ SurveyQuestion.swift,\ AuthenticationClient.swift,\ Simpplr+RemoteNotification.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/Feed/FeedDataTransformer.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/FeatureFlag/Harness/EventType+extension.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/Content/Model/ContentTranslation.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/RecognitionHub/DataModel/ScheduledDirectAwardInfoModel.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/Feed/Model/FeedPoll.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/People/ProfileSettings/ProfileSettingsDataProvider.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/People/ProfileSettings/Manager/ProfileSettingsManager.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/Survey/Models/SurveyQuestionUIModel.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/FeatureFlag/Harness/HarnessTargetInfo.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/ListenerSuite/ListenerSuiteDataProvider.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/SFCore/Enums/DashboardEnums.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/People/Model/MemberModel.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/SFCore/Modules/Config/Model/SFAppConfig.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/SFCore/Modules/Site/SFPrivateSiteRequestDetail.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Common/NetworkClient/NewBaseRequestFactory.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/RecognitionHub/DataModel/RecognitionHubDataModel.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/People/Model/SlackInfo.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Modules/NewsLetter/NewsLetterDataProvider.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Common/AppSecrets/AppSecretKeys.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Common/ThirdParty/Mention.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Zeus/Enums/AppUpdateType.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/CommonRecognitionHub/RequestFactory/RecoHubRequestFactory.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/Survey/Models/SurveyQuestion.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Common/AuthClient/AuthenticationClient.swift /Users/runner/work/w2nma-core/w2nma-core/Simpplr/SimpplrExtensinos/Simpplr+RemoteNotification.swift (in target 'Simpplr' from project 'Simpplr')
2024-01-30T06:55:08.6906160Z 	CompileSwift normal x86_64 /Users/runner/work/w2nma-core/w2nma-core/Simpplr/Modules/CommonRecognitionHub/RequestFactory/RecoHubRequestFactory.swift (in target 'Simpplr' from project 'Simpplr')

Compile fails. That has nothing to do with analysis. You’ll need to fix your compilation before you can analyze (which explains why I didn’t find analysis logs the last time I looked at this).

 
Ann

Hi Ann,

The source code compiles just fine if I run the below command on the terminal after pod install.

xcodebuild -scheme Simpplr -workspace Simpplr-Framework.xcworkspace -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.2' clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO

I have very limited knowledge on build wrapper, could you please recheck the script used in the provided workflow, specially last step:

Build and analyse the project

As you said the swift project doesn’t need build-wrapper, could you please guide me on what should be used instead? I couldn’t find anything specific for swift in the documents, or I might be looking at the wrong place.

Hi,

You don’t need build-wrapper for Swift.

Completely remove build-wrapper, and all uses / references.

 
Ann

Hi Ann,

If you look at the last step in workflow it contains 3 statements one is pod install, and other 2 are related to build wrapper.

- name: Build and analyse the project
        run: |
          # Potential improvement : add these paths to the PATH env var.
          pod install
          $HOME/.sonar/build-wrapper-macosx-x86/build-wrapper-macosx-x86 --out-dir bw-output xcodebuild -scheme <scheme_name> -workspace <workspace_name>.xcworkspace -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.2' clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
          $HOME/.sonar/sonar-scanner-4.6.2.2472-macosx/bin/sonar-scanner -Dsonar.cfamily.build-wrapper-output=bw-output

Now, if I remove the last 2 statements, how sonar is gonna analyze the swift project? What should I put into workflow then?
I am very new to SonarQube, it is my first time integrating it on a swift project and I can’t find any related document. Kindly help.

Hi,

You don’t need the build-wrapper. You do need the scanner. So:

Note that your scanner version has some age on it at this point. You should upgrade to the latest version, which is 5.0.1, currently.

 
HTH,
Ann

Hi Ann,

It’s still getting failed.

Build
Process completed with exit code 127.

logs_66.zip (61.2 KB)

Hi,

Here’s the failure

2024-02-07T05:12:03.9919170Z e[36;1m$HOME/.sonar/sonar-scanner-5.0.1.3006-macosx/bin/sonar-scannere[0m
2024-02-07T05:12:04.0053910Z shell: /bin/bash -e {0}
2024-02-07T05:12:04.0054400Z env:
2024-02-07T05:12:04.0055120Z   SONAR_TOKEN: ***
2024-02-07T05:12:04.0055660Z   SONAR_HOST_URL: ***
2024-02-07T05:12:04.0056090Z ##[endgroup]
2024-02-07T05:12:04.0203000Z ##[debug]/bin/bash -e /Users/runner/work/_temp/8645affc-a0dc-4e0a-969a-e941309fd806.sh
2024-02-07T05:12:04.0345610Z /Users/runner/work/_temp/8645affc-a0dc-4e0a-969a-e941309fd806.sh: line 1: -: command not found
2024-02-07T05:12:04.0368780Z ##[error]Process completed with exit code 127.

Did you upgrade the scanner? Because I’m looking at the current scanner’s shell script and nowhere in it do I see an invocation of /bin/bash. I suggest you verify your scanner installation and make sure there’s no script in front of it.

 
HTH,
Ann

Hi Ann,

Here is the workflow I am using:

name: Build

on:
  push:
    branches:
      - universal-develop
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  build:
    name: Build
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis

      - name: Download and install the build wrapper
        run: |
          mkdir $HOME/.sonar
          curl -sSLo $HOME/.sonar/build-wrapper-macosx-x86.zip ${{ secrets.SONAR_HOST_URL }}/static/cpp/build-wrapper-macosx-x86.zip
          unzip -o $HOME/.sonar/build-wrapper-macosx-x86.zip -d $HOME/.sonar/
        env:
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

      - name: Download and install the SonarScanner
        run: |
          curl -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-macosx.zip
          unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/

      - name: Build and analyse the project
        run: |
          - name: Build and analyse the project
          # Potential improvement : add these paths to the PATH env var.
          pod install
          xcodebuild -scheme Simpplr -workspace Simpplr-Framework.xcworkspace -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.2' clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
          $HOME/.sonar/sonar-scanner-5.0.1.3006-macosx/bin/sonar-scanner
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

the second step Download and install the SonarScanner is supposed to install the latest version, OR do I need to update sonar in our enterprise account?(I am afraid I do not have permission for that)

With the above workflow, the runner exited with a command not found error, but what exact command it is looking for? What am I missing here? Kindly help, I have been struggling with this for quite some time now.

Hi,

Have you looked at what you get when you download and extract that zip?

I just did it, and here’s what I got:

Selection_1569

 
Ann

I am not downloading the zip manually, above provided workflow file is in the root project of my GitHub repository, and I am trying to get sonar analysis done through GitHub action.

I am still unable to make it work for my swift project. Can someone please help.
I am following this tutorial: https://www.youtube.com/watch?v=6zvBuZr8CeI&t=205s&ab_channel=Sonar
I can’t find any option for Swift (@5:40 in video), so I have added the script for c,c++ or ObjC, and it is not working for my swift project. Please help.

Hi,

Again,

I suggest you just follow the docs.

 
HTH,
Ann