无法从gitlab-ci.yml推送

问题描述 投票:12回答:3

与我的同事一起,我们致力于开发一个每天变得越来越重要的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:[email protected]/developers/benchmarks.git/': The requested URL returned error: 403

这两个项目都在同一个site下,我有权推进这两个项目。我在哪里做错了什么?

shell gitlab-ci gitlab-ci-runner
3个回答
19
投票

gitlab ci令牌更像是github.com中的deploy键,因此它只具有对存储库的读访问权限。要实际推送,您需要生成个人访问令牌并使用它。

首先,你需要生成如here in the gitlab documentation所示的令牌。确保检查read user和api scope。此外,这仅适用于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 ..

1
投票

您还可以提供用户和密码(具有写访问权限的用户)作为秘密变量并使用它们。

例:

before_script:
 - git remote set-url origin https://$GIT_CI_USER:[email protected]/$CI_PROJECT_PATH.git
 - git config --global user.email '[email protected]'
 - git config --global user.name 'MyUser'

你必须将GIT_CI_USERGIT_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

0
投票

虽然之前的答案或多或少都很好,但还有一些重要的问题。

  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_NAME}:${CI_GIT_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_COMMIT_TAG}"

首先,我们只需要将用户名/电子邮件设置为git。

其次在前脚本中使用它并不是非常重要,但允许在执行'扩展'时更容易重用。

最后,推送https是“很好”,但由于我们没有使用存储的ssh密钥,我们应该避免任何可以揭示令牌的东西。首先,虽然gitlab不会在此命令中打印令牌,但git会很高兴地通知我们新的上游设置为https://username:thetokeninplaintexthere@url所以你的令牌是纯文本的,所以不要使用-u来设置上游。

此外,它不需要,我们只做一次推。

此外,在确定URL时,我发现使用现有的CI_REPOSITORY_URL是最可靠的解决方案(例如,当移动repo时,或者诸如此类的东西)。所以我们只需要替换URL字符串中的用户名/标记。

© www.soinside.com 2019 - 2024. All rights reserved.