代码之家  ›  专栏  ›  技术社区  ›  Dimitri Kopriwa

是否可以在没有ssh的Gitlab CI中执行Git推送?

  •  3
  • Dimitri Kopriwa  · 技术社区  · 7 年前

    我们想知道在技术上是否有可能像在Github那样 git push 使用 https 协议与否 ssh 没有 直接在curl请求中使用用户名和密码。

    我见过一些人认为这是可能的,我们无法证明。

    是否有任何证据或证人可以确认这样一个特性,允许您使用用户访问令牌或 gitlab-ci-token 在CI中?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Vít Kotačka    7 年前

    我无法通过 HTTPS 码头工人 当我在由克隆的存储库中进行更改时执行器 Gitlab运行程序 . 因此,我使用以下解决方法:

    1. 通过将存储库克隆到某个临时位置 HTTPS 用一个 用户访问令牌 .
    2. 做一些 吉特 工作(如合并或标记)。
    3. 将更改推回。

    我有份工作 .gitlab-ci.yml :

    tagMaster:
      stage: finalize
      script: ./tag_master.sh
      only:
      - master
      except:
      - tags
    

    然后我有一个shell脚本 tag_master.sh 具有 吉特 命令:

    #!/usr/bin/env bash
    
    OPC_VERSION=`gradle -q opcVersion`
    CI_PIPELINE_ID=${CI_PIPELINE_ID:-00000}
    
    mkdir /tmp/git-tag
    cd /tmp/git-tag
    git clone https://deployer-token:$DEPLOYER_TOKEN@my.company.com/my-user/my-repo.git
    cd my-repo
    git config user.email deployer@my.company.com
    git config user.name 'Deployer'
    git checkout master
    git pull
    git tag -a -m "[GitLab Runner] Tag ${OPC_VERSION}-${CI_PIPELINE_ID}" ${OPC_VERSION}-${CI_PIPELINE_ID}
    git push --tags
    

    这很管用。

        2
  •  0
  •   Dimitri Kopriwa    7 年前

    我在给我 before_script.sh 可以在任何 .gitlab-ci.yml

    before_script:
      - ./before_script.sh
    

    您只需要设置一个名为 GL_TOKEN GITLAB_TOKEN 在你的项目中。

    if [[ -v "GL_TOKEN" || -v "GITLAB_TOKEN" ]]; then
      if [[ "${CI_PROJECT_URL}" =~ (([^/]*/){3}) ]]; then
        mkdir -p $HOME/.config/git
        echo "${BASH_REMATCH[1]/:\/\//://gitlab-ci-token:${GL_TOKEN:-$GITLAB_TOKEN}@}" > $HOME/.config/git/credentials
        git config --global credential.helper store
      fi
    fi
    

    它不需要更改默认的git策略,并且可以使用默认值在不受保护的分支上正常工作。 gitlab-ci-token .

    在受保护的分支上,可以使用 git push 照常指挥。

    我们停止使用ssh密钥,v_t kota_ka answers帮助我们理解它以前失败的原因。

        3
  •  -2
  •   Hamed    7 年前

    我想您必须在下面的地址将您的公钥添加到Gitlab中

    gitlab.com/profile/keys
    

    因此,您可以在不使用用户名密码的情况下推拉

    推荐文章