GitLab运行程序,在推送时出错,但从控制台在git bash或python上也可以使用相同的东西

问题描述 投票:1回答:1

上下文

我们正在尝试做一个GitLab运行器工作,该工作在某个标签上修改版本头文件,并向此变更集添加一个发布分支/标签。

GitLab运行器服务器在我的机器上,由我的用户作为服务启动(已正确注册到我们的GitLab服务器)。

GitLab运行程序工作基本上会启动一个使用gitpython进行作业的python脚本,运行程序yml文件中只有几处更改(添加了before_script部分以具有上载权限,并从那里获得了它:[C0 ]),这是完整的https://stackoverflow.com/a/55344804/11159476文件:

.gitlab-ci.yml

[在推送时还在python URL中添加了技巧(使用variables: GIT_SUBMODULE_STRATEGY: recursive stages: [ build, publish, release ] release_tag: stage: build before_script: - git config --global user.name ${GITLAB_USER_NAME} - git config --global user.email ${GITLAB_USER_EMAIL} script: - python .\scripts\release_gitlab_runner.py only: # Trigger on specific regex... - /^Src_V[0-9]+\.[0-9]+\.[0-9]+$/ except: # .. only for tags then except branches, see doc (https://docs.gitlab.com/ee/ci/yaml/#regular-expressions): "Only the tag or branch name can be matched by a regular expression." - branches 推送而不是默认运行器URL,从上面的相同答案中得到了,并且已经从公司gitlab生成了令牌=>用户“ Settings” =>“访问令牌“ =>”添加具有所有权利且永不过期的个人访问令牌“,这里不是实际的user:personal_access_token@repo_URL python脚本,而是简化为具有一个git flow尽可能符合我们想要的标准的脚本(全部获取) ,使用随机名称创建本地分支,使其不存在,修改文件,暂存,提交并最终推送):

scripts\release_gitlab_runner.py

问题

即使拥有权限的技巧,当我们尝试从GitLab运行程序推送时,也会引发以下错误:

# -*-coding:utf-8 -*

import uuid
import git
import sys
import os

# Since we are in <git root path>/scripts folder, git root path is this file's path parent path
GIT_ROOT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
try:
    # Get user login and URL from GITLAB_USER_LOGIN and CI_REPOSITORY_URL gitlab environment variables
    gitlabUserLogin = os.environ["GITLAB_USER_LOGIN"]
    gitlabFullURL = os.environ["CI_REPOSITORY_URL"]
    # Push at "https://${GITLAB_USER_NAME}:${PERSONAL_ACCESS_TOKEN}@gitlab.companyname.net/demo/demo.git")
    # generatedPersonalAccessToken has been generated with full rights from https://gitlab.companyname.net/profile/personal_access_tokens and set in a variable not seen here
    gitlabPushURL = "https://{}:{}@{}".format(gitlabUserLogin, generatedPersonalAccessToken, gitlabFullURL.split("@")[-1])
    print("gitlabFullURL is [{}]".format(gitlabFullURL))
    print("gitlabPushURL is [{}]".format(gitlabPushURL))

    branchName = str(uuid.uuid1())

    print("Build git.Repo object with [{}] root path".format(GIT_ROOT_PATH))
    repo = git.Repo(GIT_ROOT_PATH)

    print("Fetch all")
    repo.git.fetch("-a")

    print("Create new local branch [{}]".format(branchName))
    repo.git.checkout("-b", branchName)

    print("Modify file")
    versionFile = os.path.join(GIT_ROOT_PATH, "public", "include" , "Version.h")
    patchedVersionFileContent = ""
    with open(versionFile, 'r') as versionFileContent:
        patchedVersionFileContent = versionFileContent.read()
        patchedVersionFileContent = re.sub("#define VERSION_MAJOR 0", "#define VERSION_MAJOR {}".format(75145), patchedVersionFileContent)
    with open(versionFile, 'w') as versionFileContent:
        versionFileContent.write(patchedVersionFileContent)

    print("Stage file")
    repo.git.add("-u")

    print("Commit file")
    repo.git.commit("-m", "New version file in new branch {}".format(branchName))

    print("Push new branch [{}] remotely".format(branchName))
    # The error is at below line:
    repo.git.push(gitlabPushURL, "origin", branchName)

    sys.exit(0)
except Exception as e:
    print("Exception: {}".format(e))

    sys.exit(-1)

什么有效

如果打开Git Bash,我将成功运行手动命令:

Cmd('git') failed due to: exit code(1)
  cmdline: git push https://user:[email protected]/demo/repo.git origin 85a3fa6e-690a-11ea-a07d-e454e8696d31
  stderr: 'error: src refspec origin does not match any
error: failed to push some refs to 'https://user:[email protected]/demo/repo.git''

如果我们从其他地方获取所有内容,则可以看到带有版本文件修改的newBranch

同样,如果我们从python命令行运行脚本内容(不进行URL修改)(假设已经执行了脚本中的所有导入操作:]

git fetch -a
git checkout -b newBranch
vim public/include/Version.h
=> At this point file has been modified
git add -u
git commit -m "New version file in new branch"
git push origin newBranch

结论

从GitLab运行程序运行时,我找不到我做错的事情,我缺少什么吗?

[从GitLab运行程序运行时,我唯一看到的不同是,在获取之后,我可以看到我在一个分离的头上(例如,列表GIT_ROOT_PATH = "E:\\path\\to\\workspace\\repo" branchName = str(uuid.uuid1()) repo = git.Repo(GIT_ROOT_PATH) repo.git.fetch("-a") repo.git.checkout("-b", branchName) versionFile = os.path.join(GIT_ROOT_PATH, "public", "include" , "Version.h") patchedVersionFileContent = "" with open(versionFile, 'r') as versionFileContent: patchedVersionFileContent = versionFileContent.read() patchedVersionFileContent = re.sub("#define VERSION_MAJOR 0", "#define VERSION_MAJOR {}".format(75145), patchedVersionFileContent) with open(versionFile, 'w') as versionFileContent: versionFileContent.write(patchedVersionFileContent) repo.git.add("-u") repo.git.commit("-m", "New version file in new branch {}".format(branchName)) repo.git.push("origin", branchName) 给出了['*(HEAD在560976b处分离)),'branchName ','remotes / origin / otherExistingBranch',...]),但这应该不是问题,因为我创建了一个新的分支来推送,对吗?

python gitlab gitlab-ci-runner git-push gitpython
1个回答
0
投票

Git说您使用了错误的refspec。当需要推入其他遥控器时,必须先将其推入repo.git.branch('-a').split('\n'),然后像gitlab = repo.create_remote("gitlab", gitlabPushURL)一样推入它。

© www.soinside.com 2019 - 2024. All rights reserved.