首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MSBuild/SonarQube通过Jenkins分析非Visual,统一C#代码

使用MSBuild/SonarQube通过Jenkins分析非Visual,统一C#代码
EN

Stack Overflow用户
提问于 2017-04-17 22:34:12
回答 1查看 1.3K关注 0票数 2

我继承了SonarQube Linux服务器的职责,该服务器运行着5.1版本,并分析了数十个Unity项目。在将其升级到SQ 6.1之后,我发现对分析非Windows系统上的C#项目的支持在5.5版中被删除,所以我现在将其降级为5.4版。

我随后将Windows系统设置为Jenkins节点和SonarQube服务器。但是,我希望用SonarQube来评估的项目(大部分)是基于macOS开发的,而且(总是)构建在统一中的macOS上,因此缺乏MSbuild解决方案文件。除了在项目中添加特定于Visual的代码之外,我是否有可能在这些约束中使用SonarQube?是否有某种虚拟的解决方案文件,我可以添加到项目中,这样可以在不需要生成工作二进制文件的情况下对它们进行分析(比如解析/标记化而不需要编译/链接)?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-07 20:05:35

下面是我为通过Jenkins分析SonarQube中的而开发的过程。

  1. 具有以下设置和选项的面向用户的Jenkins作业,以及特定于我的环境的其他设置和选项。
代码语言:javascript
复制
1. **General—This project is parameterized** 
    1. **String parameter** `PROJECTKEY`
    2. **String parameter** `PROJECTNAME`
    3. **String parameter** `PROJECTVERSION`

代码语言:javascript
复制
1. **General—Restrict where this project can be run** set to the Jenkins build node name you used for your SonarQube server.
2. **Source Code Management** 
    1. **Repository URL** set to the repository you want to analyze.
    2. **Branches to build** set to the main branch, i.e. `refs/heads/master`.
    3. **Additional Behaviors—Recursively update submodules** checked as appropriate for your repository.

代码语言:javascript
复制
1. **Build Triggers** 
    1. **Poll SCM** checked, but no schedule specified.

代码语言:javascript
复制
1. **Build Environment** 
    1. **Run buildstep before SCM runs—Inject environment variables** set to `HOME=C:\Users\Jenkins` (or as appropriate for your system). This environment variable makes it possible for git to find the correct ssh credentials to use with the Git server. You'll need to put your git private key in `%HOME%\.ssh`; that is documented elsewhere and left as an exercise to the reader.
    2. **Use build environment from another project—Template Project** pointing to a second Jenkins job. Mine is called _SonarQube\_Scanner_ (see below).

代码语言:javascript
复制
1. **Build** 
    1. **Use builders from another project—Template Project** pointing to the same Jenkins job `SonarQube_Scanner`.

代码语言:javascript
复制
1. **Post-build Actions** 
    1. **Archive the artifacts—Files to archive** set to `log.zip`.

  1. 一个面向非用户的Jenkins作业(SonarQube_Scanner),具有以下设置和选项。
代码语言:javascript
复制
1. **General—Disable this project** checked.
2. **General—Restrict where this project can be run** checked and set to your SonarQube server.
3. **Source Code Management** set to None.
4. **Build Environment—Inject environment variables to the build process** checked.
代码语言:javascript
复制
    1. **Properties Content** contains

LANG=en\_US.UTF-8

代码语言:javascript
复制
1. **Build**

设置FILENAME=%WORKSPACE%\client\Assets\Editor\SQMenuItems.cs REM是的,这个语法工作得更好,并且更易读>%文件名%回波使用UnityEditor;>> %文件名%回送公共静态类SQMenuItems { >> %文件名%回送静态空AssetsOpenCSharpProject() { >> %文件名% ECHO #if UNITY_5 >> %FILENAME% ECHO EditorApplication.ExecuteMenuItem("Assets/Open C#项目“);>> %文件名% ECHO ##en2# >> % EditorApplication.ExecuteMenuItem("Assets/Sync MonoDevelop项目”);>> %文件名% ECHO #endif >> % >> % ECHO EditorApplication.Exit(0);>> %文件名% ECHO }>>%>>% ECHO } "C:\Program Files\Unity_5.5.2\Editor\Unity.exe“-quit -batchmode -nographics -logFile "%WORKSPACE%\log.txt”-buildTarget android -projectPath %工作区%\client -executeMethod SQMenuItems.AssetsOpenCSharpProject "C:\Program \7-zip\7z.exe“-tzip a log.zip log.txt

代码语言:javascript
复制
        - SQMenuItems.cs is what triggers Unity to create the `.sln` and `.*proj` files that MSBuild needs. The `.sln` and `.*proj` files are typically in the `.gitignore` file and therefore not in the repository.

代码语言:javascript
复制
    1. **SonarQube Scanner for MSBuild - Begin Analysis**
代码语言:javascript
复制
        1. **SonarQube Installation** set to the Jenkins build node name you used for your SonarQube server.
        2. **Project key** set to `${PROJECTKEY}`.
        3. **Project name** set to `${PROJECTNAME}`.
        4. **Project version** set to `${PROJECTVERSION}`.
        5. **Additional arguments**

/d:sonar.exclusions=**\\SQMenuItems.cs

代码语言:javascript
复制
    1. **Execute Windows batch command**

"C:\Program (x86)\MSBuild\14.0\Bin\MSBuild.exe“/maxcpucount /nr:false /nologo /target:rebuild /verbosity:quiet client\client.sln

代码语言:javascript
复制
        - You may need to change `client\client.sln` above to match your environment. I haven't yet tried replacing that with an environment variable defined in the user-facing Jenkins job.

代码语言:javascript
复制
    1. **SonarQube Scanner for MSBuild - End Analysis**

  1. DONE
  2. GOTCHAS
    1. 如果作业因为找不到projectPath而失败,那么它可能忘记了它的许可密钥。远程桌面到您的SonarQube服务器并启动Unity。如果弹出请求您的许可证密钥,请重新输入密钥,然后退出。随后的工作应该会成功。
    2. 这使得很难将信息以动态的方式传递给SonarQube。
      1. 在2.5.1“执行Windows命令”中定义的Windows环境变量不会通过2.5.2“SonarQube扫描器for MSBuild - Begin Analysis”来保存。
      2. SonarQube.Analysis.xml文件特别禁止分配属性sonar.projectNamesonar.projectKeysonar.projectVersion

  1. 我可能漏掉了什么东西,请不要犹豫。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43460716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档