Reputation: 2567
I implemented the oauth2 web flow in order to get access_token from users of my app. With the access_token, I would like to do the following actions:
I already successfully get the user information(1) and create a repo(2)
The problem is I can't push code (3), I got "Unauthorized" error.
The command I run:
git remote add origin https://gitlab-ci-token<mytoken>@gitlab.com/myuser/myrepo.git
git push origin master
Upvotes: 104
Views: 159554
Reputation: 2070
The OP asked about using git push
, but some Maven plugins also write to the repository.
Git credentials can be cached in the git credential system or placed in
the settings.xml
file.
git credential | settings.xml | |
---|---|---|
git push |
X | |
maven-release-plugin |
X | |
versions-maven-plugin |
X | X |
Create a (personal, project, group) access token with write-repository
permission and copy it to a masked (project, group) variable REPO_TOKEN
.
project/.gitlab-ci.yml
:
job:
script:
- echo -e
"protocol=https\n
host=gitlab.example.com\n
username=git\n
password=$REPO_TOKEN\n"
| git credential-cache store
- git commit -m "Upload changes"
- mvn versions:use-latest-releases
- mvn release:prepare
- mvn release:perform
project/pom.xml
:
<scm>
<url>https://gitlab.example.com/group/${project.artifactId}</url>
<connection>scm:git:https://gitlab.example.com/group/${project.artifactId}.git</connection>
<developerConnection>scm:git:https://gitlab.example.com/group/${project.artifactId}.git</developerConnection>
</scm>
<properties>
<scm.tag>${env.COMMIT_ID}</scm.tag>
<project.scm.id>gitlab-scm</project.scm.id>
</properties>
~/.m2/settings.xml
<server>
<id>gitlab-scm</id>
<username>git</username>
<password>${env.REPO_TOKEN}</password>
</server>
Upvotes: 0
Reputation: 6836
It is also possible to push directly without adding a new remote repository:
git push https://gitlab-ci-token:<access_token>@gitlab.com/myuser/myrepo.git <branch_name>
This could be particularly useful if you want to pull from and push to different repositories.
Upvotes: 53
Reputation: 4156
I placed the following into my ~/.gitconfig
:
[credential "https://gitlab.com"]
username = <insertusername>
helper = "!f() { echo "username=<insertusername>"; echo "password=$GITLAB_PERSONAL_ACCESS_TOKEN"; };f"
Upvotes: 2
Reputation: 31760
You can also use git remote set-url
. After creating your access token, do:
git remote set-url origin https://gitlab-ci-token:${ACCESS_TOKEN}@gitlab.com/<group>/<repo-name>.git
Upvotes: 15
Reputation: 1954
You should do
git remote add origin https://<access-token-name>:<access-token>@gitlab.com/myuser/myrepo.git
Note that this stores the access token as plain text in the .git\config
file. To avoid this you can use the git credential system, providing the access token name for "username" and the access token for "password". This should store the credentials in the git credential system in a more secure way.
Upvotes: 135
Reputation: 6259
Push using gitlab-ci-token
is not currently supported by Gitlab. There is an open feature request.
Upvotes: -3