GitLab运行程序,在推送时出错,而从控制台在git bash或python上也可以运行

上下文

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

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

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

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

还在推送时在python URL中添加了技巧(使用user:personal_access_token@repo_URL推送而不是默认运行者URL,从上面的相同答案中获得了答案,并且令牌已由公司gitlab =>用户“ Settings” =>生成。 “ access Tokens” =>“具有所有权利的永不过期的添加个人访问令牌”,并且永不过期),这不是实际的scripts\release_gitlab_runner.py python脚本,而是简化为具有git flow尽可能标准的脚本我们想要(全部获取,使用随机名称创建本地分支,以使其不存在,修改文件,暂存,提交并最终推送):

# -*-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)

问题

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

Cmd('git') failed due to: exit code(1)
  cmdline: git push https://user:token@gitlab.companyname.net/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:token@gitlab.companyname.net/demo/repo.git''

什么有效

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

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

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

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

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,"Version.h")
patchedVersionFileContent = ""
with open(versionFile,'r') as versionFileContent:
    patchedVersionFileContent = versionFileContent.read()
    patchedVersionFileContent = re.sub("#define VERSION_MAJOR 0",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)

结论

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

从GitLab运行程序运行时,我唯一看到的不同是在获取之后,我可以看到我在一个分离的头上(列出repo.git.branch('-a').split('\n')给出了[[*(HEAD在560976b分离头))” ,“ branchName”,“ remotes / origin / otherExistingBranch”,...]),但这应该不是问题,因为我创建了一个新的分支来推送,对吗?

since1129 回答:GitLab运行程序,在推送时出错,而从控制台在git bash或python上也可以运行

Git说您使用了错误的refspec。当您需要推入其他遥控器时,必须先使其gitlab = repo.create_remote("gitlab",gitlabPushURL),然后像repo.push("gitlab",branchName)那样推入它。

从@gluttony编辑,以使其在下一次使用“远程已存在”运行git时不会中断:

remote_name = "gitlab"
if remote_name not in repo.remotes:
    repo.create_remote(remote_name,gitlabPushURL)
本文链接:https://www.f2er.com/2624695.html

大家都在问