How to download code from a particular branch in AzureDevOps release pipeline - azure-devops

I need to download all the files from the feature branch in AzureDevOps release pipeline. I am using Azure repo. Is there any task in AzureDevOps classic editor for the same? How to implement this using Powershell?

[UPDATE]
In your release pipeline,
if a previous step in the same job will check out the source repository but not check out the feature branch, you can use the git checkout command to check out the feature branch to the local repository on the agent machine, and this command also will switch the current branch to feature in the local repository.
git checkout -b feature --track origin/feature
if no previous step to check out any source version from the repository, you can use the git clone command to only check out the the feature branch to the local.
git clone <Repo_URL> -b feature
You can use either Bash or PowerShell to execute these commands in your pipeline.

Related

Azure DevOps CI/CD pipeline: Revert commit on failure

I am currently building an Azure DevOps CI/CD pipeline and if it fails, I don't want to keep the code that lead to the fail in my repository. So, if the pipeline fails, I want the repo to be reverted to the last version before that commit. I can't find any help on that. How does this option look like and how can I add this option to my .yaml file?
Thank you so much.
Normally, you can use the git revert command to revert some existing commits.
In your pipeline, you can check out the git repository and the branch where you made the changes. Then run the git revert command to 'undo' some commits.
For more details, you can reference the following articles:
Git - git-revert Documentation
Git Revert
However, as #GeralexGR has suggested, it is recommended that you'd better create a develop branch based on the default branch (main/master) and make changes on this branch. Then build and test the code on the develop branch. Once everything is good on the develop branch, you can create a Pull Request to merge the changes from the develop branch to the default branch.

how do i automatically pull code from master to fork in azure repos using azure devOps pipeline

I was trying to pull the code from master to fork repository automatically through the azure pipeline. If any one know about this?
If you fork azure repo and want to automatically sync the Fork repo using VSTS Git, please follow below steps.
Supposed the url of original repo is https://dev.azure.com/{organization}/{project}/_git/test, and the url of forked repo is https://dev.azure.com/{organization}/{project}/_git/test_fork.
If you click "Clone" button in test repository, you will see below panel.
Clicking "Generate Git Credentials" button will show the following panel.
So we can use command git remote add upstream https://username:password#dev.azure.com/{organization}/{project}/_git/test to specify it as the upstream of test_fork repo in script.
Now creating a build pipeline using Microsoft-hosted Windows agents, setting the test_fork as the source.
Adding the Command Line task with below script.
git remote add upstream https://username:password#dev.azure.com/{organization}/{project}/_git/test
git fetch upstream
git rebase upstream/master
git push -f origin HEAD:master
Queuing a new build and it will succeed to sync the test_fork repo using VSTS Git.
You can also configure schedules for this pipeline. Now everything is done.
If you use GitHub repo, please refer to this thread for guidance.

git clone hangs within Azure DevOps build task

For some reason performing a git clone command within a Azure DevOps build pipeline always hangs. This includes git submodule update --init --recursive because I think in the background it simply calls git clone on the submodule's repo URL.
The parent repository and the submodules exist in "Azure Repos Git".
So the first phase in the build task is "Get sources" and within this phase i'm able to add command line tasks and call git tag, git branch, git commit, etc. But for some reason it hangs if I try to call git clone.
Seems like it doesn't want to communicate with any other repository other than the repository specified in the "Get sources" phase properties.
Is this a limitation?
Anyone know how I can get git clone to work in an Azure DevOps build task?
There are no errors, it simply hangs on any call to git clone.
My permissions look right:
It was right in front of my face. Under the Get sources properties, way at the bottom:
Here's documentation.

How to sync repo in bitbucket to Visual studio team service?

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?
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.
2. Enable continuous integration
In the build definition -> Triggers Tab -> Enable continuous integration -> Include all branches with *.
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.
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
2. Enable continuous integration
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
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.
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.

On-premises GIT repository with Visual Studio Team Services Project

We have on-premises installed GIT. There we have our code repositories.
Is it possible to connect a repository from this on-premises instance to Visual studio team services project?
So they display under "Code" bar?
vsts
I need it hosted on premises, but see code changes/commits and other GIT stuff in VSTS project
No, it isn’t supported to display files or code of another repository under Code bar. You need to import that repository to the repository in VSTS. After that, you can push updates to VSTS repository if there are changes in your on-premises git repository.
You can't connect on premise GIT to VSTS. You may however use the VSTS rest APIs to push in code from your on premise GIT to VSTS.
Typically you will setup a hook/trigger on your on premise GIT repo in order to automate the replication process.
As others have said, you can have a "git hook", which is basically a git trigger to take action on some event. In this case, when code is pushed, to push it up to VSTS, but I assume you need to know the technical commands.
I had to do this the opposite way, and I did this quick and dirty. This pushes everything in one go, not each commit. This can also catch up a repo that is behind.
# Clone source repo (your local git repo)
git clone some_repo_path_goes_here
# I am skipping steps and assuming you are only syncing master branch.
# I have code to get all branches down before proceeding, but not posting it here.
# Assuming tags are on master branch..
# Get all tags
git fetch origin --tags
# Test to see if remote alias already exists
git ls-remote http://path_to_.visualstudio.com/org/project/_git/TargetRepoSameName
# Add a remote alias
git remote add any_name_123 http://path_to_.visualstudio.com/org/project/_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
You can schedule this job if you would like. I have a PowerShell job to do this with a lot more functions to do pull down the branches.