Reputation: 913
As part of our build process we update version numbers and we want to commit and push the changes back to the main repository. This is a git repo hosted on azure devops and using get sources as the first step:
I added a command line task at the end of the pipeline with following code. There appears to be something wrong, how can I do this properly? Thank you.
git config --global user.email "[email protected]"
git config --global user.name "VSTS Admin"
git status
ECHO SOURCE BRANCH IS %BUILD_SOURCEBRANCH%
ECHO ADDING MODIFIED FILES
git add *.cs
git add *.build
git add *.wxs
ECHO CREATING COMMIT
git commit -m "Update version numbers for Build %BUILD_BUILDNUMBER%"
git push origin master
here are the results:
##[section]Starting: Commit/Push Changes To Git Repo
==============================================================================
Task : Command Line
Description : Run a command line script using cmd.exe on Windows and bash on macOS and Linux.
Version : 2.148.0
Author : Microsoft Corporation
Help : [More Information](https://go.microsoft.com/fwlink/?LinkID=613735)
==============================================================================
Generating script.
========================== Starting Command Output ===========================
##[command]"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\8b19361b-8f47-4d31-834d-132fedb0b386.cmd""
HEAD detached at e7a1479d
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: ../../../Properties/AssemblyInfo.cs
.. (more files modified)
SOURCE BRANCH IS refs/heads/master
ADDING MODIFIED FILES
CREATING COMMIT
[detached HEAD 5c1bf6dc] Update version numbers for Build myBuildNumber_5.1.001
2 files changed, 2 insertions(+), 2 deletions(-)
error: src refspec master does not match any
error: failed to push some refs to 'https://{org}@dev.azure.com/{projectName}/_git/{reponame}'
##[error]Cmd.exe exited with code '1'.
##[section]Finishing: Commit/Push Changes To Git Repo
Upvotes: 2
Views: 9100
Reputation: 41750
To do this using YAML pipelines you need to persistCredentials: true
steps:
- checkout: self
persistCredentials: true
- bash: |
git config --global user.email "[email protected]"
git config --global user.name "Blah at foo dot com"
displayName: set up Git user
- bash: |
... do your stuff
git push origin HEAD:whateverbranchyouareworkingwith
workingDirectory: $(Build.SourcesDirectory)
If you have to work with mutiple repos
resources:
repositories:
- repository: self
- repository: otherRepo
type: git
name: otherRepo
steps:
- checkout: otherRepo
persistCredentials: true
path: otherRepo
- bash: |
git config --global user.email "[email protected]"
git config --global user.name "Blah at foo dot com"
displayName: set up Git user
- bash: |
... do your stuff
git push origin HEAD:whateverbranchyouareworkingwith
workingDirectory: $(Pipeline.Workspace)/otherRepo
Upvotes: 1
Reputation: 843
Things to note are:
git checkout <commit-hash-SHA1>
git push <remote-repo-url> HEAD:refs/heads/<target-branch>
could be origin or fully qualified repo-urlgit push url_with_pat HEAD:refs/heads/<target-branch>
(url can also contain the PAT (personal access token to Github - with write access to target repo) of format:https://<username>:<PAT>@github.com/<username>/<repo>.git
environment variable $(Build.SourceBranch)
which is of format /refs/head/<branch>
since its in detached state mode (uses refs-heads
)This is particularly useful if for whatever reason you are trying not to checkout a branch
If the above method is not required, then still a direct checkout of specific branch can be made using classical git checkout -b <branch>
NOTE: can be substituted with trimmed $(Build.SourceBranch) where prefixing /refs/heads/
can be removed before using
Upvotes: 7
Reputation: 1329692
"src refspec master does not match any
" means the re is no local master branch to push.
That is because the commit has been created in a detached HEAD (no branch)
You must create first the master branch with git checkout -b master
before adding and committing.
More precisely, as I mentioned here:
git checkout master 2>/dev/null || git checkout -b master;
The OP dan confirms in the comments:
when it got sources in previous step it checked out to different branch than master (
git checkout --progress --force e7a1479dc5d2139be5247a027721e2a88b44a890
)
A checkout of a commit directly results by definition in a detached HEAD.
Upvotes: 0