无法从gitlab-ci.yml推送

bap*_*ste 17 shell gitlab-ci gitlab-ci-runner

与我的同事一起,我们致力于开发一个每天变得越来越重要的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 ..
Run Code Online (Sandbox Code Playgroud)

我们还尝试了一个基本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
Run Code Online (Sandbox Code Playgroud)

这两个项目都是一样的site,我有权推进这两个项目.我在哪里做错了什么?

Cli*_*ara 28

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

首先,您需要生成令牌,如gitlab文档中所示.确保检查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 ..
Run Code Online (Sandbox Code Playgroud)

  • 我已经更新了我的答案,以便更容易得到你需要的东西;) (3认同)
  • 我已经检查了所有范围,包括 api 范围,但仍然无法推送到我的存储库。您确定可以使用令牌。我之前使用的是手动配置的 ssh,因为我认为这是一种解决方案 (2认同)

oli*_*ver 24

虽然以前的答案或多或少都很好,但有一些重要的gotya。

  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}"
Run Code Online (Sandbox Code Playgroud)

一方面,我们只需要将用户名/电子邮件设置为请 git。

其次,在之前的脚本中使用它并不是非常重要,但可以在执行“扩展”时更轻松地重用。

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

此外,它不是必需的,我们只进行一次推送。

此外,在确定 URL 时,我发现使用存在的 CI_REPOSITORY_URL 是最可靠的解决方案(例如移动 repo 或诸如此类时)。所以我们只是替换 URL 字符串中的用户名/令牌。

  • `#*@` 是子字符串删除,将删除从开始到 `@` 的所有内容 (5认同)
  • “@”实际上是 HTTP 协议的一部分,表示您想使用用户名:密码@主机名登录。变量:“${CI_REPOSITORY_URL}”是变量。“#*@”是正则表达式(regex),但我也不知道它是做什么的。或者,您也可以在“@”符号后使用:“${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git”。 (2认同)
  • 我也想知道为什么使用这个“${CI_REPOSITORY_URL#*@}”。@miaonster 的评论让我走上了正轨:“${CI_REPOSITORY_URL}”扩展为“https://gitlab-ci-token:[masked]@example.com/gitlab-org/gitlab-foss.git” ` 正如你可以从 [gitlab 文档](https://docs.gitlab.com/ee/ci/variables/#list-all-environment-variables) 中看到的那样——并且由于我们只需要“@”之后的所有内容,完成后 `${CI_REPOSITORY_URL#*@}` 我们得到 `example.com/gitlab-org/gitlab-foss.git`。 (2认同)

Prz*_*wak 8

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

例子:

before_script:
 - git remote set-url origin https://$GIT_CI_USER:$GIT_CI_PASS@gitlab.com/$CI_PROJECT_PATH.git
 - git config --global user.email 'myuser@mydomain.com'
 - git config --global user.name 'MyUser'
Run Code Online (Sandbox Code Playgroud)

您必须定义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
Run Code Online (Sandbox Code Playgroud)

  • 我用“$CI_SERVER_HOST”而不是 gitlab.com 更新了这个答案。 (2认同)