Build definition mirroring repo to external git repo on push in VSTS / Azure DevOps

702 views Asked by At

As the title indicates, I have an issue making a build step in VSTS / Azure DevOps, where I would like to update an external git repo when I push to ie. my realease or master branch. I've tried several add ons from the marketplace, but none of them seems to fit my needs. I have a few prerequisites:

  • My external git is behind port 33
  • My external git only accepts SSH keys as authentication so I need to store an SSH private key somewhere to be able to push to the external source.

I have alternatively also tried Gitlab because of the built in mirroring feature, but unfortunately that won't accept port 33, only 22 and other standard ports :-(

Thank you so much in advance!

1

There are 1 answers

3
Antebios On

You could add a PowerShell Build Step with a Condition if the 'Build.SourceBranch' matches 'master' or 'release', and then the PowerShell script being something like this example. This pushes everything in one go, not each commit. This can also catch up a repo that is behind.

# Test to see if remote alias already exists
git ls-remote ssh://user@host:1234/srv/git/TargetRepoSameName
# Add a remote alias
git remote add any_name_123 ssh://user@host:1234/srv/git/TargetRepoSameName

# push local repo to 'any_name_123'
git push any_name_123 --all
# optional: delete all tags before attempting to push local tags
git push any_name_123 --delete `$(git tag -l)
# push local tags to remote repo 
git push any_name_123 --tags

Notice that I'm using port # 1234, whereas if you do not specify a port, then 22 is the default. Please use this snippet in your overall solution. You could also use a git-hook to push changes.