如何在 gitlab-ci 脚本中执行 git 命令

Pau*_*aul 3 gitlab-ci

我想更改文件并在 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
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*tel 11

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

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

所以解决方案是创建一个 ssh 密钥对并通过CI/CD 变量将 ssh 私钥加载到您的 CI 环境中,并将相应的公钥添加到您的 Git(lab) 帐户中

来源: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
Run Code Online (Sandbox Code Playgroud)

  • 如果我不想使用我的个人凭据怎么办?我在这里问了一个相关问题(/sf/ask/5054194631/) (3认同)