Alex. b
Alex. b

Reputation: 689

Gitlab Ci: automatically execute before_script actions

I have the following .gitlab-ci.yml file

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - ssh-add /root/gitlab-runner/.ssh/id_rsa
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  - apt-get update -qq && apt-get install -y -qq apt-utils sqlite3 libsqlite3-dev nodejs tree
  - gem install bundler --no-ri --no-rdoc
  - bundle install --jobs $(nproc) "${FLAGS[@]}"
  - cp /root/gitlab-runner/.database.gitlab-ci.yml config/database.yml
  - RAILS_ENV=test rake parallel:setup

rspec:
  script:
    - rake parallel:spec

The issue is that we have so many projects using the exact same before_script actions and these actions sometimes change, so we have to update this file for every project. Is there a way to automatically configure the runner to execute actions so that the .gitlab-ci.yml in this case becomes:

rspec:
  script:
    - rake parallel:spec

Upvotes: 0

Views: 678

Answers (1)

tmt
tmt

Reputation: 8634

You can save all the before_script commands into a Bash script, store it on the server hosting the runner and then just reference it in all the projects:

before_script:
  - /[path on the host]/script.sh

If you are using Docker, you can either include the file in your own image or use volumes to mount the host directory in the Docker container.

It would be a bit more complicated in case you have multiple runners on different servers.

Upvotes: 1

Related Questions