1

how to commit files into azure git repo using build pipeline.

I am generating some files from my azure ci pipeline and I want to commit those files into the same repo which I used in my ci pipeline.

I have added the azure CLI task and added an inline command to commit code but it gives me the error and not working.

git add .
git commit -m "test commit"
git push -u origin Trunk
Jaydeepsinh
  • 83
  • 1
  • 8
  • 1
    Have you followed these steps in the docs? https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=yaml&WT.mc_id=DOP-MVP-5001511 – jessehouwing May 25 '21 at 20:41
  • Yes it gives me "Cannot prompt because user interactivity has been disabled" error. – Jaydeepsinh May 25 '21 at 20:51
  • Hi @Jaydeepsinh Is there any update about this ticket? Feel free to let me know if the suggestion could give you some help. Just a remind of [this](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Kevin Lu-MSFT May 27 '21 at 05:59
  • Additionally, i add this url befor git add "git remote set-url origin https://username:gitpassword@repository url" – Jaydeepsinh May 27 '21 at 16:47

3 Answers3

3

For classic Mode pipeline:

You could enable the Allow scripts to access the OAuth token option.

enter image description here

And grant the Repo Contribute permission for Project Collection Build Service Accounts.

enter image description here

Git command:

   git config --global user.email "email"
   git config --global user.name "Kevin Lu"

   git add .

   git commit -m "My commit message"

   git push origin master

For Yaml Pipeline:

You could set the persistCredentials: true in YAML sample.

And grant the Repo Contribute permission for Build Service .

enter image description here

Yaml Sample:

steps:
- checkout: self
  persistCredentials: true

- script: |
   git config --global user.email "email"
   git config --global user.name "Kevin Lu"
       
   git add .
   
   git commit -m "My commit message"
   
   git push origin master
   
  displayName: 'Command Line Script'

For more detailed info, you could refer to this ticket.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • before git add set origin by giving this url"git remote set-url origin https://username:gitpassword@repository url" – Jaydeepsinh May 27 '21 at 16:50
  • for whatever reason, mine requires `git push origin HEAD:refs/heads/main` all other variations of HEAD,main,master etc failed – Cine Jul 24 '23 at 06:10
0

Please make sure that both Project Collection Build Service Accounts and <repo> Build Service (<org>) have Allow in Contribute section:

enter image description here

Asssuming you already made all steps Run Git commands in a script you should be ready to go. Please notice that documentation says that you should set Project Collection Build Service, but it is also necessary to set Project Collection Build Service Accounts.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
0

FYI this can also be achieved using GitHub as-well, all that is required is the Service Connection running the pipeline has permission to commit to the GitHub repository, you can then run the following below:

steps:
- checkout: self
  persistCredentials: true
  fetchDepth: 0
  
- bash: |
    echo "This Is Build $(Build.BuildId)" > "$(Build.SourcesDirectory)/BuildNumber"
  displayName: Create File
  
- bash: |
    git config user.name "John Smith"
    git config user.email "John.Smith@example.com"
  
    git checkout main --
    git add --all
    git commit -m "Adding Build File"
    git push origin main
  displayName: Git Commit

Ref: https://jimferrari.com/2023/08/02/commit-files-to-github-during-azure-pipelines-run/

jfdevops
  • 171
  • 2
  • 2