简体   繁体   中英

Enforcing in git that topic branches are only created from certain branch

We're using azure devops and git to manage our CI/CD workflow to deploy our application to azure.

We have git branches for each env (canary, test, prod etc), new changed are made by a user creating a topic branch from the prod branch and then creating a PR to the canary branch using a non-fast-forward merge type, this ensure the that other in-flight changes are kept separate.

But we have issues where our users don't always remember to create a new topic branch from the right most branch (prod) which means they can end up pulling other changes through when they didn't mean to - or creating merge conflicts.

Is there a way in either azure devops or in git (which we could create a validation pipeline for) to check that a topic branch was indeed started from the right most branch?

Consider a build pipeline such as the one below. You could configure this as a validation build as part of your canary branch policy to ensure that prod is in the history of your topic branches.

trigger: none

pool:
  vmImage: ubuntu-latest

variables:
  BranchOfInterest: "origin/prod"

steps:

- checkout: self    
  persistCredentials: true

- script: |
    git for-each-ref --contains "$(BranchOfInterest)" | grep ` git rev-parse HEAD `;
    if [ $? -ne 0 ]; then
        echo "$(BranchOfInterest) is not in the history of $(Build.SourceBranch). This is not allowed when merging into $(System.PullRequest.TargetBranch)."
        exit 1;
    fi
  displayName: 'Ensure $(BranchOfInterest) is in the history of $(Build.SourceBranch).'

You could also invert this using --no-contains to ensure that a certain branch isn't in the history of the topic branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM