gitazure-devopsazure-pipelinessonarqube

Automatically merge "develop" branch into "sonar-scan" branch


We have a branch named sonar-scan (like a feature branch) where SonarQube related configuration is present under azure-pipelines.yml pipeline, hence this branch cannot be merged into develop branch because of the unique configurations.

But we always need latest code/commits from develop branch so that SonarQube scans it. However, only sonar-scan branch should run on self-hosted Ubuntu agent (VMSS) due to security requirement, hence no chance to use default agent pools unlike other branches. Also, merge conflict could occur because both azure-pipelines.yml now have different configurations.

I tried to implement simple git script inside sonar-scan branch but unfortunately no success yet. Probably, git behaves differently on Azure DevOps compared to locally.

- 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'  

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

This task currently only works on Windows machines.

How to solve above issue and find an optimal solution?


Solution

  • Update

    As far as I tested, the previous workflow is able to bring both the newly created and updated files from develop branch into sonar-branch and keep the azure-pipelines.yml in sonar-branch unchanged.

    Image

    No sure why the changes in your index.html file from the develop branch failed to get merged into the sonar-branch, but you may also try the workflow below as we discussed.

    1. Create a new branch called newsonar based on the latest commit in develop branch; Image
    2. Create a new sonar.yml file with the same contents as those in the azure-piplines.yml of sonar-branch to run the same SonarQube scanning steps in your VMSS agent pool; Image
    3. Create a new pipeline referencing the sonar.yml file; Image Image
    4. When the code from develop branch is ready for scanning, you can create a PR to merge the changes/commits from develop branch into newsonar branch; Image
    5. The merge should complete without rebasing, so that the sonar.yml file only exists in newsonar branch and thus, there will be no conflicts in this file;
    6. If you need to adjust your SonarQube scans, you can edit the sonar.yml definition file in the newsonar branch; and don't touch the other files in the newsonar branch, as the changes in them should be always brought from develop branch.

    Based on the requirement to merge the code from develop branch and keep the azure-pipelines.yml in sonar-branch unchanged, you may try the script below defined in the azure-pipelines.yml file from sonar-branch.

    steps:
    - checkout: self
      clean: true # To make sure no legacy left in the System.DefaultWorkingDirectory that may affect the git commands in each run, since running in self-hosted agent pool.
      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 ================"
        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