我目前正在尝试使用GitLab运行CI / CD作业,该作业运行一个Python文件,该Python文件对特定存储库进行更改,然后提交并将这些更改推送到master。在存储库中,我还担任过Master的角色。看来所有git
函数运行正常,除了git push
导致fatal: You are not currently on a branch.
并使用git push origin HEAD:master --force
导致fatal: unable to access 'https://gitlab-ci-token:xxx@xxx/project.git/': The requested URL returned error: 403
之外。我一直在寻找在线解决方案,一个是this one,另一个是unprotecting,但我现在还找不到我想要的东西。这也是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
答案 0 :(得分:1)
通过确保添加了 ssh 密钥,然后使用完整的 git url,我设法通过 ssh 在 runner 上执行此操作:
task_name:
stage: some_stage
script:
- ssh-add -K ~/.ssh/[ssh key]
- git push -o ci-skip git@gitlab.com:[path to repo].git HEAD:[branch name]
如果是触发作业的同一个repo,url也可以写成:
git@$CI_SERVER_HOST:$CI_PROJECT_PATH.git
答案 1 :(得分:0)
The requested URL returned error: 403
Control Panel => User Accounts => Manage your credentials => Windows Credentials
它对我有用。但是我不确定它是否对您有用。
答案 2 :(得分:0)
以下是来自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文件中进行更改。
答案 3 :(得分:0)
此方法可用于提交标签或文件。您也可以考虑使用 CI CD 变量 API 用于存储跨构建持久数据,如果它不必提交到存储库 https://docs.gitlab.com/ee/api/project_level_variables.html https://docs.gitlab.com/ee/api/group_level_variables.html
下面的 ACCESS_TOKEN 是回购或上行组级别的变量,其中包含一个令牌 可以写入目标存储库。由于维护者可以看到这些,最好的做法是 为特殊的 API 用户创建令牌,这些用户只需要执行他们需要做的事情。
write_to_another_repo:
before_script:
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script:
- |
echo "This CI job demonstrates writing files and tags back to a different repository than this .gitlab-ci.yml is stored in."
OTHERREPOPATH="guided-explorations/gitlab-ci-yml-tips-tricks-and-hacks/commit-to-repos-during-ci/pushed-to-from-another-repo-ci.git"
git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@$CI_SERVER_HOST/$OTHERREPOPATH
cd pushed-to-from-another-repo-ci
CURRENTDATE="$(date)"
echo "$CURRENTDATE added a line" | tee -a timelog.log
git status
git add timelog.log
# "[ci skip]" and "-o ci-skip" prevent a CI trigger loop
git commit -m "[ci skip] updated timelog.log at $CURRENTDATE"
git push -o ci-skip http://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$OTHERREPOPATH HEAD:master
#Tag commit (can be used without commiting files)
git tag "v$(date +%s)"
git tag
git push --tags http://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$OTHERREPOPATH HEAD:master
答案 4 :(得分:-1)
因为您尝试推送到master
,所以403表示请求已被理解,但未获得许可。
您的分支机构(例如,主分支机构)可能受到了直接推送的保护,
在您的git仓库中检查它,转到settings->repository->protected branches
。
从受保护的分支列表中删除master
或将更改推送到其他分支