gpt4 book ai didi

Gitlab Ci 无法从运行器推送分支

转载 作者:行者123 更新时间:2023-12-02 08:25:56 27 4
gpt4 key购买 nike

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

注意:这是一个 typescript 项目

  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 运行程序完成,这里是输出控制台:

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 CI 运行程序内推送到存储库,您需要使用对要推送到的分支具有推送访问权限的用户。我们使用以下设置来完成此操作(我们让 Gitlab CI 标记版本并推送它们)。

  1. 创建一个名为 gitlab-ci新 Gitlab 用户
  2. Create a SSH key-pair并将公钥添加到 gitlab-ci user's SSH keys in Gitlab
  3. 给gitlab-ci用户push access to your repo (开发者角色)
  4. 私钥的内容添加为 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 运行程序设置中使用此功能,您正在分发可能访问您的存储库的私有(private) SSH key ,因此您必须谨慎使用此功能。

关于Gitlab Ci 无法从运行器推送分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47523286/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com