Skip to content

Commit

Permalink
Merge pull request #276 from riemers/remove-not-longer-configured-run…
Browse files Browse the repository at this point in the history
…ners

Remove not longer configured runners
  • Loading branch information
riemers authored Aug 2, 2023
2 parents 6a4fa0d + d4bda2f commit d297222
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ On Linux, use `gitlab_runner_package_version` instead.
- `gitlab_runner_runners` - A list of gitlab runners to register & configure. Defaults to a single shell executor.
- `gitlab_runner_skip_package_repo_install`- Skip the APT or YUM repository installation (by default, false). You should provide a repository containing the needed packages before running this role.
- `gitlab_runner_config_update_mode`- Set to `by_config_toml` (default) if this role should apply config changes by updating the `config.toml` itself or set it to `by_registering` if config changes should be applied by unregistering and regeistering the runner in case the config has changed.
- `gitlab_unregister_runner_executors_which_are_not_longer_configured` - Set to `true` if executors should be unregistered from a runner in case it is are not longer configured in ansible. Default: `false`

See the [`defaults/main.yml`](https://github.com/riemers/ansible-gitlab-runner/blob/master/defaults/main.yml) file listing all possible options which you can be passed to a runner registration command.

Expand Down
1 change: 1 addition & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ gitlab_runner_session_server_session_timeout: 1800
# gitlab_runner_skip_package_repo_install: true

gitlab_runner_config_update_mode: by_config_toml
gitlab_unregister_runner_executors_which_are_not_longer_configured: false

# The credentials for the Windows user used to run the gitlab-runner service.
# Those credentials will be passed to `gitlab-runner.exe install`.
Expand Down
6 changes: 6 additions & 0 deletions tasks/main-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
changed_when: '"Updated " in verified_runners.container.Output'
check_mode: no

- import_tasks: list-configured-runners-container.yml

- name: (Container) Register GitLab Runner
include_tasks: register-runner-container.yml
vars:
Expand All @@ -30,6 +32,10 @@
index_var: gitlab_runner_index
loop_var: gitlab_runner

- name: Unregister runners which are not longer configured
include_tasks: unregister-runner-if-not-longer-configured.yml
when: gitlab_unregister_runner_executors_which_are_not_longer_configured

- name: (Container) Set global options
import_tasks: global-setup.yml

Expand Down
14 changes: 10 additions & 4 deletions tasks/main-unix.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
- name: Install GitLab Runner (Debian)
import_tasks: install-debian.yml
include_tasks: install-debian.yml
when: ansible_os_family == 'Debian'

- name: Install GitLab Runner (RedHat)
import_tasks: install-redhat.yml
include_tasks: install-redhat.yml
when: ansible_os_family == 'RedHat'

- name: Install GitLab Runner (macOS)
import_tasks: install-macos.yml
include_tasks: install-macos.yml
when: ansible_os_family == 'Darwin'

- name: Install GitLab Runner (Arch)
import_tasks: install-arch.yml
include_tasks: install-arch.yml
when: ansible_os_family == 'Archlinux'

- name: (Unix) Delete runners which were removed in GitLab
Expand All @@ -21,6 +21,8 @@
check_mode: no
become: "{{ gitlab_runner_system_mode }}"

- import_tasks: list-configured-runners-unix.yml

- name: (Unix) Register GitLab Runner
include_tasks: register-runner.yml
vars:
Expand All @@ -31,6 +33,10 @@
index_var: gitlab_runner_index
loop_var: gitlab_runner

- name: Unregister runners which are not longer configured
include_tasks: unregister-runner-if-not-longer-configured.yml
when: gitlab_unregister_runner_executors_which_are_not_longer_configured

- name: Set global options (macOS/Debian/RedHat)
import_tasks: global-setup.yml

Expand Down
6 changes: 6 additions & 0 deletions tasks/main-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
changed_when: '"Updated " in verified_runners.stderr'
check_mode: no

- import_tasks: list-configured-runners-windows.yml

- name: (Windows) Register GitLab Runner
include_tasks: register-runner-windows.yml
vars:
Expand All @@ -19,6 +21,10 @@
index_var: gitlab_runner_index
loop_var: gitlab_runner

- name: Unregister runners which are not longer configured
include_tasks: unregister-runner-if-not-longer-configured.yml
when: gitlab_unregister_runner_executors_which_are_not_longer_configured

- name: (Windows) Set global options
import_tasks: global-setup-windows.yml

Expand Down
6 changes: 6 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
- 'vars'

- name: Install Gitlab Runner (Container)
vars:
gitlab_install_target_platform: container
include_tasks: main-container.yml
when: gitlab_runner_container_install

- name: Install GitLab Runner (Unix)
vars:
gitlab_install_target_platform: unix
include_tasks: main-unix.yml
when: ansible_os_family != 'Windows' and not gitlab_runner_container_install

- name: Install GitLab Runner (Windows)
vars:
gitlab_install_target_platform: windows
include_tasks: main-windows.yml
when: ansible_os_family == 'Windows' and not gitlab_runner_container_install
20 changes: 2 additions & 18 deletions tasks/register-runner-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,8 @@
dest: '{{ gitlab_runner_config_file_location }}/last-runner-config-{{ actual_gitlab_runner_name }}'
register: runner_config_state

- import_tasks: list-configured-runners-container.yml

- name: Unregister runner when config has changed
docker_container:
name: "{{ gitlab_runner_container_name }}-list"
image: "{{ gitlab_runner_container_image }}:{{ gitlab_runner_container_tag }}"
command: unregister --name {{ actual_gitlab_runner_name }}'
mounts:
- type: bind
source: "{{ gitlab_runner_container_mount_path }}"
target: /etc/gitlab-runner
cleanup: yes
interactive: yes
tty: yes
detach: no
changed_when: False
check_mode: no
when:
- import_tasks: unregister-runner.yml
when:
- actual_gitlab_runner_name in registered_gitlab_runner_names
- runner_config_state.changed
when: gitlab_runner_config_update_mode == 'by_registering'
Expand Down
7 changes: 1 addition & 6 deletions tasks/register-runner-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@
dest: '{{ gitlab_runner_config_file_location }}/last-runner-config-{{ actual_gitlab_runner_name }}'
register: runner_config_state

- import_tasks: list-configured-runners-windows.yml

- name: (Windows) Unregister runner when config has changed
win_command: '{{ gitlab_runner_executable }} unregister --name {{ actual_gitlab_runner_name }}'
args:
chdir: "{{ gitlab_runner_config_file_location }}"
- import_tasks: unregister-runner.yml
when:
- actual_gitlab_runner_name in registered_gitlab_runner_names
- runner_config_state.changed
Expand Down
7 changes: 2 additions & 5 deletions tasks/register-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,8 @@
dest: '{{ gitlab_runner_config_file_location }}/last-runner-config-{{ actual_gitlab_runner_name }}'
register: runner_config_state

- import_tasks: list-configured-runners-unix.yml

- name: Unregister runner when config has changed
ansible.builtin.command: '{{ gitlab_runner_executable }} unregister --name {{ actual_gitlab_runner_name }}'
when:
- import_tasks: unregister-runner.yml
when:
- actual_gitlab_runner_name in registered_gitlab_runner_names
- runner_config_state.changed
when: gitlab_runner_config_update_mode == 'by_registering'
Expand Down
18 changes: 18 additions & 0 deletions tasks/unregister-runner-if-not-longer-configured.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

- name: Get the names of all configured runner executors
set_fact:
local_gitlab_executor_names: "{{ gitlab_runner_runners | map(attribute='name') | list }}"

- name: Find all executors which are not longer configured in ansible
set_fact:
available_runners_not_longer_configured: "{{ registered_gitlab_runner_names | difference(local_gitlab_executor_names) }}"

- name: These runners are going to be unregistered
debug: var=available_runners_not_longer_configured
when: available_runners_not_longer_configured | length > 0

- include_tasks: unregister-runner.yml
loop: "{{ available_runners_not_longer_configured }}"
loop_control:
loop_var: actual_gitlab_runner_name
when: available_runners_not_longer_configured | length > 0
34 changes: 34 additions & 0 deletions tasks/unregister-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@



- name: (Container) Unregister runner
docker_container:
name: "{{ gitlab_runner_container_name }}-list"
image: "{{ gitlab_runner_container_image }}:{{ gitlab_runner_container_tag }}"
command: unregister --name {{ actual_gitlab_runner_name }}'
mounts:
- type: bind
source: "{{ gitlab_runner_container_mount_path }}"
target: /etc/gitlab-runner
cleanup: yes
interactive: yes
tty: yes
detach: no
changed_when: False
check_mode: no
when:
- gitlab_install_target_platform == 'container'


- name: (Windows) Unregister runner
win_command: '{{ gitlab_runner_executable }} unregister --name {{ actual_gitlab_runner_name }}'
args:
chdir: "{{ gitlab_runner_config_file_location }}"
when:
- gitlab_install_target_platform == 'windows'


- name: Unregister runner
ansible.builtin.command: '{{ gitlab_runner_executable }} unregister --name {{ actual_gitlab_runner_name }}'
when:
- gitlab_install_target_platform == 'unix'

0 comments on commit d297222

Please sign in to comment.