如何使用gitlab ci作业推动gitlab回购?
我不熟悉GitLab CI/CD作业,但我正在尝试设置一个Python脚本,当推送到GitLab时,它会触发CI/CD作业来运行它,并调用一个内部函数,在满足某些条件的情况下再次推送到GitLab。例如,假设我有以下内容:如何使用gitlab ci作业推动gitlab回购?,git,python-2.7,gitlab,gitlab-ci,gitlab-ci-runner,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): pr
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
这个错误发生在我调用gitpush时,所以我想知道我能做些什么来解决这个问题。我真的很感激任何帮助 GitLab CI运行程序还不能推送到回购:存在一个 同时,您可以:
- SSH私钥通过GitLab中的Settings>CI/CD Pipelines web界面定义为秘密变量,并且
- SSH密钥的公共部分存储为同一web UI的部署密钥设置>存储库>部署密钥部分
在gitlab控制台中打开项目,进入设置>CI/CD>秘密变量,创建一个具有密钥值的变量(在概要文件中生成)。
我将“${CI_JOB_TOKEN}”替换为我的变量“${VAR01}” 使用gitlab-ci.yml
CI\u TAG\u UPLOAD\u TOKEN
是秘密变量我们最终使用的命令是:
git标记我的新标记
git推式回购=git@YOUR_REPO_URL:YOUR_GROUP/YOUR_PROJECT.git--标记
默认情况下,我们得到:
remote: You are not allowed to upload code.
fatal: unable to access 'https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@YOUR_REPO_URL:YOUR_FORK/YOUR_PROJECT.git/': The requested URL returned error: 403`
原因是CI运行程序使用HTTPS协议执行git命令,该协议使用的令牌不支持@VonC所述的推送
我们已将运行程序配置为将卷共享到/root/.ssh
目录。因此,使用git
协议和适当的ssh
配置,我们可以使用Gitlab CI runner推送git命令
编辑
gitlab
runner的执行如下(为了清晰起见,我删除了无用的参数)
因此,
/root
目录在所有跑步者之间共享。因此,一旦正确配置了/root/.ssh/
正确的密钥,并且这些密钥有权将推送到gitlab,它将如上所述工作。可能重复的,请详细说明“配置我们的运行程序以将卷共享到/root/.ssh目录”部分。是在“.gitlab ci.yml”中吗?我已经编辑了我的回复,以提供更多的细节。希望这能有所帮助。@jtheoof您是说/root已经跨运行程序装载,还是说您必须更改docker exec
的启动方式(如果是,那么如何启动)?问题是,当使用gitlab机器时
,当管道启动时,实际会根据需要生成运行程序,那么我该如何配置/root/.ssh
?顺便说一句,尽管我的令牌拥有所有可能的权限,但我确实会得到403
错误代码。绝对没有通过https推送的解决方案?两种解决方案都是have缺点。“ssh url”不能用于某些只能进行http访问的环境中。“个人访问令牌”是个人的-当CI作业想要更改存储库中的某些内容时-不应该从某个预定义的用户帐户进行更改,而应该从启动作业的用户帐户进行更改。@Ezh所以使用部署令牌()而不是用户令牌?应该可以。并且符合您的条件。@VonC部署令牌不能用于写入存储库。Use可以使用它们读取存储库或读取/写入注册表。@MartinŽid确定,然后没有部署令牌。
remote: You are not allowed to upload code.
fatal: unable to access 'https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@YOUR_REPO_URL:YOUR_FORK/YOUR_PROJECT.git/': The requested URL returned error: 403`
docker exec -it gitlab-runner.service gitlab-runner register \
--non-interactive \
--name `hostname` \
--url "some-gitlab-url" \
...
--executor "docker" \
--limit "1" \
--docker-image "debian" \
--docker-volumes "/var/run/docker.sock:/var/run/docker.sock" \
--docker-volumes "/fs1/runner/builds:/builds" \
--docker-volumes "/fs1/runner/cache:/cache" \
--docker-volumes "/fs1/runner/profile:/root" \