Cannot clone git repository in command line script task in Azure DevOps Pipelines - azure-devops

I created azure devops pipeline where I need to download code from another git repository. As per recommended solution I've read somewhere, I added a command line script task with git clone command. Unfortunatelly this doesn't work.
The error that I get is:
remote: TF200016: The following project does not exist: My0Test0Project. Verify that the name of the project is correct and that the project exists on the specified Azure DevOps Server.
fatal: repository 'https://dev.azure.com/myCompany/My0Test0Project/_git/Service.Azure.Core/' not found
My project in Azure has spaces, maybe there is a bug in azure related to that? Does anybody knows any workarounds?
This is some code that I have tried already:
git -c http.extraheader="AUTHORIZATION: Basic bXl1c2VyOmxtNjRpYTYzb283bW1iYXp1bnpzMml2eWxzbXZoZXE2azR1b3V2bXdzbnl5b3R5YWlnY2E=" clone https://dev.azure.com/myCompany/My%20Test%20Project/_git/Service.Azure.Core
git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" clone https://dev.azure.com/myCompany/My%20Test%20Project/_git/Service.Azure.Core
git clone https://oauth:lm64ia63oo7mmbazunzs2ivylsmvheq6k4uouvmwsnyyotyaigca#dev.azure.com/myCompany/My%20Test%20Project/_git/Service.Azure.Core
git clone https://test:$(System.AccessToken)#dev.azure.com/myCompany/My%20Test%20Project/_git/Service.Azure.Core

The reason is because you need to add the system access token so that Azure can have access to the external repo. (your credentials need access to that repo)
NOTE:
I wrapped my URL in quotes to escape the % signs in the URL string because Powershell treats those as variables otherwise.
- powershell: |
git clone -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" "https://{YOUR PIPELINE URL}"
displayName: '{YOUR PIPELINE NAME}'

I had the same issue from a bash script and solved it by escaping the % character with %%.
So, you can use this :
git clone https://test:$(System.AccessToken)#dev.azure.com/myCompany/My%%20Test%%20Project/_git/Service.Azure.Core

Can you try to change your Command Line task to something like this one?
git clone --single-branch --branch $(branchName) https://$(gitUserName):$(gitPassword)#dev.azure.com/myCompany/My%20Test%20Project/_git
Where $(gitUserName) and $(gitPassword) configured as Alternate git credentials in
your Azure DevOps account.
Also wanted to ask about ending of git repository URL:
".../_git/Service.Azure.Core", you are trying to specify which folder to clone or maybe working directory? You can also try your previous code without that ending, just
https://dev.azure.com/myCompany/My%20Test%20Project/_git

I tried this from Powershell task and it worked
Its very simple just use
- powershell: |
git config user.email "$(Build.RequestedForEmail)"
git config user.name "$(Build.RequestedFor)"
git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" clone https://<org_Name>.visualstudio.com/<project_name>/_git/<repository_name>;
displayName: "<your task display name>"

This will happen when your path has space characters in it.
The workaround for this is to not use %20 in your URL and use actual space characters instead. Since your URL now has space characters, also make sure that your URL is wrapped inside double quotes.
So your
git clone https://dev.azure.com/myCompany/My%20Test%20Project/_git/Service.Azure.Core/
should be
git clone "https://dev.azure.com/myCompany/My Test Project/_git/Service.Azure.Core/"

Related

Receiving "fatal: not a git repository (or any of the parent directories): .git" while using pat to push code into bitbucket using azure powershell

I am writing an Azure PowerShell script that will consume the JSON file which has the location of all my SQL scripts and Migrationflag column (which holds to execute/to be executed) and execute all the sequence of scripts.
upon execution, the flag will change to 'N' and the updated JSON file should be uploaded to bitbucket.
Now, I am stuck with "fatal: not a git repository (or any of the parent directories): .git" error while trying to push.
I've created a pat token and service connection with username: santhoshsreshta and below is the code to push.
$v_JSON = Get-Content '$(system.defaultworkingdirectory)\locationToBuild\BuildOrder.json' -Raw | ConvertFrom-Json
$v_JSON | Sort-Object -Property OrderNo | Where-Object {$_.MigratedFlag -like 'Y'} | ForEach {
$Script = $_.Location
Write-Host "Executing Script: $Script"
Invoke-Sqlcmd -ServerInstance "myservername" -Database $(database) -Username $(testauto_username) -Password $(testauto_password) -InputFile $(system.defaultworkingdirectory)\$Script
$_.MigratedFlag = 'N'
}
$v_JSON | ConvertTo-Json -depth 32| set-content '$(system.defaultworkingdirectory)\locationToBuild\BuildOrder.json'
$MyPat = 'mypatcode'
git push https://mygitusername:$MyPat#bitbucket.org/xyz/abcd.git
getting the error,"##[error]fatal: Not a git repository (or any of the parent directories): .git"
but when issuing git clone https://mygitusername:$MyPat#bitbucket.org/xyz/abcd.git -- getting invalid username/password error.
I believe we should not clone again as my pipelines get sources task will clone it and puts in a self-hosted agent.
this is my git url: https://mygitusername#bitbucket.org/xyz/abcd.git
Thanks a ton,
A DevOps, PowerShell newbie here.
Receiving “fatal: not a git repository (or any of the parent directories): .git” while using pat to push code into bitbucket using azure powershell
That because we are using git feature, but we are not in the folder managed by git.
When we use Azure powershell task directly, the default work folder should be:
PS C:\Users\<Username>
Obviously, there are no files managed by our git in this directory. That the reason why you get the error Not a git repository.
So, to resolve this issue, we just need to switch the working folder to the repo folder by the command:
cd $(System.DefaultWorkingDirectory)
Check the Use predefined variables and this thread for some details.
Update:
The test sample:
cd $(System.DefaultWorkingDirectory)
cd leotest
echo 123>Test.txt
git add Test.txt
git commit -m "Add a test file"
git push https://Username:password#bitbucket.org/Lsgqazwsx/leotest.git HEAD:master
It means that there is no local .git & you need to do the below first:
git init
git commit -m "first commit"
git branch -M main
git remote add origin https://your-repo-url
git push -u origin main
I just copied the below from GitHub page & it works for Azure Repos :-) further showing the power & advantage of companies following universal standards

Extract Git URL from file using Powershell

1/ I need to extract in a folder a specific part of url. The url is in the subfolder named ".git" and I use the command:
git remote get-url --all origin
url look like this:
- git#servername:do-amont/production/ei2/nova/migration-devfactory.git
- ssh://servername.domain.com:30272/do-amont/production/ei2/nova/migration-devfactory.git
- https: //gitlab-review-dev-jxuor3.openshift-intra-dev.domain. com/do-amont/production/ei2/nova/migration-devfactory.git
the part I need is
do-amont/production/ei2/nova/migration-devfactory.git
After that I need to put the extracting url on this command
git remote set-url --add origin ssh://git#l203ei3007.domain.com:30272/do-amont/production/ei2/nova/migration-devfactory.git
I need to automatise these step on powershell
You can get the URL from the git config for a specified remote via the git config git command-line command, as long as you run it from within the directory with the .git folder.
So you could do something like this (assumes the part of the URL you want to keep is always after a colon):
$GitURL = (git config remote.origin.url).split(':')[1]
git remote set-url --add origin "ssh://git#l203ei3007.domain.com:30272/$GitURL"

Polymer 2.0 upload to GitHub-Pages

I have problem with uploading my Polymer component into gh pages.
I'm try this from tutorial:
# git clone the Polymer tools repository somewhere outside of your
# element project
git clone git://github.com/Polymer/tools.git
# Create a temporary directory for publishing your element and cd into it
mkdir temp && cd temp
# Run the gp.sh script. This will allow you to push a demo-friendly
# version of your page and its dependencies to a GitHub pages branch
# of your repository (gh-pages). Below, we pass in a GitHub username
# and the repo name for our element
../tools/bin/gp.sh <username> <test-element>
# Finally, clean-up your temporary directory as you no longer require it
cd ..
rm -rf temp
But it's not working.
In terminal I have this errors:
There is something I'm, missing?
Here is your problem:
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
For the script to run as intended, you need to add your public ssh key to your github project. Settings -> Deploy keys -> Add deploy Key.
Alternatively, you can manually execute the steps in gp.sh that involve pulling from and pushing to github.
If you don't feel like splitting up the script, try running the commands manually, that should work. The only multi-line command in the script is this one:
echo "{
\"directory\": \"components\"
}
" > .bowerrc
Good luck.

Pushing entire Rundeck configuration to github

I want to push entire Rundeck configuration to Github. Is there any way for doing this ?
Considering the Rundeck configuration layout, all you would need to do is:
cd $RDECK_BASE
git init .
git remote add origin /url/new/empty/GitHub/repo
echo '*'>.gitignore
echo '!/etc/'>.gitignore
echo '!/server/'>.gitignore
echo '/server/*'>.gitignore
echo '!/server/config/'>.gitignore
git add .
git commit -m "Rundeck config"
git push -u master
Basically, you need to ignore what is not configuration before adding everything else (ie, the config files), and pushing to your own GitHub repo.
Make sure those files don't have sensitive credential information in them though (or at least push them to a private GitHub repo if you have one)

SCM environment variables missing

Usually, when using SCM like the Git Plugin, there are a bunch of environment variables that you can use (e.g. see these)
But neither the Git Step nor the Generic SCM seem to do that.
Is there a way to get these variables into the groovy env.* so that they can be used?
Something like this would be useful:
def commitMessage = sh 'git log --max-count=1 --oneline --no-merges | cut -b9-'
I can think of writing the results to a file and read them via the readFile() mehtod -- but is there an easier way to achieve this?
For the Record: I have the following code to get the branch-name:
stage 'preparation'
node {
// checkout branch
git branch: 'origin/master', url: 'git#example.net:project.git'
// write current branch-name to file
sh 'git branch -a --contains `git rev-parse HEAD` | grep origin | sed \'s!\\s*remotes/origin/\\(.*\\)!\\1!\' > git-branch.txt'
// read data from file into environment-variable
env.gitBranch = readFile('git-branch.txt').trim()
// let people know what's up
echo "testing branch ${env.gitBranch}"
}
The remainder of the flow-script is comprised of serveral parametrized jobs which get the env.gitBranch passed as parameter (among others, if needed).
Be sure to allow concurrent builds for the workflow to catch every updated branch.
See JENKINS-24141; these variables are not yet available from Workflow.
In the meantime, you are on the right track: run a git command to record any information you need, and use readFile to load it (see also JENKINS-26133).