Gitlab Ci unable to push on a branch from runner

社会主义新天地 提交于 2019-12-06 11:53:39

To push to a repo from within a Gitlab CI runner you need to use a user that has push access to the branch you want to push to. We use the following set-up to accomplish this (we let Gitlab CI tag releases and push them).

  1. Create a new Gitlab user called gitlab-ci
  2. Create a SSH key-pair and add the public key to the gitlab-ci user's SSH keys in Gitlab
  3. Give the gitlab-ci user push access to your repo (developer role)
  4. Add the content of the private-key as a CI/CD secret variable called **SSH_PRIVATE_KEY**

This way the private key will be available in CI jobs, next the first part of my CI job looks like this:

script:
    # Install ssh-agent through openssh-client if not present
    - 'which ssh-agent || ( apt-get update -qy && apt-get install openssh-client -qqy )'
    # Add the private key to this user
    - eval $(ssh-agent -s) && ssh-add <(echo "$SSH_PRIVATE_KEY") && mkdir -p ~/.ssh
    # Docker specific settings
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    # Config git to avoid first usage questions. Set the identity
    - git config --global user.email "noreply@example.com" && git config --global user.name "Gitlab CI"
    # 
    # Do Git stuff, for example:
    #
    - git checkout $CI_COMMIT_REF_NAME
    - git tag my-release-1.0
    - git push -u origin my-release-1.0

Big fat disclaimer: Only use this in Gitlab CI runner setups that are disposed of, you are distributing private SSH keys with potential access to your repo so you must use this carefully.

try with this solution : Gitlab runner failing -urgent

Open and edit /etc/gitlab-runner/config.toml file.

and then add this code

extra_hosts = [“yourhostname:youripddress”]

for example file :

[[runners]]
name = "co1"
url = "http://co1/ci"
token = “1ccc03308b71a1c9a10640326adf1a"
executor = “docker”
[runners.docker]
tls_verify = false
image = “co1:5000/pub/pub"
privileged = false
disable_cache = false
volumes = [”/cache”]
shm_size = 0
extra_hosts = [“co1:192.168.176.137”]
[runners.cache]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!