user10688813
user10688813

Reputation: 71

Make a gitlab-ci runner running on docker use shell executor on host

we are have a gitlab-ci runner which is hosted on a docker container running under server A. Now we want to make the gitlab-ci runner configured as a container to execute the commands on the host machine.

We tried registering the runner as "shell" executor using below command but still it is trying to access the shell only inside the gitlab-ci runner container rather than the host server A shell.

sudo gitlab-runner register
--non-interactive
--url "https://gitlab.com/"
--registration-token "xXXXXXXXXXXXXXXx"
--executor "shell"
--description "gitlab-runner"
--run-untagged
--locked "false"

Thanks in advance!

Upvotes: 1

Views: 2403

Answers (1)

Ashley Sanders
Ashley Sanders

Reputation: 43

A simple solution is to have the container SSH into the host machine from within the runner and run the command once the runner has access to the host machine, i.e

before_script:        
  - set -e
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null  
  - mkdir -p ~/.ssh
  - touch ~/.ssh/config
  - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
     
example-stage:
  stage: example  
  script: 
    - ssh $SERVER "your shell command here"

Where $SSH_PRIVATE_KEY and $SERVER should be environment variables for your SSH key and user@ipaddress

Note, this assumes following best practices of using an SSH key instead if password, and also assumes a Debian based host, your before_script will differ if it isn't Debian i.e replacing apt-get with apk add for Alpine based distros

Upvotes: 3

Related Questions