首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从gitlab-ci.yml

无法从gitlab-ci.yml
EN

Stack Overflow用户
提问于 2017-09-28 14:54:49
回答 4查看 44.7K关注 0票数 42

与我的同事们一起,我们开发了一个C++库,这个库每天变得越来越重要。我们已经通过gitlab-ci.yml文件构建了连续集成实用程序,该文件允许我们:

  • 调试模式下的构建与测试
  • 发布模式下的构建与测试
  • 执行安全检查,如内存泄漏,使用Valgrind,并检查在我们的库中是否有明确的符号,我们不想在其中。
  • 生成文档

所有让我们选择GitLab的东西!

我们希望对我们的整个库进行概要分析,并在一个单独的项目中推进基准测试。我们已经使用SSH密钥方法为out文档做了一些工作,但是这次我们想避免这样做。

我们试过这样的脚本:

代码语言:javascript
复制
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来推送更新的文件,但是每次我们得到相同的答案:

代码语言:javascript
复制
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下,我有权利推进这两个项目。我在哪里做错事了?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-09-29 07:53:58

gitlab令牌更像github.com中的部署密钥,因此它只具有对存储库的读取访问权限。要真正推动,您将需要生成一个个人访问令牌,并使用它。

首先,您需要生成令牌,如所示在gitlab文档中所示。确保同时检查read和api作用域。而且,这只适用于GitLab 8.15和更高版本。如果您使用的是旧版本,并且不希望升级,我可以向您展示另一种方法,但它更复杂,安全性更低。

最后,您的gitlab-ci.yml应该是这样的:

代码语言:javascript
复制
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 ..
票数 66
EN

Stack Overflow用户

发布于 2019-03-25 19:02:27

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

代码语言:javascript
复制
  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}"

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

其次,在前脚本中使用它并不是非常关键,但是在执行“扩展”时可以更容易地重用。

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

而且,这是不需要的,我们只是做了一个推动。

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

票数 31
EN

Stack Overflow用户

发布于 2018-05-17 08:46:05

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

示例:

代码语言:javascript
复制
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_USERGIT_CI_PASS定义为秘密变量(可以为此目的始终创建专用用户)。

使用此配置,您通常可以使用git。我正在使用这种方法在发布后推送标签(使用Axion发布Gradle Pluing - http://axion-release-plugin.readthedocs.io/en/latest/index.html)

示例发布作业:

代码语言:javascript
复制
release:
  stage: release
  script:
    - git branch
    - gradle release -Prelease.disableChecks -Prelease.pushTagsOnly
    - git push --tags
  only:
   - master
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46472250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档