bap*_*ste 17 shell gitlab-ci gitlab-ci-runner
与我的同事一起,我们致力于开发一个每天变得越来越重要的C++库.我们已经通过gitlab-ci.yml文件构建了持续集成实用程序,让我们:
各种让我们选择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)
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 字符串中的用户名/令牌。
您还可以提供用户和密码(具有写访问权限的用户)作为秘密变量并使用它们。
例子:
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_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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14975 次 |
| 最近记录: |