Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI script needs entry in /etc/hosts

I have a GitLab CI docker runner to execute my automated tests when I push. One of my tests requires a custom entry in /etc/hosts. I can't figure out how to get the entry into that file.

Here's basically what my .gitlab-ci.yml file looks like:

before_script:
  - cat /etc/hosts   # for debugging
  - ...              # install app dependencies
specs:
  script:
    - rspec          # <- a test in here fails without the /etc/hosts entry 

All my tests pass, except for the one that requires that /etc/hosts entry.

Let's say I'm trying to have the hostname myhost.local resolve to the IPv4 address XX.XX.XX.XX...

I tried using extra_hosts on the runner config, but it didn't seem to have any effect (got idea from here):

/etc/gitlab-runner/config.toml:

concurrent = 1
check_interval = 0

[[runners]]
  name = "shell"
  url = "https://mygitlabinstance.com/"
  token = "THETOKEN"
  executor = "shell"
  [runners.cache]

[[runners]]
  name = "docker-ruby-2.5"
  url = "https://mygitlabinstance.com/"
  token = "THETOKEN"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "ruby:2.5"
    privileged = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    extra_hosts = ["myhost.local:XX.XX.XX.XX"]
  [runners.cache]

But the test still failed. The cat /etc/hosts shows that it's unchanged:

# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
#
127.0.1.1 ip-172-31-2-54.ec2.internal ip-172-31-2-54
127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

I figured I could just add the entry myself in a before_script line, but I don't seem to be able to execute anything with root privileges in the container:

before_script:
  - echo 'XX.XX.XX.XX myhost.local' >> /etc/hosts
  ...

But that just fails because the gitlab-runner user doesn't have permissions to write to that file. I tried to use sudo, but gitlab-runner can't do that either (echo 'XX.XX.XX.XX myhost.local' | sudo tee --non-interactive --append /etc/hosts --> sudo: a password is required)

So in summary, how can I get my container to have the host entry I need (or how can I execute a before_script command as root)?

like image 351
Nathan Wallace Avatar asked Jan 29 '18 16:01

Nathan Wallace


People also ask

How do you run a script from file in another project using include in gitlab CI?

If you have access project A and B, you can use multi-project pipelines. You trigger a pipeline in project A from project B. In project A, you clone project B and run your script.

Where does gitlab runner execute?

Gitlab Runner is an application that works with GitLab CI/CD to run the job in a pipeline. It is open-source and written in Go Language. It can also be run inside the Docker container or it can be deployed into a Kubernetes cluster.

Where is gitlab runner config file?

You can find the config. toml file in: /etc/gitlab-runner/ on *nix systems when GitLab Runner is executed as root (this is also the path for service configuration)

What is Before_script in gitlab CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.


1 Answers

The following statement is incorrect:

"But that just fails because the gitlab-runner user doesn't have permissions to write to that file."

The gitlab-runner is not the user executing your before_script, it is the user that runs the container in which your job is executed.

You are using the ruby:2.5 Docker image as far as I can tell and that does not contain any USER reference in its or its parents Dockerfile.

Try adding a whoami command right before your echo 'XX.XX.XX.XX myhost.local' >> /etc/hosts command to verify you are root.

Update

If gitlab-runner is shown as the result of whoamithe docker-executor is not used and instead a shell-executor has picked up the job.

like image 142
Stefan van Gastel Avatar answered Oct 06 '22 19:10

Stefan van Gastel