自动将“develop”分支合并到“sonar-scan”分支

问题描述 投票:0回答:1

我们有一个名为

sonar-scan
的分支(类似于功能分支),其中 SonarQube 相关配置存在于
azure-pipelines.yml
管道下,因此由于独特的配置,该分支不能合并到
develop
分支中。

但是我们总是需要来自开发分支的最新代码/提交,以便 SonarQube 扫描它。然而,由于安全要求,only

sonar-branch
应该在自托管的Ubuntu代理(VMSS)上运行,因此与其他分支不同,没有机会使用默认代理池。此外,可能会发生合并冲突,因为两个
azure-pipelines.yml
现在具有不同的配置。

我尝试在

sonar-scan
分支内实现简单的 git 脚本,但不幸的是还没有成功。 与本地相比,git 在 Azure DevOps 上的行为可能有所不同。

- script: |
    git fetch origin
    git checkout develop
    git pull origin develop
    git checkout feat/sonar-scan
    git pull origin feat/sonar-scan

    git merge develop --no-commit --no-ff --allow-unrelated-histories || true
     
    // Tries to solve conflicts by keeping 'azure-pipelines.yml' from the sonar-scan branch as it is
    // --ours flag wants keep the version of .yml file in sonar-scan branch, like stash or staging
    git checkout --ours azure-pipelines.yml
    git add azure-pipelines.yml

    // Complete the merge
    git add .
    git commit -m "Auto-merge latest changes from develop"
    git push origin feat/sonar-scan

  displayName: 'Checkout and merge branch'  

参考:https://marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest

此任务目前适用于 Windows 计算机。

如何解决上述问题并找到最佳解决方案?

git azure-devops azure-pipelines sonarqube
1个回答
0
投票

基于合并

develop
分支中的代码并保持
azure-pipelines.yml
中的
sonar-branch
不变的要求,您可以尝试使用
azure-pipelines.yml
中的
sonar-branch
文件中定义的以下脚本。

steps:
- checkout: self # During a PR validation build checkout: self will checkout the intermediate PR branch of refs/pull/{prId}/merge
  fetchDepth: 0 # Disable shallow fetch to keep the related history between branches
  persistCredentials: true

- script: |
    git config --global user.email "$(Build.RequestedForEmail)"
    git config --global user.name "$(Build.RequestedFor)"

    echo "================ 1. Checkout sonar-branch ================"
    git checkout -b sonar-branch

    echo "================ 2. Fetch develop ================"
    git fetch origin develop

    echo "================ 3. Merge develop into sonar-branch, excluding azure-pipelines.yml ================"
    git merge -X ours --no-commit origin/develop

    echo "================ 4. Restore azure-pipelines.yml from sonar-branch ================"
    git checkout sonar-branch -- azure-pipelines.yml
    
    echo "================ 5. Commit the merge ================"
    git commit -m "Merge develop into sonar-branch, excluding azure-pipelines.yml"

    echo "================6. Push the merge to remote sonar-branch ================"
    git push origin sonar-branch

  displayName: 'Merge develop into sonar-branch and push'

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.