Gitlab Ci 无法从 runner 推送一个分支

Gitlab Ci unable to push on a branch from runner

我正在尝试使用 Gitlab 设置 CI/CD 管道 这是我想做的:

注意:这是一个打字稿项目

  1. 单元测试&&集成测试
  2. 将分支开发提升为分支集成
  3. 从分支集成构建 docker 图像
  4. 部署到集成环境

这是我正在使用的 .gitlab-ci.yml (i:

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 脚本中进行分支推广吗?

请随时给我有关我尝试做的管道的任何反馈...

此致

试试这个解决方案: 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]

要从 Gitlab CI 运行程序中推送到存储库,您需要使用对要推送到的分支具有推送访问权限的用户。我们使用以下设置来完成此操作(我们让 Gitlab CI 标记发布并推送它们)。

  1. 创建一个名为 gitlab-ci
  2. 新 Gitlab 用户
  3. Create a SSH key-pair and add the public key to the gitlab-ci user's SSH keys in Gitlab
  4. 给gitlab-ci用户push access to your repo(开发者角色)
  5. 添加 私钥 的内容作为 CI/CD secret variable 调用 **SSH_PRIVATE_KEY**

这样私钥将在 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 已处理的运行器设置中使用它,您正在分发 SSH 私钥并可能访问您的存储库,因此您必须小心使用。