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

标签 python linux gitlab gitlab-ci gitlab-ci-runner

我目前正在尝试使用 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/

相关文章:

go - 如何使用gitlab(Go Env)制作Linux可执行文件?

python - 值错误: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' , 'occurred at index 0')

python - 来自 HTML 表中相同表单类的多个 WTForm 字段?

linux - 在 scala shell 上使用向上箭头进行历史/部分搜索

linux - Buildroot:运行Lighttpd

gitlab - 将存储库从 Gitlab 服务器导出到另一个 Gitlab 服务器

gitlab - 如何使用gitlab API获取子文件夹和文件

python - 将数组传递给 Spark Lit 函数

python - 循环遍历字符串

linux - 抑制makefile中命令调用的回声?