gpt4 book ai didi

python - 如何解决 GitLab CI/CD 作业中的 "remote: You are not allowed to upload code."错误?

转载 作者:太空宇宙 更新时间:2023-11-03 12:00:10 25 4
gpt4 key购买 nike

我目前正在尝试使用 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/':请求的 URL 返回错误: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

最佳答案

这是来自 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 文件中执行。

关于python - 如何解决 GitLab CI/CD 作业中的 "remote: You are not allowed to upload code."错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51420002/

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