how to git clone via https with personal access token in private project gitlab - authentication

I want to git clone a private repository with two factor authentication on my mac, I already have a personal access token in my gitlab, and I'm trying to find the correct command to run on the vscode terminal.
omarfuentes#192 ~ % git clone https://gitlab.com/OmarLeonardo/frappe/proyectos/frappe-oms/frappe-server.git?personal_access_token=myaccesstoken
however the answer is:
zsh: no matches found: https://gitlab.com/OmarLeonardo/frappe/proyectos/frappe-oms/frappe-server.git?personal_access_token=myaccesstoken

If you are using a PAT (Personal Access Token) with the right scope, you should use:
git clone https://<my-user-id>:<my-token>#gitlab.com/<my-account>/<my-project-name>.git
# or
git clone https://oauth2:<my-token>#gitlab.com/<my-account>/<my-project-name>.git
That would avoid the need to URL encode your parameter.

To clarify, if you created your account with Google you can find your username in the URL to the repository. You can also see the name of your project there. So if your URL looks like the below:
https://gitlab.com/me1234567/myproject/-/tree/main
your final command would look like this:
git clone https://oauth2:my-personal-token-123#gitlab.com/me1234567/myproject.git
The User ID you see under the Gitlab Edit profile can not be used, nor your email address or the "username" you see under your avatar which starts with #

Related

Authentication for github on Dockerfile

In my Dockerfile I want to clone a repo from github. To achieve that I applied the following command to my Dockerfile:
RUN git clone -b docker https://<pat-token>:x-oauth-basic#github.com/<mygithubaccount>/<repository.git> /myfolder
Of course, the phrases in square brackets are placeholders.
When I build the image from my Dockerfile I can read authentication error in the output:
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
I assume that is something around my authentication string. Maybe I cannot use a personal access token?
How do I correctly authenticate against github in my Dockerfile?
Fixed it - I've had a type in the token 🙈

How to connect a project from IDEA to Gitlab

Is it possible to connect a project from IDEA to Gitlab?
There are no problems with GitHub, you specify a github account, then VCS-> Import into and it creates a project in your account. But with GitLab I don't see such a possibility. Maybe there is some way?
Or just manually throw it in there?
If all you want to do is create a project, that is as simple as pushing the repo. In GitLab, you don't have to create the project in the UI first. You can simply push directly to a project namespace that does not yet exist. The project will be created when you push to it.
Therefore, all you have to do is configure your git remote per usual, then push.
git init
git checkout -b main
echo "# My Project" > README.md
git add README.md
git commit -m "initial commit"
git push --set-upstream git#gitlab.com:namespace/myproject.git main
Then you'll see the message from the remote in the git console log
remote: The private project namespace/myproject was created.
In the JetBrains IDEs, you can simply configure the remote and push.
You only have define the remote ahead of time in the terminal. For example:
git remote add origin git#gitlab.com:namespace/project
Then in the IDE, when you go to push, you'll see the ability to push to the new remote/branch.
Then you'll also note in the git tab console you'll see the message from the remote that the project has been created.
Full integration for GitLab hasn't been implemented yet, but there is a feature request for that:
https://youtrack.jetbrains.com/issue/IDEA-109294
Meanwhile, you can create a repository in GitLab, then press Cmd/Ctrl+K and Click "Commit and push". In Push dialog there will be "Define remote" button - click on it and paste URL to GitLab's repository.
If it's HTTPS then you'll be prompted with username and password - enter them, alternatively you can enter Username and Personal access token (in password field)

Is there a way of adding username and password into the bzr checkout command?

I'm trying to automate an install process using bash, the following Bazaar command are part of this:
bzr export /home/path bzr+https://adress
bzr checkout bzr+https://address/ /home/path
After running these commands manually I'm prompted for username and password.
Is there any way of adding username and password directly into the commands in order to avoid the prompting?
Checked the Bazaar documentation, but couldn't find a solution.
The standard format for HTTP URLs can be used to pass a username/password combination:
$ bzr checkout bzr+https://username:password#host/path /local/path
Another option would be to add your credentials to ~/.config/bazaar/authentication.conf. See this page for details: http://doc.bazaar.canonical.com/beta/en/user-reference/authentication-help.html

Using Gitlab deploy keys with write access

I am currently running CE version 8.17.4 and am attempting to setup a deploy key with write access (as of 8.16) so that my runner instance may commit build artifacts back to the repository. I took the following steps to set this up:
On the runner instance, I generated the ssh keypair with the command: 
sudo ssh-keygen -t rsa -C "label" -b 4096
The generated keypair was saved to /home/gitlab-runner/.ssh/id_rsa and password protected.
Within Gitlab, I created a public deploy key from the admin console and pasted the contents of id_rsa.pub into the appropriate field and verified that the key fingerprints matched. I checked the "Write access allowed" box. 
In the private project that I wished to enable repository access from the runner, I enabled the newly created public deploy key.
This is a LaTeX document respository, so in the .gitlab-ci.yml file, I issue the following script after building the pdf:
after_script:
  - "git commit -am 'autobuild PDF'"
  - "git push origin master"
When the changes were committed, the build ran successfully on the runner up until the git push origin master command, and this error was thrown:
fatal: Authentication failed for 'http://gitlab-ci-token:xxxxxxxxxxxxxxxx#host/project.git/'
Ok. A couple questions:
If the deploy key is just an SSH key, shouldn't it be connecting on the secure port or does this matter? I haven't found much documentation on using this new write-permission deploy key feature, so am I missing something in the steps I took above?
Do I need to include [ci skip] in the commit message to avoid looping CI builds? I saw this concern come up in the original issue tickets for this feature, but did not see whether this step was required or not. 
Thanks for any help!
Jawad's comment worked for me: you need to force SSH. for example
git remote add ssh_remote git#host:user/project.git
git push ssh-remote HEAD:dev
thanks jawad

How to tag a git repo in a bamboo build

I'm trying to tag the git repo of a ruby gem in a Bamboo build. I thought doing something like this in ruby would do the job
`git tag v#{current_version}`
`git push --tags`
But the problem is that the repo does not have the origin. somehow Bamboo is getting rid of the origin
Any clue?
Yes, if you navigate to the job workspace, you will find that Bamboo does not do a straightforward git clone "under the hood", and the the remote is set to an internal file path.
Fortunately, Bamboo does store the original repository URL as ${bamboo.repository.git.repositoryUrl}, so all you need to do is set a remote pointing back at the original and push to there. This is what I've been using with both basic Git repositories and Stash, creating a tag based on the build number.
git tag -f -a ${bamboo.buildNumber} -m "${bamboo.planName} build number ${bamboo.buildNumber} passed automated acceptance testing." ${bamboo.planRepository.revision}
git remote add central ${bamboo.planRepository.repositoryUrl}
git push central ${bamboo.buildNumber}
git ls-remote --exit-code --tags central ${bamboo.buildNumber}
The final line is simply to cause the task to fail if the newly created tag cannot be read back.
EDIT: Do not be tempted to use the variable ${bamboo.repository.git.repositoryUrl}, as this will not necessarily point to the repo checked out in your job.
Also bear in mind that if you're checking out from multiple sources, ${bamboo.planRepository.repositoryUrl} points to the first repo in your "Source Code Checkout" task. The more specific URLs are referenced via:
${bamboo.planRepository.1.repositoryUrl}
${bamboo.planRepository.2.repositoryUrl}
...
and so on.
I know this is an old thread, however, I thought of adding this info.
From Bamboo version 6.7 onwards, it has the Git repository tagging feature Repository Tag.
You can add a repository tagging task to the job and the Bamboo variable as tag name.
You must have Bamboo-Bitbucket integrated via the application link.
It seems that after a checkout by the bamboo agent, the remote repository url for origin is set as file://nothing
[remote "origin"]
url = file://nothing
fetch = +refs/heads/*:refs/remotes/origin/*
That's why we can either update the url using git remote set-url or in my case I just created a new alias so it does not break the existing behavior. There must be a good reason why it is set this way.
[remote "build-origin"]
url = <remote url>
fetch = +refs/heads/*:refs/remotes/build-origin/*
I also noticed that using ${bamboo.planRepository.<position>.repositoryUrl} did not work for me since it was defined in my plan as https. Switching to ssh worked.