如何解决“远程:您不允许上传代码”。 GitLab CI/CD 作业出错?

     2023-02-19     74

关键词:

【中文标题】如何解决“远程:您不允许上传代码”。 GitLab CI/CD 作业出错?【英文标题】:How to solve the "remote: You are not allowed to upload code." error on GitLab CI/CD job? 【发布时间】:2018-12-27 10:26:58 【问题描述】:

我目前正在尝试使用 GitLab 运行 CI/CD 作业,该作业运行 Python 文件,该文件对特定存储库进行更改,然后提交并将这些更改推送到 master。我在存储库中也有 Master 的角色。似乎所有git 函数都运行良好,除了git push 导致fatal: You are not currently on a branch. 和使用git push origin HEAD:master --force 导致fatal: unable to access 'https://gitlab-ci-token:xxx@xxx/project.git/': The requested URL returned error: 403。我一直在网上寻找解决方案,一个是this one,另一个是unprotecting,但还没有找到我想要的东西。这也是 GitLab 存储库中的一个子项目。

现在,这几乎就是我的.gitlab-ci.yml 的样子。

before_script:
  - apt-get update -y
  - apt-get install git -y
  - apt-get install python -y
  - apt-get python-pip -y

main:
  script:
    - git config --global user.email "xxx@xxx"
    - git config --global user.name "xxx xxx"
    - git config --global push.default simple
    - python main.py

我的main.py 文件本质上具有在内部目录中创建新文件的功能,前提是该文件尚不存在。它的外观类似于以下内容:

import os
import json

def createFile(strings):
    print ">>> Pushing to repo...";
    if not os.path.exists('files'):
        os.system('mkdir files');
    for s in strings:
        title = ("files/"+str(s['title'])+".json").encode('utf-8').strip();
        with open(title, 'w') as filedata:
            json.dump(s, filedata, indent=4);
    os.system('git add files/');
    os.system('git commit -m "Added a directory with a JSON file in it..."');
    os.system('git push origin HEAD:master --force');

createFile(["title":"A", "title":"B"]);

我不完全确定为什么这种情况不断发生,但我什至尝试修改存储库设置以更改 protected 拉取和推送访问,但是当我点击保存时,它实际上并没有保存。尽管如此,这是我的整体输出。我非常感谢任何可以提供的指导。

 Running with gitlab-runner 10.4.0 (00000000)
      on cicd-shared-gitlab-runner (00000000)
 Using Kubernetes namespace: cicd-shared-gitlab-runner
 Using Kubernetes executor with image ubuntu:16.04 ...
 Waiting for pod cicd-shared-gitlab-runner/runner-00000000-project-00000-concurrent-000000 to be running, status is Pending
 Waiting for pod cicd-shared-gitlab-runner/runner-00000000-project-00000-concurrent-000000 to be running, status is Pending
 Running on runner-00000000-project-00000-concurrent-000000 via cicd-shared-gitlab-runner-0000000000-00000...
 Cloning repository...
 Cloning into 'project'...
 Checking out 00000000 as master...
 Skipping Git submodules setup
 $ apt-get update -y >& /dev/null
 $ apt-get install git -y >& /dev/null
 $ apt-get install python -y >& /dev/null
 $ apt-get install python-pip -y >& /dev/null
 $ git config --global user.email "xxx@xxx" >& /dev/null
 $ git config --global user.name "xxx xxx" >& /dev/null
 $ git config --global push.default simple >& /dev/null
 $ python main.py
 [detached HEAD 0000000] Added a directory with a JSON file in it...
  2 files changed, 76 insertions(+)
  create mode 100644 files/A.json
  create mode 100644 files/B.json
 remote: You are not allowed to upload code.
 fatal: unable to access 'https://gitlab-ci-token:xxx@xxx/project.git/': The requested URL returned error: 403
 HEAD detached from 000000
 Changes not staged for commit:
    modified:   otherfiles/otherstuff.txt
 no changes added to commit
 remote: You are not allowed to upload code.
 fatal: unable to access 'https://gitlab-ci-token:xxx@xxx/project.git/': The requested URL returned error: 403
 >>> Pushing to repo...
 Job succeeded

【问题讨论】:

remote: GitLab: You are not allowed to push code to protected branches on this project的可能重复 我面临同样的错误消息,它与上面提到的 SO 问题不同,它是从 Gitlab CI 管道中提出的,错误消息略有不同。提供的答案虽然很好,但无助于解决我的问题。如果有人有想法,请分享一下 【参考方案1】:

这里有一个来自 Gitlab 的资源,描述了如何在 CI 管道中提交到存储库:https://gitlab.com/guided-explorations/gitlab-ci-yml-tips-tricks-and-hacks/commit-to-repos-during-ci/commit-to-repos-during-ci

尝试配置您的 gitlab-ci.yml 文件以推送更改,而不是尝试从 python 文件中进行。

【讨论】:

【参考方案2】:

我设法通过 ssh 在跑步者上做到这一点,确保添加了 ssh 密钥,然后使用完整的 git url:

task_name:
  stage: some_stage
  script:
    - ssh-add -K ~/.ssh/[ssh key]
    - git push -o ci-skip git@gitlab.com:[path to repo].git HEAD:[branch name]

如果是同一个repo触发了job,url也可以写成:

git@$CI_SERVER_HOST:$CI_PROJECT_PATH.git

【讨论】:

你能解释一下你是如何把 SSH 密钥放到 CI 运行器上的吗?您是否必须通过存储库 CI 参数或其他方式预安装 ssh 密钥文件? 我手动将 ssh 私钥添加到构建代理中。不过,您也许可以将其添加为运行器文件变量。但我不能谈论这里的安全风险。【参考方案3】:

The requested URL returned error: 403

HTTP 403 Forbidden客户端错误状态响应码表示服务器理解请求但拒绝授权。 问题是我们无法为 git 提供有效的身份验证,因此我们的请求被禁止。 试试这个:Control Panel => User Accounts => Manage your credentials => Windows Credentials

它对我有用。但是我不太确定它是否对你有用。

【讨论】:

【参考方案4】:

此方法可用于提交标签或文件。您可能还希望考虑使用 CI CD 如果不必将其提交到存储库,则用于存储交叉构建持久数据的可变 API https://docs.gitlab.com/ee/api/project_level_variables.html https://docs.gitlab.com/ee/api/group_level_variables.html

下面的 ACCESS_TOKEN 是 repo 或上行组级别的变量,其中包含一个令牌 可以写入目标仓库。由于维护者可以看到这些,因此最好的做法是 为特殊的 API 用户创建令牌,这些用户在他们需要做的事情上拥有最少的特权。

write_to_another_repo:
  before_script:
    - git config --global user.name "$GITLAB_USER_NAME"
    - git config --global user.email "$GITLAB_USER_EMAIL"
  script:
    - |
      echo "This CI job demonstrates writing files and tags back to a different repository than this .gitlab-ci.yml is stored in."
      OTHERREPOPATH="guided-explorations/gitlab-ci-yml-tips-tricks-and-hacks/commit-to-repos-during-ci/pushed-to-from-another-repo-ci.git" 
      git clone https://gitlab-ci-token:$CI_JOB_TOKEN@$CI_SERVER_HOST/$OTHERREPOPATH
      cd pushed-to-from-another-repo-ci
      CURRENTDATE="$(date)"
      echo "$CURRENTDATE added a line" | tee -a timelog.log
      git status
      git add timelog.log
      # "[ci skip]" and "-o ci-skip" prevent a CI trigger loop
      git commit -m "[ci skip] updated timelog.log at $CURRENTDATE"
      git push -o ci-skip http://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$OTHERREPOPATH HEAD:master
      #Tag commit (can be used without commiting files)
      git tag "v$(date +%s)"
      git tag
      git push --tags http://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$OTHERREPOPATH HEAD:master

【讨论】:

尝试将现有 git 项目上传到 gitlab 时如何解决“预接收挂钩被拒绝”错误?

】尝试将现有git项目上传到gitlab时如何解决“预接收挂钩被拒绝”错误?【英文标题】:Howtofixthe"pre-receivehookdeclined"errorwhentryingtouploadanexistinggitprojecttogitlab?【发布时间】:2022-01-0112:06:59【问题描述】:我们的项目由另一... 查看详情

gitlab中fork库和主库存在代码冲突时如何解决

参考技术A最简单的方法。用RadioButton控件就行了。何苦搞得这么累呢。如果真要这样做也可以就是五个控件共用一个事件同时订阅相同事件然后再判断你选中的Checkbox其他的为假代码如下:usingSystem;usingSystem.Collections.Generic;usingSys... 查看详情

Gitlab:如何在没有身份验证的情况下通过用户 ID 获取问题

】Gitlab:如何在没有身份验证的情况下通过用户ID获取问题【英文标题】:GitLab:Howtogetissuesbyuseridwithoutauthentication【发布时间】:2017-12-1819:22:48【问题描述】:按照GitLabApiDocumentation,我找不到解决问题的方法。需要使用GitLabapi获... 查看详情

如何排除 gitlab-ci.yml 更改触发作业

】如何排除gitlab-ci.yml更改触发作业【英文标题】:Howtoexcludegitlab-ci.ymlchangesfromtriggeringajob【发布时间】:2021-12-0907:29:48【问题描述】:我无法找到解决方案来解决如何忽略.gitlab-ci.yml中所做的更改以触发工作。到目前为止,我已... 查看详情

如何在 Gitlab Omnibus 服务器旁边为其他虚拟主机提供服务? [完整的逐步解决方案]

】如何在GitlabOmnibus服务器旁边为其他虚拟主机提供服务?[完整的逐步解决方案]【英文标题】:HowtoserveothervhostsnexttoGitlabOmnibusserver?[Fullstep-by-stepsolution]【发布时间】:2015-10-2402:48:17【问题描述】:我在带有Omnibus包的专用Ubuntu14.0... 查看详情

如何访问 gitlab 问题迭代更改

】如何访问gitlab问题迭代更改【英文标题】:Howtoaccessgitlabissueiterationchanges【发布时间】:2021-11-1715:45:57【问题描述】:当我更改问题的迭代时,我正试图从gitlab中获取信息。含义:“当我将一张票从Sprint5移动到Sprint6时”。我... 查看详情

如何设置动态 gitlab-ci 文件

】如何设置动态gitlab-ci文件【英文标题】:Howtosetupadynamicgitlab-cifile【发布时间】:2019-11-1302:35:45【问题描述】:我正在尝试设置一个动态gitlab-ci文件,其中包含基于项目变量所需的gitlab-ci文件。不幸的是,我似乎无法让它工作... 查看详情

gitlab宕机无法再次重启解决办法

问题:在gitlab由于某些问题挂机,或无法再次启动的情况下,如何将已有项目导出到新的gitlab服务器中 解决:gitlab有自己的备份命令和定时备份配置,但是如果既没有定时备份,而备份命令在挂机的情况也无法执行,这是灾难性的时... 查看详情

gitlab-500问题解决办法

安装12-0-0版本并进行汉化后,开始创建其他组用户。使用其他用户登陆页面时候出现500错误首先查看gitlab运行状态,发现并无异常[root@gitlab~]#gitlab-ctlstatus 查网上资料得知,GitLab发现报500错误,大意为DB数据关系错误,需要升... 查看详情

gitlab无法显示头像的解决方法

...Gravatar的头像,而Gravatar目前是被墙的,所以访问不了。2.解决方案更换其URL为国内的某个镜像URL。3.解决方法我这里是docker安装的,所以先进入docker镜像中dockerexec-itgitlab/bin/bashvim/etc/gitlab/gitlab.rb&nbs 查看详情

gitlab502问题解决

GitLab出现502情况之一:存储空间占用百分百。1、删除空间其他多余文件,释放空间。2、数据迁移至新挂在盘,例如:/data盘。 首先关闭服务gitlab-ctlstop部分进程关闭不了,kill掉。 GitLab默认存储数据位置 /var/opt/gitlab/g... 查看详情

如何直接从我的 Gitlab 存储库部署到 Heroku

】如何直接从我的Gitlab存储库部署到Heroku【英文标题】:HowtodeploytoHerokudirectlyfrommyGitlabrepository【发布时间】:2016-08-3102:50:14【问题描述】:在我的团队中,我们使用Gitlab作为远程存储库,因此我们正在寻找一种将我们的应用程... 查看详情

gitlab提交时间显示错误解决办法

...该是时间戳转换出错,导致提交一天的文件显示几年前。解决办法:1.进入dockerdockerexec-itgitlab/bin/bash2.vim/etc/gitlab/gitlab.rb启用timezone,并将UTC改为Asia/Shanghai 3. rm-rf/var/opt/gitlab/prometheus/ 查看详情

Maven 无法使用 gitlab 解决依赖关系

】Maven无法使用gitlab解决依赖关系【英文标题】:MavenCouldnotresolvedependencyusinggitlab【发布时间】:2021-08-2716:07:24【问题描述】:我需要使用maven解决依赖关系但是它给出了一个错误。我正在使用GitLab将工件存储在包注册表下。下面... 查看详情

安装 Gitlab 服务器作为 HA 解决方案

】安装Gitlab服务器作为HA解决方案【英文标题】:InstallingaGitlabServerasaHAsolution【发布时间】:2019-02-1512:21:34【问题描述】:我正在尝试为我的公司提出一个解决方案,其中我们有一个带有GitLab的高可用性解决方案。我知道如果我... 查看详情

gitlab自带nginx与原nginx冲突的解决方案

...用80端口,这样就与系统原本安装的Nginx冲突。有完美的解决方案吗?当然!CentOS7安装请参考将其中的80改为其它端口即可,如我的是886,执行gitlab-ctlrestart重启gitlab等待网关恢复,重新访问:http://ip:886即可禁用捆绑的Nginxvim/etc/g... 查看详情

国内注册gitlab账号失败解决办法

...资料,发现是由于墙了谷歌的原因,导致无法注册成功。解决方案:1、搭梯子。2、找国外的朋友帮忙注册。3、最实际的做法:先注册一个github的账户,然后在登录gitlab的时候使用第三方登录选择github账户,这样可以绕过gitlab的... 查看详情

宝塔面板安装gitlab提示配置错误解决办法

原文链接: https://www.xgboke.com/34862.html宝塔面板在安装gitlab时会遇到安装以提示已完成,在实际设置时提示“未找到相关配置,gitlab可能已损坏”,此错误安装“gitlab中文社区办”和“gitlab最新社区版”均有可能遇到此错误,... 查看详情