Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add/update files in git repository from Azure DevOps Pipeline Dynamics 365 CE

Short Version

When a build pipeline is triggered, one of the build pipeline task will get the latest files and it has to be added/updated in the git repository of current running pipeline. I tried to do it by using command line task but its failing as not git repository

fatal: not a git repository (or any of the parent directories): .git

Long Version

I m tried to achieve solution pack and unpack process for dynamics 365 instance. So the build pipeline have the following tasks

  1. Export solution from Dynamics 365 and store it in $(build.binariesdirectory)

  2. Unpack the solution zip file and store in $(Build.Repository.LocalPath) i.e., adding/updating existing files

  3. Command line task to commit and push the files to current pipeline repository

ECHO "Setting git config..."
git config --global user.email "[email protected]"
git config --global user.name "Admin"

ECHO "CHECK GIT STATUS..."
git status

ECHO "GIT ADD..."
git add -A

ECHO "CHECK GIT STATUS..."
git status

ECHO "Commiting the changes..."
git commit -m "Latest Customizations updated"

ECHO "Pushing the changes..."
git push -u origin master

ECHO "Customization Committed Successfully"

Updated

If a pipeline is created with 2 command line task i.e. one to create a random file in the Build.Sourcedirectory and another to commit the changes, then the git commit and push commands are working.

But if we add other tasks (such as unzip files in Build.Sourcedirectory directory) before the command line task then getting error as not a repository.

If you see the below screenshot, in the checkout phase the git repo is cloned to D:\a\1\s enter image description here

And below screenshot is from command line task, where the current working directory is same as the checkout task (i.e., D:\a\1\s) and we can see that the ".git" folder is present so the current directory has local repository in it, but still getting error as fatal: not a git repository

enter image description here

Am I missing anything here?

like image 494
Nitin Avatar asked Aug 27 '19 18:08

Nitin


1 Answers

By default the source files are checked out to the Build.SourcesDirectory (e.g : Directory: D:\a\1\s), it can be considered as a temporary git repository.

According to the error message, it appears that the working directory of the command line task is not under the Build.SourcesDirectory and you did't git checkout again to that working directory.

So, please try unpack the solution zip file and store in Build.SourcesDirectory, then run below Command line to push the commits (it works for me):

ECHO "Setting git config..."
git config --global user.email "[email protected]"
git config --global user.name "Admin"

ECHO "CHECK GIT STATUS..."
git status

git checkout -b master

ECHO "GIT ADD..."
git add -A

ECHO "CHECK GIT STATUS..."
git status

ECHO "Commiting the changes..."
git commit -m "Latest Customizations updated"

ECHO "Pushing the changes..."
git push -u origin master

ECHO "Customization Committed Successfully"

Please note that you need to Grant version control permissions to the build service and enable Allow scripts to access the system token.

enter image description here

like image 84
Andy Li-MSFT Avatar answered Oct 07 '22 07:10

Andy Li-MSFT