在 gitlab ci/cd 期间进行的 git push 更改

时间:2021-02-10 17:18:27

标签: continuous-integration gitlab gitlab-ci gitlab-ci-runner

我试图在我正在开发的管道中的工作期间对文件进行更改,然后将该更改提交到同一项目的主分支,但我很难让它工作。< /p>

这是工作:

M = [[0]* n[i] for i in range(nJobs)]
    iter_i = 0
    cellInfo = []
    for i in range(nJobs):
        if i != 0:
            iter_i = iter_i+n[i-1]
        for j in range(n[i]):
            cellInfo = DataSheet.cell(5 + j + iter_i, 5).value
            M[i][j] = [int(y) for y in cellInfo.split(",")]

所以,除了 push 命令之外,一切似乎都有效。这是日志:

maven_next_release:
  stage: next-version
  dependencies:
    - maven_test
  before_script:
    - apt update && apt-get install git perl-base -yrelease-demo.git
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git fetch
    - git checkout master
  script:
    - cat VERSION
    - perl -i -pe 's/\d+\.\d+\.\K(\d+)/ $1+1 /e' VERSION
    - echo $(cat VERSION)-SNAPSHOT > VERSION
    - cat VERSION
    - git add VERSION
    - git commit -m "[skip ci]New version $(cat VERSION)"
    - git push https://${GIT_USERNAME}:${GIT_PASSWORD}@gitlab.com/myproject/release-demo.git
  only:
    - tags
  except:
- branches

我真的不知道该怎么做,我阅读了有关设置 SSH 密钥的信息,这样我就不必传递用户名和密码,但我不确定如何为运行程序生成 SSH 密钥。

>

3 个答案:

答案 0 :(得分:0)

GitLab CI(与 GitHub Actions 不同)不会自动授权您在签出时推送代码。

要实现您的目标,您需要生成 Git Push Token 并将其秘密传递给您的管道。

有关示例 - 您可以在此处参考我的示例 helm cd 项目 - https://gitlab.com/taleodor/sample-helm-cd/

特别是,在文档中搜索“GIT_PUSH_TOKEN”,然后实际的 git commit 部分在“.git-script”块中的 https://gitlab.com/taleodor/sample-helm-cd/-/blob/master/.gitlab-ci.yml 中。

答案 1 :(得分:0)

您可以使用 ssh_key 来代替 https。 您可以在共享运行器或私有运行器 gitlabci 中的容器内添加 ssh_key。

答案 2 :(得分:0)

所以我解决了我的问题:

首先,我之前在我的 ci/cd 中创建了两个环境变量,GIT_USER 和 GIT_PASSWORD,我将它们作为受保护的变量,所以我不得不取消选择它并将它们标记为掩码。

其次,我像这样修改了我的工作:

maven_next_release:
  stage: next-version
  dependencies:
    - maven_test
  before_script:
    - apt update && apt-get install git perl-base -y
    - git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/myteam/release-demo.git &> /dev/null
    - cd release-demo
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git config --global user.name "${GITLAB_USER_NAME}"
  script:
    - cat VERSION
    - perl -i -pe 's/\d+\.\d+\.\K(\d+)/ $1+1 /e' VERSION
    - echo $(cat VERSION)-SNAPSHOT > VERSION
    - cat VERSION
    - git add VERSION
    - git commit -m "[skip ci]Version $(cat VERSION)"
    - git push "https://${GIT_USERNAME}:${GIT_PASSWORD}@${CI_REPOSITORY_URL#*@}" HEAD:master
  only:
    - tags
  except:
    - branches

这样,我的管道终于工作了,可以将更改推送到 master 分支。