无法从 gitlab 推送-ci.yml
Cannot push from gitlab-ci.yml
我们和我的同事一起开发一个每天都变得越来越重要的 C++ 库。我们已经通过 gitlab-ci.yml
文件构建了持续集成实用程序,让我们:
- 在调试模式下构建和测试
- 在发布模式下构建和测试
- 使用 Valgrind 执行内存泄漏等安全检查,并检查我们的库中是否有任何我们不希望在其中的明确符号
- 生成文档
所有让我们选择 GitLab 的东西!
我们想分析我们的整个库并将基准测试推送到一个单独的项目中。我们已经使用 SSH 密钥 方法为输出文档做了一些事情,但这次我们想避免这种情况。
我们试过这样的脚本:
test_ci_push:
tags:
- linux
- shell
- light
stage: profiling
allow_failure: false
only:
- new-benchmark-stage
script:
- git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git &> /dev/null
- cd benchmarks
- touch test.dat
- echo "This is a test" > test.dat
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
- git add --all
- git commit -m "GitLab Runner Push"
- git push http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git HEAD:master
- cd ..
我们还尝试了基本的 git push origin master
来推送我们更新的文件,但每次我们都得到相同的答案:
remote: You are not allowed to upload code for this project.
fatal: unable to access 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mycompany.home/developers/benchmarks.git/': The requested URL returned error: 403
两个项目都在同一个site
下,我有权同时推送这两个项目。我在哪里做错了什么?
gitlab ci 令牌更像是 github.com 中的部署密钥,因此它只有对存储库的读取权限。要真正推送,您需要生成一个个人访问令牌并使用它。
首先您需要生成令牌,如图所示here in the gitlab documentation。确保同时检查读取用户和 api 范围。此外,这仅适用于 GitLab 8.15 及更高版本。如果您使用的是旧版本并且不想升级,我可以向您展示另一种方法,但它更复杂且更不安全。
最后你的 gitlab-ci.yml 应该看起来像这样:
test_ci_push:
tags:
- linux
- shell
- light
stage: profiling
allow_failure: false
only:
- new-benchmark-stage
script:
- git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git &> /dev/null
- cd benchmarks
- echo "This is a test" > test.dat
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
- git add --all
- git commit -m "GitLab Runner Push"
- git push http://${YOUR_USERNAME}:${PERSONAL_ACCESS_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git HEAD:master
- cd ..
您还可以提供用户和密码(具有写入权限的用户)作为秘密变量并使用它们。
示例:
before_script:
- git remote set-url origin https://$GIT_CI_USER:$GIT_CI_PASS@$CI_SERVER_HOST/$CI_PROJECT_PATH.git
- git config --global user.email 'myuser@mydomain.com'
- git config --global user.name 'MyUser'
您必须将 GIT_CI_USER
和 GIT_CI_PASS
定义为秘密变量(您始终可以为此目的创建专用用户)。
使用此配置,您可以正常使用 git。我正在使用这种方法在发布后推送标签(使用 Axion Release Gradle Pluing - http://axion-release-plugin.readthedocs.io/en/latest/index.html)
示例发布作业:
release:
stage: release
script:
- git branch
- gradle release -Prelease.disableChecks -Prelease.pushTagsOnly
- git push --tags
only:
- master
虽然前面的答案或多或少都不错,但还是有一些重要的问题。
before_script:
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script:
- <do things>
- git push "https://${GITLAB_USER_LOGIN}:${CI_GIT_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_COMMIT_TAG}"
首先,我们只需要设置 username/email 来取悦 git。
其次,将它放在之前的脚本中并不是非常重要,但在执行 'extend' 时可以更轻松地重用。
最后,推送 https 是 'fine',但由于我们没有使用存储的 ssh 密钥,我们应该避免任何可以泄露令牌的事情。首先,虽然 gitlab 不会在此命令中打印令牌,但 git 会很高兴地通知我们新的上游设置为 https://username:thetokeninplaintexthere@url
所以你的令牌是纯文本的,所以不要使用 -u 来设置上游。
另外,不需要,我们只是单推
此外,在确定 URL 时,我发现使用存在的 CI_REPOSITORY_URL 是最可靠的解决方案(例如,在移动回购时或诸如此类)。所以我们只需替换 URL 字符串中的 username/token。
我正在使用以下 GitLab 作业:
repo_pull_sync:
image: danger89/repo_mirror_pull:latest
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
- if: $REMOTE_URL
- if: $REMOTE_BRANCH
- if: $ACCESS_TOKEN
before_script:
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script:
- git checkout $CI_DEFAULT_BRANCH
- git pull
- git remote remove upstream || true
- git remote add upstream $REMOTE_URL
- git fetch upstream
- git merge upstream/$REMOTE_BRANCH
- git push "https://${GITLAB_USER_LOGIN}:${ACCESS_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_DEFAULT_BRANCH}"
我正在使用自己的 danger89/repo_mirror_pull docker image based on alpine, check this GitHub repository for more info。
此 GitLab 作业从预定义的远程存储库 + 分支(请参阅下面的变量)拉取上游更改,并在本地合并它们 CI/CD 并再次将它们推送到 GitLab。
基本上我创建了一个仓库pull mirror(GitLab CE 官方不免费提供,GitLab 只支持push mirror)
- 首先在 GitLab 中创建一个项目访问令牌。通过:设置 - >访问令牌。选中 'api' 作为范围。
- 通过以下方式创建新日程:CI/CD->日程->新日程。使用以下 3 个变量:
- REMOTE_URL(示例:https://github.com/project/repo.git)
- REMOTE_BRANCH(例如:大师)
- ACCESS_TOKEN:(第一步看Access Token!例子:gplat-234hcand9q289rba89dghqa892agbd89arg2854,)
- 保存管道计划
同样,另请参阅:https://github.com/danger89/repo_pull_sync_docker_image
关于问题,请参阅上面的 git push
命令,它允许您使用 GitLab(项目)访问令牌将更改推回 GitLab。
我们和我的同事一起开发一个每天都变得越来越重要的 C++ 库。我们已经通过 gitlab-ci.yml
文件构建了持续集成实用程序,让我们:
- 在调试模式下构建和测试
- 在发布模式下构建和测试
- 使用 Valgrind 执行内存泄漏等安全检查,并检查我们的库中是否有任何我们不希望在其中的明确符号
- 生成文档
所有让我们选择 GitLab 的东西!
我们想分析我们的整个库并将基准测试推送到一个单独的项目中。我们已经使用 SSH 密钥 方法为输出文档做了一些事情,但这次我们想避免这种情况。
我们试过这样的脚本:
test_ci_push:
tags:
- linux
- shell
- light
stage: profiling
allow_failure: false
only:
- new-benchmark-stage
script:
- git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git &> /dev/null
- cd benchmarks
- touch test.dat
- echo "This is a test" > test.dat
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
- git add --all
- git commit -m "GitLab Runner Push"
- git push http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git HEAD:master
- cd ..
我们还尝试了基本的 git push origin master
来推送我们更新的文件,但每次我们都得到相同的答案:
remote: You are not allowed to upload code for this project.
fatal: unable to access 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mycompany.home/developers/benchmarks.git/': The requested URL returned error: 403
两个项目都在同一个site
下,我有权同时推送这两个项目。我在哪里做错了什么?
gitlab ci 令牌更像是 github.com 中的部署密钥,因此它只有对存储库的读取权限。要真正推送,您需要生成一个个人访问令牌并使用它。
首先您需要生成令牌,如图所示here in the gitlab documentation。确保同时检查读取用户和 api 范围。此外,这仅适用于 GitLab 8.15 及更高版本。如果您使用的是旧版本并且不想升级,我可以向您展示另一种方法,但它更复杂且更不安全。
最后你的 gitlab-ci.yml 应该看起来像这样:
test_ci_push:
tags:
- linux
- shell
- light
stage: profiling
allow_failure: false
only:
- new-benchmark-stage
script:
- git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git &> /dev/null
- cd benchmarks
- echo "This is a test" > test.dat
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
- git add --all
- git commit -m "GitLab Runner Push"
- git push http://${YOUR_USERNAME}:${PERSONAL_ACCESS_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git HEAD:master
- cd ..
您还可以提供用户和密码(具有写入权限的用户)作为秘密变量并使用它们。
示例:
before_script:
- git remote set-url origin https://$GIT_CI_USER:$GIT_CI_PASS@$CI_SERVER_HOST/$CI_PROJECT_PATH.git
- git config --global user.email 'myuser@mydomain.com'
- git config --global user.name 'MyUser'
您必须将 GIT_CI_USER
和 GIT_CI_PASS
定义为秘密变量(您始终可以为此目的创建专用用户)。
使用此配置,您可以正常使用 git。我正在使用这种方法在发布后推送标签(使用 Axion Release Gradle Pluing - http://axion-release-plugin.readthedocs.io/en/latest/index.html)
示例发布作业:
release:
stage: release
script:
- git branch
- gradle release -Prelease.disableChecks -Prelease.pushTagsOnly
- git push --tags
only:
- master
虽然前面的答案或多或少都不错,但还是有一些重要的问题。
before_script:
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script:
- <do things>
- git push "https://${GITLAB_USER_LOGIN}:${CI_GIT_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_COMMIT_TAG}"
首先,我们只需要设置 username/email 来取悦 git。
其次,将它放在之前的脚本中并不是非常重要,但在执行 'extend' 时可以更轻松地重用。
最后,推送 https 是 'fine',但由于我们没有使用存储的 ssh 密钥,我们应该避免任何可以泄露令牌的事情。首先,虽然 gitlab 不会在此命令中打印令牌,但 git 会很高兴地通知我们新的上游设置为 https://username:thetokeninplaintexthere@url 所以你的令牌是纯文本的,所以不要使用 -u 来设置上游。
另外,不需要,我们只是单推
此外,在确定 URL 时,我发现使用存在的 CI_REPOSITORY_URL 是最可靠的解决方案(例如,在移动回购时或诸如此类)。所以我们只需替换 URL 字符串中的 username/token。
我正在使用以下 GitLab 作业:
repo_pull_sync:
image: danger89/repo_mirror_pull:latest
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
- if: $REMOTE_URL
- if: $REMOTE_BRANCH
- if: $ACCESS_TOKEN
before_script:
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script:
- git checkout $CI_DEFAULT_BRANCH
- git pull
- git remote remove upstream || true
- git remote add upstream $REMOTE_URL
- git fetch upstream
- git merge upstream/$REMOTE_BRANCH
- git push "https://${GITLAB_USER_LOGIN}:${ACCESS_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_DEFAULT_BRANCH}"
我正在使用自己的 danger89/repo_mirror_pull docker image based on alpine, check this GitHub repository for more info。
此 GitLab 作业从预定义的远程存储库 + 分支(请参阅下面的变量)拉取上游更改,并在本地合并它们 CI/CD 并再次将它们推送到 GitLab。
基本上我创建了一个仓库pull mirror(GitLab CE 官方不免费提供,GitLab 只支持push mirror)
- 首先在 GitLab 中创建一个项目访问令牌。通过:设置 - >访问令牌。选中 'api' 作为范围。
- 通过以下方式创建新日程:CI/CD->日程->新日程。使用以下 3 个变量:
- REMOTE_URL(示例:https://github.com/project/repo.git)
- REMOTE_BRANCH(例如:大师)
- ACCESS_TOKEN:(第一步看Access Token!例子:gplat-234hcand9q289rba89dghqa892agbd89arg2854,)
- 保存管道计划
同样,另请参阅:https://github.com/danger89/repo_pull_sync_docker_image
关于问题,请参阅上面的 git push
命令,它允许您使用 GitLab(项目)访问令牌将更改推回 GitLab。