
# 在SonarQube创建代码自动检查
#### 配置代码自动检查
1. 使用GitHub账号登录[SonarQube首页](https://sonarcloud.io/explore/projects)。
2. 单击上方导航栏"Favorite Projects"，单击"Configure analysis"。 
   图1配置代码检查   
   ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002688609817.png "点击放大")
   
   
3. 单击"With GitHub Actions"，进入SonarQube对接GitHub Actions的配置页面。 
   ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002688729629.png "点击放大")
   
   
4. 单击"Settings \> Secrets and variables \> Actions"，选择"Python"（**本案例采用Python语言编码** ），进入目标代码仓库的新建密钥页面。
   
   图2对接GitHub Actions的配置参数   
   ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002658610214.png "点击放大")
   
   
5. 单击"New repository secret"，进入密钥信息填写页面。 
   图3配置密钥   
   ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002658450278.png "点击放大")
   
   
6. 复制[4]的"Name"和"Value"值，粘贴至新建密钥页面的"Name"和"Secret"。填写完成后，单击"Add secret"，完成密钥新建。
   
   图4填写密钥参数   
   ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002688609821.png "点击放大")
   
   
7. 在GitHub成功配置密钥后，请回到[4]的SonarQube页面，单击"Python"，单击"Windows"，单击"Copy"，复制并保存到本地。
   
   ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002688729633.png "点击放大")
   
   
8. 登录GitHub，在"url-link-extractor"代码仓库首页，单击 ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002658610224.png)，进入代码仓库的分支页面。单击右上角"New branch"，在"New branch name"填写新代码分支的名称，单击"Create new branch"，即可完成新分支创建。本案例填写分支名为"develop"。
9. 单击"develop"，进入develop分支的代码首页。 
   图5进入develop分支首页   
   ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002658450284.png "点击放大")
   
   
10. 单击"Add file"，单击"Create new file"，即可进入文件的新建页面。 
    图6进入新建文件页面   
    ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002688609825.png "点击放大")
    
    
11. 复制".github/workflows/ci.yml"路径并粘贴至文件名的框内，将自动在.github/workflows/路径下新建ci.yml文件。 
    图7新建ci.yml文件   
    ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002688729637.png "点击放大")
    
    
12. 将[4]复制并保存的配置内容粘贴到"ci.yml"文件。当从本地提交代码到develop分支时，代码仓库"url-link-extractor"将自动触发流水线。
    
    ```
    name: Build
    on:
      push:
        branches:
          - main
          - develop
      pull_request:
        types: [opened, synchronize, reopened]
    jobs:
      sonarqube:
        name: SonarQube
        runs-on: windows-latest
        steps:
          - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
            with:
              fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
          - name: SonarQube Scan
            uses: SonarSource/sonarqube-scan-action@7006c4492b2e0ee0f816d36501671557c97f5995 # v8.1.0
            env:
              SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
    ```
    **其中，需要注意的是，本案例基于develop分支进行开发，期望在develop推送时也扫描代码，因此在branches下增加"- develop"值。**
    
    
13. 内容粘贴完成后，单击右上角"Commit changes"。
14. 单击"Add file"，单击"Create new file"，即可进入文件的新建页面。 
    图8进入新建文件页面   
    ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002658610226.png "点击放大")
    
    
15. 切换至develop分支，在根目录下，粘贴[4]的第3点内容为新建文件名和文件内容后，单击"Commit changes"，再单击"Commit changes"后，将自动跳转到"Code"页面。
    
    图9Project onboarding的第3点   
    ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002658450286.png "点击放大")
    图10新建"sonar-project.properties"
    ![](https://support.huaweicloud.com/bestpractice-codeartsagent/zh-cn_image_0000002688732183.png "点击放大")
    
    
 
