Last active
October 4, 2022 07:38
-
-
Save eegeeZA/5d79a6da7b4fb7385d1fb8029d8e7712 to your computer and use it in GitHub Desktop.
Add to your Azure Pipelines (VSTS) task for closing your `release` and `hotfix` branches as per GitFlow recommendations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Variables | |
$repoURL = "git@ssh.dev.azure.com:v3/example/example/example" # or "https://dev.azure.com/example/example/_git/example" | |
$sourceBranch = ("$env:BUILD_SOURCEBRANCH" -replace "refs/heads/") | |
$tagName = ($sourceBranch -replace ".*/(v.*)", '$1') # or $(Get-Date -Format yyy-MM-dd) | |
$userEmail = "email@example.com" | |
$userName = "Example" | |
Write-Host "##[debug] repoURL: $repoURL" | |
Write-Host "##[debug] sourceBranch: $sourceBranch" | |
Write-Host "##[debug] tagName: $tagName" | |
Write-Host "##[debug] userEmail: $userEmail" | |
Write-Host "##[debug] userName: $userName" | |
# Enhance logging of Git errors and create folder as working directory | |
Write-Host "##[debug] redirecting all Git output to Azure logs" | |
$env:GIT_REDIRECT_STDERR = '2>&1' | |
Write-Host "##[command] New-Item -Path $sourceBranch -ItemType Directory; Set-Location $sourceBranch" | |
New-Item -Path $sourceBranch -ItemType Directory; Set-Location $sourceBranch | |
# Checkout repository | |
Write-Host "##[command] git clone $repoURL ." | |
git clone $repoURL . | |
## if using Azure Repos then enable OAuth access on task and use the lines below | |
# Write-Host "##[command] git clone -c http.extraheader=`"AUTHORIZATION: bearer $env:SYSTEM_ACCESSTOKEN`" $repoURL ." | |
# git clone -c http.extraheader="AUTHORIZATION: bearer $env:SYSTEM_ACCESSTOKEN" $repoURL . | |
# Configure user details for Git commit | |
Write-Host "##[command] git config user.email `"$userEmail`"" | |
git config user.email "$userEmail" | |
Write-Host "##[command] git config user.name `"$userName`"" | |
git config user.name "$userName" | |
# Verify branch exists | |
$remoteBranches = (git branch -r).Trim().Split() | |
Write-Host "##[debug] remoteBranches:`n$remoteBranches" | |
if ($remoteBranches -notmatch $sourceBranch) { | |
Write-Host "##[warning] skipping merge because '$sourceBranch' not found" | |
Exit 0 | |
} | |
# Merge into master and tag | |
Write-Host "##[command] git checkout master" | |
git checkout master | |
Write-Host "##[command] git merge --no-ff --no-edit origin/$sourceBranch" | |
git merge --no-ff --no-edit origin/$sourceBranch | |
Write-Host "##[command] git tag -m `"deployed $(Get-Date -Format yyy-MM-dd)`" $tagName" | |
git tag -m "deployed $(Get-Date -Format yyy-MM-dd)" $tagName | |
Write-Host "##[command] git push origin master" | |
git push origin master | |
Write-Host "##[command] git push origin $tagName" | |
git push origin $tagName | |
# Merge into develop or release(s)/hotfix(s) | |
$destinationRegex = "origin/((release|hotfix)/v[.0-9]+).*" | |
$remoteBranches = ($remoteBranches -replace $sourceBranch, 'ignore-current-branch') | |
if ($remoteBranches -notmatch $destinationRegex) { | |
Write-Host "##[debug] git checkout develop" | |
git checkout develop | |
Write-Host "##[command] git merge --no-ff --no-edit origin/master" | |
git merge --no-ff --no-edit origin/master | |
Write-Host "##[command] git push origin develop" | |
git push origin develop | |
} | |
else { | |
Write-Host "##[debug] found release(s)/hotfix(s) branches, skipping 'develop'" | |
foreach ($destinationBranch in $remoteBranches -match $destinationRegex) { | |
$destinationBranch = ($destinationBranch -replace "origin/(.*)", '$1') | |
Write-Host "##[command] git checkout $destinationBranch" | |
git checkout $destinationBranch | |
Write-Host "##[command] git merge --no-ff --no-edit origin/master" | |
git merge --no-ff --no-edit origin/master | |
Write-Host "##[command] git push origin $destinationBranch" | |
git push origin $destinationBranch | |
} | |
} | |
# Cleanup old branch | |
Write-Host "##[debug] deleting '$sourceBranch' from origin" | |
Write-Host "##[command] git push origin -d $sourceBranch" | |
git push origin -d $sourceBranch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment