【问题标题】:how to execute git commands in gitlab-ci scripts如何在 gitlab-ci 脚本中执行 git 命令
【发布时间】:2019-08-29 08:24:26
【问题描述】:

我想在 gitlab-ci 管道中更改文件并提交更改

我尝试在脚本中编写普通的 git 命令

script:
    - git clone git@gitlab.url.to.project.git
    - cd project file
    - touch test.txt
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git add .
    - git commit -m "testing autocommit"
    - git push

我找不到命令 git 或类似的东西,我知道它与标签有关,但如果我尝试添加一个 git 标签,它会说没有活动的跑步者。有人知道如何在 gitlab-ci 上运行 git 命令吗?

【问题讨论】:

  • 你得到的确切错误是什么,你得到像“git:找不到命令”这样的错误吗?

标签: gitlab-ci


【解决方案1】:

首先,您需要确保您可以实际使用 git,因此要么在具有 git 的系统上的 shell 执行器上运行您的作业,要么使用 docker 执行器并使用具有git 已安装。

您将遇到的下一个问题是您无法推送到 Git(lab),因为您无法输入凭据。

所以解决方法是创建一个ssh密钥对,通过CI/CD variables将ssh私钥加载到你的CI环境中,同时给你添加对应的公钥your Git(lab) account

来源:https://about.gitlab.com/2017/11/02/automating-boring-git-operations-gitlab-ci/

您的.gitlab-ci.yml 将如下所示:

job-name:
  stage: touch
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$GIT_SSH_PRIV_KEY")
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - mkdir -p ~/.ssh
    - cat gitlab-known-hosts >> ~/.ssh/known_hosts
  script:
    - git clone git@gitlab.url.to.project.git
    - cd project file
    - touch test.txt
    - git add .
    - git commit -m "testing autocommit"
    - git push

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    相关资源
    最近更新 更多