3

I am very new to VSTS platform. In one of my project, I am trying to integrate the bitbucket source control to VSTS. By this way I should be able to see the updates made on bitbucket onto the VSTS account.

I have tried creating build on VSTS, but that only shows the commits history of the selected repository of bitbucket.

Is there a way to manage all the bitbucket changes on VSTS as source control?

Community
  • 1
  • 1
DIGVJSS
  • 463
  • 1
  • 10
  • 21
  • How do you want to "integrate the bitbucket source control to VSTS"? Do you wanr the sync each changes from bitbucket repo to VSTS git repo after new changes are pushed to bitbucket repo (for any branches)? – Marina Liu Jun 18 '18 at 07:46
  • Exactly, the same thing I want :) We are moving on VSTS. We already have our project on bitbucket and uses JIRA integration, and we don't want to break that integration. So, if any changes are done in VSTS for any repository, that should be reflected in Bitbucket, and vice versa. – DIGVJSS Jun 18 '18 at 10:33

2 Answers2

11

To sync changes from bitbucket repo to VSTS git repo automatically, you can achieve it by using a VSTS build definition. Detail steps as below:

1. Create a build definition with Bitbucket repo

When creating a VSTS build definition -> Select the Bitbucket repo you want to sync -> create.

enter image description here

2. Enable continuous integration

In the build definition -> Triggers Tab -> Enable continuous integration -> Include all branches with *.

enter image description here

3. Add PowerShell task with the script to sync bitbucket repo with VSTS git repo

Add a PowerShell task with below script:

if ( $(git remote) -contains 'vsts' )
{git remote rm vsts
echo 'remove remote vsts'
}

$branch="$(Build.SourceBranch)".replace("refs/heads/","")
git remote add vsts https://Personal%20Access%20Token:PAT@account.visualstudio.com/project/_git/repo
git checkout $branch
git push vsts $branch -f

For the detail steps to add and config the PowerShell task as below:

Edit your build definition -> Click + to add a task for your agent phase -> Search powershell task -> click Add -> click the PowerShell task you added -> select Inline type -> then add your powershell script in the Script option -> Save build definition.

enter image description here

enter image description here

Now no matter which branch is updated in your bitbucket repo, VSTS git repo will be synced automatically.


Yo sync changes from VSTS git repo to bitbucket repo, you can create another CI build to achieve it. Detail steps as below:

1. Create a CI build with VSTS git repo

enter image description here 2. Enable continuous integration enter image description here 3. Add a PowerShell task with below aspects

if ( $(git remote) -contains 'bitbucket' )
{git remote rm bitbucket
echo 'remove remote bitbucket'
}

git remote add bitbucket https://username:password@bitbucket.org/username/repo.git 
$branch="$(Build.SourceBranch)".replace("refs/heads/","")
git checkout $branch
git push bitbucket $branch -f
Community
  • 1
  • 1
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • Thanks Marina! one question here, how the build definition will be used by this powershell script? Can you please provide a bit information on what this script command does? Will it trigger all the builds I have in my VSTS account? – DIGVJSS Jun 20 '18 at 09:24
  • @DIGVJSS I added the annotation in the PowerShell script. And the build will be triggered only when new changes are pushed to bitbucket repo. The process as: new changes pushed to bitbucket repo -> build triggered -> new changes will also updated to VSTS git repo correspondlingly. – Marina Liu Jun 20 '18 at 09:31
  • thanks, got it. So, the trigger is going to run the powershell script is it? Where can I add the powershell script? – DIGVJSS Jun 20 '18 at 15:00
  • Yes. And I added the detail steps to add and config a PowerShell task (under step3 in my answer), and you can have a try. – Marina Liu Jun 21 '18 at 01:56
  • @DIGVJSS Can you sync bitbucket repo to VSTS git repo successful now? – Marina Liu Jun 22 '18 at 03:14
  • it works, thanks alot :) I see git remote rm vsts, what it does? does it removed anything, and recreates? Also Marina, it would be much appreciated if you could help on reverse process as well. Syncing Bitbucket when changed in VSTS. – DIGVJSS Jun 22 '18 at 07:41
  • If the build is queued by private agent and the work directory not clean before each time build, then you need to delete the remote and readded. Ok, I will post the solution with the reverse process later. – Marina Liu Jun 22 '18 at 07:44
  • @DIGVJSS I added the opposite way in the and of my answer, you can have a try. – Marina Liu Jun 22 '18 at 08:01
  • I am getting an error for the second powershell script - fatal: unable to access 'https://digvijay-gww:xxxxxxxx@bitbucket.org/digvijay-gww/test-gwr.git/': Could not resolve host: digvijay-gww. My password has special characters in it. – DIGVJSS Jun 22 '18 at 09:30
  • I have that in place, does the special symbol in password needs to be encoded? – DIGVJSS Jun 22 '18 at 09:54
  • @DIGVJSS No, the password not need to be encoded. You just need to provide the original password as it is in the url. – Marina Liu Jun 25 '18 at 07:45
  • it fails for new branch creation of type `digvijay/test-01`. I am getting an error "_pathspec 'test-01' did not match any file(s) known to git_". Works fine for normal branch eg. test-01. – DIGVJSS Jul 09 '18 at 07:55
  • @DIGVJSS Thanks for pointing it out. I update the script by adding `$branch="$(Build.SourceBranch)".replace("refs/heads/","")`, and use the variable `$branch` for checkout and push. And you can try again. – Marina Liu Jul 09 '18 at 08:40
  • Thanks, $(Build.SourceBranch) returns me the correct branch name, does refs/heads/ appears in any scenarios? – DIGVJSS Jul 09 '18 at 11:38
  • It's specify the references from .git folder. And it just need to trim remove refs/heads/ from $(Build.SourceBranch) , then the remaining string is the branch name. – Marina Liu Jul 10 '18 at 01:24
  • Long shot, given how old this post is... but what permissions are required on the remote BitBucket repo to enable this approach? I'm getting the following error while attempting to set this up with a friend's repo and just want to know what permissions I need to ask for: _"Unable to configure a service on the selected Bitbucket repository. Bitbucket returned the error 'Forbidden: '."_ – m-smith Oct 20 '18 at 15:11
  • @oliver-clare I'm also getting the same permissions error. Did you manage to resolve this? – Tom Chantler Feb 21 '19 at 10:19
  • 1
    @TomChantler yes I did manage to resolve the permissions issue, but only because my friend made me an admin on his repo. Sorry, I'm afraid I don't know what the specific permission needed is. – m-smith Feb 21 '19 at 10:46
  • 1
    @oliver-clare thank you. I think your friend had to do that. _"The administrators of a repository are the only users who can create a webhook on that repository."_ according to https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html – Tom Chantler Feb 21 '19 at 11:16
  • @TomChantler great spot! I have submitted an edit to this post to try and make this clear for future readers. – m-smith Feb 21 '19 at 16:36
0

When you connect your Bitbucket account to VSTS, you are setting up build triggers to run automated builds on pull requests or merges. This is what is called "continuous integration" in the DevOps world.

enter image description here

Consider reading the documentation for more information on this topic.

You will continue to "manage" your Bitbucket repos on Bitbucket. It's totally separate. If you want to manage everything through VSTS, you should import your Bitbucket repo to your VSTS account.

enter image description here

Brian Cristante
  • 781
  • 1
  • 5
  • 4
  • Thanks for your answer brian. I am looking for something of automation. We are moving on VSTS. We already have our project running on Bitbucket and integrated with JIRA. If any change made on VSTS then that should be reflected on Bitbucket repo and vice versa. – DIGVJSS Jun 18 '18 at 10:39
  • You want to look into Git mirroring. (This is a Git thing, not a VSTS-specific thing.) Though I will say that there are a lot of wonky edge cases around consistency and it will complicate your team's workflow considerably. If you want to "manage" your source code through repo A and mirror it in repo B, I would recommend forbidding pushes to repo B so that it is a read-only view of repo A. – Brian Cristante Jun 19 '18 at 13:43
  • @BrianCristante—How is that done? Forbidding `push`es to repo B, I mean. But doesn't `git push --mirror ` also constitute a `push`, and would therefore be prohibited as well? More info at [this Q&A](https://stackoverflow.com/q/50938180/722393). – InteXX Jun 19 '18 at 23:45