您如何使用gitlab-ci作业推送到gitlab存储库? [英] How do you push to a gitlab repo using a gitlab-ci job?

查看:136
本文介绍了您如何使用gitlab-ci作业推送到gitlab存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是不熟悉GitLab CI/CD作业的人,但是我试图建立一个Python脚本,当将其推送到GitLab时,它会触发CI/CD作业来运行它,并调用一个内部函数来再次推送到GitLab只要满足某些条件.因此,例如,假设我有以下内容:

I am new to GitLab CI/CD jobs, but I'm trying to set up a Python script that when pushed to GitLab, triggers the CI/CD job to run it, and call an internal function that pushes to GitLab again provided that certain criteria are met. So, for example, suppose I have the following:

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作业中运行.我得到的输出是:

This function seems to run perfectly fine if I run it myself from the command line, however, it seems to not be able to run in the GitLab CI/CD job. The output I am getting is:

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时会发生此错误,所以我想知道如何解决此问题.我将非常感谢您的帮助!

This error occurs when I call git push so I was wondering what I could do to fix this. I would really appreciate any help!

推荐答案

GitLab CI运行程序还不能推送回购协议:存在一个

A GitLab CI runner cannot yet push to a repo: there is a proposal in progress here.

同时,您可以使用SSH URL ,并使用:

In the meantime, you can use an SSH URL, with:

  • 通过GitLab中的设置">"CI/CD管道" Web界面,将SSH私钥定义为秘密变量,
  • SSH密钥的公共部分存储为同一Web UI的部署密钥设置">存储库">部署密钥"部分.

或者,如此处提到的 /a>,则可以在个人资料的设置"中使用个人访问令牌".

Or, as mentioned here, you can use a "personal access token" in Settings of your profile.

我使用范围api创建了令牌,并在管道中进行配置.
在gitlab控制台中打开项目,转到设置">"CI/CD">秘密变量",创建一个值为key(在配置文件中生成)的变量.
我将"$ {CI_JOB_TOKEN}"替换为变量"$ {VAR01}".

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

With a 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变量

这篇关于您如何使用gitlab-ci作业推送到gitlab存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆