Gitlab Ci无法从runner推送分支

3

我想要使用Gitlab搭建CI/CD流水线,以下是我的需求:

注意:我的项目是Typescript

  1. 执行单元测试和集成测试
  2. 将dev分支推进到integration分支
  3. 从integration分支构建Docker镜像
  4. 部署到integration环境中

以下是我正在使用的.gitlab-ci.yml:

stages:
  - test
  - promote
  - build
  - deploy
cache:
  paths:
    - node_modules/
test:
  image: node
  stage: test
  before_script:
    - yarn
  script:
    - yarn test
promote:
  image: node
  stage: promote
  only:
    - dev
  script:
    - git push origin HEAD:integration
build
  image: node
  stage: build
  only: 
    - integration
  script: 
    - echo "build docker image from integration"
deploy:
  image: node
  stage: deploy
  only:
    - integration
  script:
    - echo "deploy integration"

我的问题是,这行代码 git push origin HEAD:integration 无法在GitLab Runner上执行,以下是控制台输出:

Running with gitlab-runner 10.1.0 (c1ecf97f)
  on RUNNER (ce8757c9)
Using Docker executor with image node ...
Using docker image sha256:fb8322a7cefdf2b3ba1c15218187bb65f9d4d4ab4e27dc3a91bb4eba38964429 for predefined container...
Pulling docker image node ...
Using docker image node ID=sha256:c1d02ac1d9b4de08d3a39fdacde10427d1c4d8505172d31dd2b4ef78048559f8 for build container...
Running on runner-ce8757c9-project-907-concurrent-0 via VERD842...
Fetching changes...
Removing node_modules/
HEAD is now at 63cccc5 update ci - dev
From https://gitlab.mycompany.com/project1/ci-demo
   63cccc5..98d347e  dev        -> origin/dev
Checking out 98d347e5 as dev...
Skipping Git submodules setup
Checking cache for default...
Successfully extracted cache
$ git push origin HEAD:integration
remote: You are not allowed to upload code for this project.
fatal: unable to access 'https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mycompany.com/project1/ci-democi-demo.git/': The requested URL returned error: 403
ERROR: Job failed: exit code 1

我已经阅读了文档和一些示例,但是我无法弄清楚如何使其工作?

我应该创建一个名为gitlab-ci-token的用户吗?

我应该在bash脚本中进行分支推广吗?

欢迎随时给我任何有关我尝试实现的流水线的反馈意见...

此致敬礼


1
  1. 您是否已向此流水线提供了git凭据?
  2. git凭据是否正确?
  3. 当设置GIT_TRACING=2和GIT_CURL_VERBOSE=1时,git调试输出了什么?
  4. 您尝试推送的远程分支是否受到保护?
- Kalpa Gunarathna
2个回答

11
为了在Gitlab CI runner内推送到一个仓库,你需要使用一个具有对要推送到的分支的推送权限的用户。我们使用以下设置来完成这一点(我们让Gitlab CI标记发布并将其推送)。
  1. 创建一个名为gitlab-ci新Gitlab用户
  2. 创建一个SSH密钥对并将公钥添加到Gitlab中的gitlab-ci用户SSH密钥
  3. 给gitlab-ci用户推送到您的仓库的权限(开发者角色)
  4. 私钥的内容添加为名为SSH_PRIVATE_KEYCI/CD机密变量
这样私钥将在CI作业中可用,接下来我的CI作业的第一部分看起来像这样:
script:
    # Install ssh-agent through openssh-client if not present
    - 'which ssh-agent || ( apt-get update -qy && apt-get install openssh-client -qqy )'
    # Add the private key to this user
    - eval $(ssh-agent -s) && ssh-add <(echo "$SSH_PRIVATE_KEY") && mkdir -p ~/.ssh
    # Docker specific settings
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    # Config git to avoid first usage questions. Set the identity
    - git config --global user.email "noreply@example.com" && git config --global user.name "Gitlab CI"
    # 
    # Do Git stuff, for example:
    #
    - git checkout $CI_COMMIT_REF_NAME
    - git tag my-release-1.0
    - git push -u origin my-release-1.0

免责声明:仅在Gitlab CI runner设置被处理后使用此方法,因为您将分发具有潜在访问权限的私有SSH密钥到您的存储库中,所以必须小心谨慎使用。


1
非常感谢! 这对我很有帮助,我在这里使用了一个脚本:https://github.com/IlyaSemenov/gitlab-ci-git-push - mimiz
7
创建一个独立的用户有点麻烦,Gitlab 有一个部署密钥系统,通过该系统你可以启用推送访问权限而无需创建虚拟用户。 - Caleb
有没有办法使用不同的用户名而不是gitlab-ci? - Damir Porobic
当然,你称呼用户的方式并不重要。它是由键值来识别的。 - Stefan van Gastel
@Ced 文档 是一个很好的起点。 - Caleb

0

尝试使用以下解决方案: Gitlab runner failing -urgent

打开并编辑/etc/gitlab-runner/config.toml文件。

然后添加以下代码:

extra_hosts = [“yourhostname:youripddress”]

例如文件:

[[runners]]
name = "co1"
url = "http://co1/ci"
token = “1ccc03308b71a1c9a10640326adf1a"
executor = “docker”
[runners.docker]
tls_verify = false
image = “co1:5000/pub/pub"
privileged = false
disable_cache = false
volumes = [”/cache”]
shm_size = 0
extra_hosts = [“co1:192.168.176.137”]
[runners.cache]

它不会。这是用于主机名解析的。 - DarkFranX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接