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

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

问题描述

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

def hasFileInDirectory():# 检查当前目录中是否至少有 1 个其他文件如果(存在 1 个或多个文件):打印'太好了!你有足够的文件!';别的:打印'哦不!您需要更多文件!让我创造一个!';createFile('missingFile'+str(random.randint(0,1000000)+'.txt');os.system('git add -A');os.system('git commit -m "自动添加新文件..."');os.system('git push origin HEAD:master --force');

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

remote:不允许上传代码.致命:无法访问https://gitlab-ci-token:xxxxx@gitlab.com/path_to/my_repository.git/":请求的 URL 返回错误:403

当我调用 git push 时会发生此错误,所以我想知道我能做些什么来解决这个问题.非常感谢任何帮助!

解决方案

GitLab CI 运行器还不能推送到 repo:有一个 这里的提案正在进行中.

与此同时,您可以使用 SSH URL,其中:

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

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

<块引用>

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

使用 gitlab-ci.yml

<块引用>

脚本:- 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 变量

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');

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

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!

解决方案

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

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

  • An SSH private key is defined as a secret variable through the Settings > CI/CD Pipelines web interface in GitLab, and
  • the public part of the SSH key is stored as a deployment key Settings > Repository > Deploy Keys section of the same web UI.

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

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}".

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 is the Secret variable

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

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