-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathgitlab-runner.yml
78 lines (65 loc) · 3.99 KB
/
gitlab-runner.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
# Install Gitlab Runner
# see https://docs.gitlab.com/runner/install/linux-repository.html#installing-the-runner
- name: Add the GitLab Runner package repository
shell: "curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash"
- name: Install GitLab Runner package
apt:
name: gitlab-runner
state: latest
# To register the Gitlab Runner, we need to obtain the Registration Token from our Gitlab instance
# Because this will change every time we start up Gitlab (and/or Vagrant Box/Ansible setup, see https://gitlab.com/gitlab-org/gitlab-ce/issues/3703)
# we need to access it somehow. Sadly there´s no API atm (see https://gitlab.com/gitlab-org/gitlab-ce/issues/24030,
# https://gitlab.com/gitlab-org/gitlab-runner/issues/1727), so we have to dive directly into the Gitlab database :(
- name: Extract Runner Registration Token directly from gitlab-rails console
become: true
shell: |
echo "Gitlab::CurrentSettings.current_application_settings.runners_registration_token" |
gitlab-rails console --environment=production
register: gitlab_runner_registration_token_result
- name: Parse token from response
set_fact:
gitlab_runner_registration_token: "{{ gitlab_runner_registration_token_result.stdout | regex_search(regexp, '\\1') | list | first }}"
vars:
regexp: 'runners_registration_token\n\"([^\"]+)'
- name: And the Token is...
debug:
msg: "{{gitlab_runner_registration_token}}"
# Register Gitlab Runner
- name: Unregister all previously used Gitlab Runners
shell: "gitlab-runner unregister --all-runners"
- name: Add gitlab-runner user to docker group
user:
name: gitlab-runner
groups: docker
append: yes
become: true
# see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-shell-executor
# and this for non-interactive mode:
# https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/commands/README.md#non-interactive-registration
- name: Register Gitlab-Runners using shell executor
shell: "gitlab-runner register --non-interactive --url '{{gitlab_url}}' --registration-token '{{gitlab_runner_registration_token}}' --description 'shell-runner-{{ item }}' --executor shell --tag-list shell"
loop: "{{ range(1,gitlab_runner_count + 1)|list }}"
# see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-workflow-with-docker-executor
- name: Register Gitlab-Runners using docker executor for Docker-in-Docker
shell: "gitlab-runner register --non-interactive --url '{{gitlab_url}}' --registration-token '{{gitlab_runner_registration_token}}' --description 'docker-in-docker-runner-{{ item }}' --executor docker --docker-image 'docker:19.03.1' --docker-privileged --docker-volumes '/certs/client' --tag-list dind"
loop: "{{ range(1,gitlab_runner_count + 1)|list }}"
# see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-socket-binding
- name: Register Gitlab-Runners using docker executor for Docker socket binding
shell: "gitlab-runner register --non-interactive --url '{{gitlab_url}}' --registration-token '{{gitlab_runner_registration_token}}' --description 'docker-socket-runner-{{ item }}' --executor docker --docker-image 'docker:stable' --docker-volumes /var/run/docker.sock:/var/run/docker.sock --tag-list socket"
loop: "{{ range(1,gitlab_runner_count + 1)|list }}"
# number of concurrent runners needs to be set directly in config-file in order to actually run jobs in parallel
# see: https://gitlab.com/gitlab-org/gitlab-runner/issues/1539
- name: set concurrent number of runners in gitlab-runner config
ini_file:
path: /etc/gitlab-runner/config.toml
section:
option: concurrent
value: "{{ gitlab_runner_count }}"
- name: Retrieve all registered Gitlab Runners
shell: "gitlab-runner list"
register: runner_result
- name: Show all registered Gitlab Runners
debug:
msg:
- "{{runner_result.stderr_lines}}"