I am using on premise Azure DevOps. When a pipeline builds the actual release build, I want the pipeline to update the CHANGELOG.md with the version and the release date directly with a new commit. But this is rejected if the branch is a protected branch.
How can my pipeline running on any build agent make a commit on branch that is a protected branch?
This is the only approach that worked for me out of all of the answers provided in 2024. (YAML below is for a very basic custom script for incrementing semantic versioning based on merged PRs containing Conventional Commit messages).
# Regular expression for conventional commits $regex = "^(feat|fix|docs|style|refactor|perf|test|chore|build|ci|revert|BREAKING CHANGE)(\(.+\))?!?: .+" if ($title -notmatch $regex) { Write-Error "PR title does not follow Conventional Commit guidelines. Please ensure the title starts with one of the allowed types (e.g., feat, fix) followed by an optional scope and a colon." exit 1 } else { Write-Host "PR title follows Conventional Commits format" } } env: SYSTEM_ACCESSTOKEN: $(System.AccessToken) displayName: "Validate PR Title against Conventional Commits"
- job: PostMergeActions displayName: "Post-Merge Actions" condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main')) steps: - checkout: self persistCredentials: true #Important - Persist creds to run further git command clean: true #Important - Certain kinds of changes to the local repository aren't automatically cleaned up by the build pipeline - task: PowerShell@2 inputs: targetType: "inline" script: | git config --global user.email "email address removed for privacy reasons" git config --global user.name "ADO pipeline"
# Fetch the latest changes git fetch --all
# Ensure the branch exists and switch to it git checkout main
# Read and increment the version number $versionFilePath = "version.txt" $version = Get-Content $versionFilePath $versionParts = $version -split '\.'
# Update the version number $newVersion = "$major.$minor.$patch" Set-Content -Path $versionFilePath -Value $newVersion Write-Host "Bumping version to $newVersion"
# Commit and tag the new version git add $versionFilePath git commit -m "chore(release): bump version to $newVersion [skip ci]" # git tag -a "v$newVersion" -m "Release $newVersion"
# Push changes and tags git push --follow-tags displayName: "Validate Commit Message and Bump Version" ```