如何解决“remote: You are not allowed to upload code.” GitLab CI/CD 作业出错?

新手上路,请多包涵

我目前正在尝试使用 GitLab 运行一个 CI/CD 作业,该作业运行一个 Python 文件,该文件对特定存储库进行更改,然后提交并将这些更改推送到 master。我在存储库中也有 Master 的角色。 It appears that all git functions run fine except for the git push , which leads to fatal: You are not currently on a branch. and with using git push origin HEAD:master --force , that leads to fatal: unable to access 'https://gitlab-ci-token:xxx@xxx/project.git/': The requested URL returned error: 403 。我一直在网上寻找解决方案,一个是 这个,另一个是 取消保护,但还不能完全找到我要找的东西。这也是 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

原文由 Chloe Bennett 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
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 文件中执行此操作。

原文由 Kellen 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题