Lee
Lee

Reputation: 1465

How to run a git command from a pipeline using yaml in Azure DevOps

I simply want to run a Git command from a YAML file. Here's what I have in my YAML file:

steps:
- checkout: self
  persistCredentials: true
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      git config user.name "my_name"
      git config user.password "my_password"
      git clone https://my_repo@dev.azure.com/the_repo_stuff
      git checkout dev
      git checkout -b newer-branch
      git commit -a -m 'new branch commit'
      git push --set-upstream origin newer-branch

I'm getting fatal: could not read Password for 'https://my_repo@dev.azure.com': terminal prompts disabled

The password I used is what I generated in Azure DevOps in the Clone window.

For now, my goal is simply to get this script to create a branch. Eventually, I would like to pass in variables and make it more complex.

Upvotes: 6

Views: 13387

Answers (3)

J.Wincewicz
J.Wincewicz

Reputation: 962

I am a little bit confused why you need such thing.

In case of using the same repository in which pipeline.yaml is located you should be able to use git commands because of

- checkout: self
  persistCredentials: true

In case you want to checkout different repository, consider checking it out with service connection and multiple repositories option:

resources:
  repositories:
  - repository: MyGitHubRepo # The name used to reference this repository in the checkout step
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
  - repository: MyBitbucketRepo
    type: bitbucket
    endpoint: MyBitbucketServiceConnection
    name: MyBitbucketOrgOrUser/MyBitbucketRepo
  - repository: MyAzureReposGitRepository # In a different organization
    endpoint: MyAzureReposGitServiceConnection
    type: git
    name: OtherProject/MyAzureReposGitRepo

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- checkout: MyAzureReposGitRepository
- script: |
     git checkout -b new-branch
     git push --set-upstream origin newer-branch

- script: dir $(Build.SourcesDirectory)

Source: Multiple repositories documentation

Also keep in mind that multiple repositories will change default repositories path:

If you are using default paths, adding a second repository checkout step changes the default path of the code for the first repository. For example, the code for a repository named tools would be checked out to C:\agent_work\1\s when tools is the only repository, but if a second repository is added, tools would then be checked out to C:\agent_work\1\s\tools. If you have any steps that depend on the source code being in the original location, those steps must be updated.

Upvotes: 7

wallezzi
wallezzi

Reputation: 359

After clicking the "Generate Git Credentials" option when cloning Azure repos, you will see below panel.

enter image description here

Thus you could create a branch for this repository using following script

git clone https://username:password@dev.azure.com/organization/project/_git/repository_name
cd repository_name
git checkout dev
git checkout -b newer-branch
git commit -a -m 'new branch commit'
git push --set-upstream origin newer-branch

Upvotes: 2

aswin putra
aswin putra

Reputation: 147

Are you using SSH? If you are, then I think you will have to create an SSH Key Authentication in order to clone to your server. For more info, you can check: https://learn.microsoft.com/en-us/azure/devops/repos/git/use-ssh-keys-to-authenticate?view=azure-devops

Upvotes: 1

Related Questions