How do I enable cloning over SSH for a Gitlab runner?

前端 未结 3 1494
醉话见心
醉话见心 2020-12-31 00:35

I am having some trouble cloning large repositories over HTTP on my Windows Gitlab runner. I\'ve tried several methods to do shallow clones or disable clone compression. Sti

3条回答
  •  感动是毒
    2020-12-31 01:16

    I had a similar problem that necessitated the use of cloning via ssh: using the virtualbox executor with very old guest linux OSes. I was able to get around it by doing a few small configuration changes:

    1. Create a deploy key for access to the project.

    2. Force the user account that will perform the clone to use the deploy key. In my virtualbox case, I modified the ssh configuration for the user that's configured for virtualbox in /etc/gitlab-runnner/config.toml.

    ~/.ssh/config

    Host gitlab.example.com
      Preferredauthentications publickey
      IdentityFile ~/.ssh/deploy-key
    
    1. Configure the runner to perform the clone via ssh in /etc/config.toml.

    /etc/config.toml

    [[runners]]
    
      # [...]
    
      environment = ["GIT_STRATEGY=none"]
      pre_build_script = '''
        # Fetching using ssh (via pre_build_script in config.toml)
        if [ -d "${CI_PROJECT_DIR}" ]; then rm -rf "${CI_PROJECT_DIR}"; fi
        mkdir -p "${CI_PROJECT_DIR}"
        cd "${CI_PROJECT_DIR}"
        git init
        git remote add origin "ssh://git@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
        git fetch origin "${CI_COMMIT_SHA}"
        git reset --hard FETCH_HEAD
      '''
    
      # [...]
    

    Here's a breakdown of the additions to config.toml:

    • The GIT_STRATEGY=none environment variable disables the runner's internal git cloning mechanism. (See the Git Strategy section of the CI/CD reference)
    • The pre_build_script performs the actual clone using predefined CI/CD environment variables. In my case, this is a bash script to perform something similar to what a GIT_STRATEGY=fetch might do.
    • If pre_build_script is multi-line, the output of the runner will only show the first line. Having a comment as the first line helps add clarity to the runner output.
    • pre_clone_script is not used here. It's disabled since the environment has GIT_STRATEGY=none set.

提交回复
热议问题