git - 你如何使用 gitlab-ci 作业推送到 gitlab 仓库?

标签 git python-2.7 gitlab gitlab-ci gitlab-ci-runner

我是 GitLab CI/CD 作业的新手,但我正在尝试设置一个 Python 脚本,当推送到 GitLab 时,触发 CI/CD 作业运行它,并调用一个内部函数再次推送到 GitLab只要满足某些标准。因此,例如,假设我有以下内容:

def hasFileInDirectory():
    # checks if the current directory has at least 1 other file in it
    if (1 or more files exist):
        print 'Great! You have enough files!';
    else:
        print 'Oh no! You need more files! Let me create one!';
        createFile('missingFile'+str(random.randint(0,1000000)+'.txt');
        os.system('git add -A');
        os.system('git commit -m "Automatically added new file..."');
        os.system('git push origin HEAD:master --force');

如果我自己从命令行运行这个函数,它似乎运行得很好,但是,它似乎无法在 GitLab CI/CD 作业中运行。我得到的输出是:

remote: You are not allowed to upload code.
fatal: unable to access 'https://gitlab-ci-token:xxxxx@gitlab.com/path_to/my_repository.git/': The requested URL returned error: 403

这个错误发生在我调用 git push 时,所以我想知道我能做些什么来解决这个问题。如果有任何帮助,我将不胜感激!

最佳答案

GitLab CI runner 还不能推送到 repo:有一个 proposal in progress here .

与此同时,您可以 use an SSH URL , 与:

  • 通过 GitLab 中的“设置”>“CI/CD 管道”Web 界面将 SSH 私钥定义为 secret 变量,并且
  • SSH key 的公共(public)部分作为部署 key 存储在同一网络用户界面的“设置”>“存储库”>“部署 key ”部分。

或者,作为 mentioned here ,您可以在个人资料的设置中使用“个人访问 token ”。

I created a token with scope api and configure in my pipeline.
Open the project in gitlab console, go to Settings > CI/CD > Secret variables, create a variable with value the key (generated in profile).
I replace “${CI_JOB_TOKEN}” to my variable “${VAR01}”.

用一个gitlab-ci.yml

script:
   - url_host=`git remote get-url origin | sed -e "s/https:\/\/gitlab-ci-token:.*@//g"`
   - git remote set-url origin "https://gitlab-ci-token:${CI_TAG_UPLOAD_TOKEN}@${url_host}"

CI_TAG_UPLOAD_TOKEN 是 Secret 变量

关于git - 你如何使用 gitlab-ci 作业推送到 gitlab 仓库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51465858/

相关文章:

php - Magento 将数据库从开发版更新为上线版

git - .git/info/exclude 替换 git 子模块?

git - 我们可以重新触发 gitlab 管道吗?

linux - 如何设置 Gitlab 钩子(Hook)来验证 git 推送到远程

python - 将Python代码转换为Linux可执行文件

gitlab - git 克隆 : fatal: unable to update url base from redirection:

git - 为什么 git diff 在命令行上对我不起作用?

performance - git 与 mercurial 性能对比

python - 如何迭代生成集合中所有可能的元素组合

python - 安装的 Python 脚本无法导入包模块