diff --git a/README.md b/README.md index 956d7da07a89eba090e4de554b6388564cce46aa..a0338919b301b20aab57c7bc74479c399eac866b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ GitLab Runner [](https://git.ethitter.com/ansible/gitlab-runner/commits/master) ============= -This role will install the [official GitLab Runner](https://gitlab.com/gitlab-org/gitlab-runner) -(fork from haroldb) with updates. Needed something simple and working, this did the trick for me. Open for changes though. +Install GitLab Runner using Docker Machine and Digital Ocean for on-demand instance creation. See https://www.digitalocean.com/community/tutorials/how-to-autoscale-gitlab-continuous-deployment-with-gitlab-runner-on-digitalocean. + +This is intended to be installed on the same host as the GitLab instance, spinning up hosts as needed and quickly tearing them down. If your CI activity is frequent, the `gitlab_runner_machine_idle_time` variable should be increased so hosts can be reused. Requirements ------------ @@ -12,47 +13,77 @@ This role requires Ansible 2.0 or higher. Role Variables -------------- -`gitlab_runner_package_name` +###### `gitlab_runner_package_name` + **Since Gitlab 10.x** The package name of `gitlab-ci-multi-runner` has been renamed to `gitlab-runner`. In order to install a version >= 10.x you will need to define this variable `gitlab_runner_package_name: gitlab-runner`. -`gitlab_runner_concurrent` +###### `gitlab_runner_concurrent` + The maximum number of global jobs to run concurrently. Defaults to the number of processor cores. -`gitlab_runner_registration_token` +###### `gitlab_runner_registration_token` + The GitLab registration token. If this is specified, a runner will be registered to a GitLab server. -`gitlab_runner_coordinator_url` +###### `gitlab_runner_coordinator_url` + The GitLab coordinator URL. Defaults to `https://gitlab.com/ci`. -`gitlab_runner_description` -The description of the runner. +###### `gitlab_runner_description_base` +Base description of the runner. Defaults to the hostname. -`gitlab_runner_executor` +###### `gitlab_runner_executor` + The executor used by the runner. Defaults to `shell`. -`gitlab_runner_concurrent_specific` +###### `gitlab_runner_concurrent_specific` + The maximum number of jobs to run concurrently on this specific runner. Defaults to 0, simply means don't limit. -`gitlab_runner_docker_image` +###### `gitlab_runner_docker_image` + The default Docker image to use. Required when executor is `docker`. -`gitlab_runner_tags` -The tags assigned to the runner, +###### `gitlab_runner_tags` + +Default tags assigned to the runner, Defaults to an empty list. -`gitlab_runner_cache_type` -Variables to set s3 as a shared cache server. If set it requires variables listed below: -`gitlab_runner_cache_s3_server_address` -`gitlab_runner_cache_s3_access_key` -`gitlab_runner_cache_s3_access_key` -`gitlab_runner_cache_s3_bucket_name` -`gitlab_runner_cache_s3_insecure` -`gitlab_runner_cache_cache_shared` +###### `gitlab_runner_machine_idle_nodes` + +How many idle machines to maintain. Defaults to `0`, meaning all hosts are created on-demand then culled after `gitlab_runner_machine_idle_time` minutes. + +###### `gitlab_runner_machine_idle_time` + +How long, in minutes, a machine can be idle before it's destroyed. Default is `3`. + +###### `gitlab_runners` + +Dict of runners + +```yml +gitlab_runners: + - { + description: '{{ gitlab_runner_description_base }}', + tags: [], + locked: false, + token: '' + } +``` + +###### `gitlab_runner_cache_type` +Variables to set `s3` as a shared cache server. If set it requires variables listed below: +* `gitlab_runner_cache_s3_server_address` +* `gitlab_runner_cache_s3_access_key` +* `gitlab_runner_cache_s3_secret_key` +* `gitlab_runner_cache_s3_bucket_name` +* `gitlab_runner_cache_s3_insecure` +* `gitlab_runner_cache_cache_shared` See the [config for more options](https://github.com/riemers/ansible-gitlab-runner/blob/master/tasks/register-runner.yml) diff --git a/defaults/main.yml b/defaults/main.yml index ec7929a410614c80b55c58d0e6f0d119f2ec944d..b88f84bdf38abffe0bd41a1d6cf047a05202a456 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -10,7 +10,7 @@ gitlab_runner_coordinator_url: 'https://gitlab.com/ci' # GitLab registration token gitlab_runner_registration_token: '' # Runner description -gitlab_runner_description: '{{ ansible_hostname }}' +gitlab_runner_description_base: '{{ ansible_hostname }}' # Runner executor gitlab_runner_executor: 'shell' # Maximum number of jobs to run concurrently on this specific runner @@ -32,4 +32,18 @@ gitlab_runner_ssh_password: '' # Runner SSH identity file gitlab_runner_ssh_identity_file: '' # Runner Locked -gitlab_runner_locked: 'false' +gitlab_runner_locked: false + +# Individual runners +gitlab_runners: + - { + description: '{{ gitlab_runner_description_base }}' + } + +# Runner machine +gitlab_runner_machine_idle_nodes: 0 +gitlab_runner_machine_idle_time: 3 + +# Docker Machine +docker_machine_version: 'v0.15.0' +docker_machine_binary_dest: /usr/local/bin/docker-machine diff --git a/meta/main.yml b/meta/main.yml index 124a476eda263482acf7b365273d2322f4b438fc..e2f27cebe777fca02da45d0c372e35a36ee13838 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,19 +1,14 @@ --- galaxy_info: - author: Erik-jan Riemers + author: Erick Hitter, Erik-jan Riemers description: GitLab Runner + issue_tracker_url: https://git.ethitter.com/ansible/gitlab-runner/issues license: MIT min_ansible_version: 2.0 platforms: - - name: EL - versions: - - all - - name: Ubuntu - versions: - - all - - name: Debian - version: - - all + - name: Debian + version: + - all galaxy_tags: - gitlab - runner diff --git a/tasks/global-setup.yml b/tasks/global-setup.yml index 403e175eeec09fe2cd02710d5995ff30f9d668c2..0844ff52c829f1d5bae6c5352495072d493fcf13 100644 --- a/tasks/global-setup.yml +++ b/tasks/global-setup.yml @@ -16,4 +16,3 @@ state: present backrefs: yes notify: restart_gitlab_runner - diff --git a/tasks/install-docker-machine.yml b/tasks/install-docker-machine.yml new file mode 100644 index 0000000000000000000000000000000000000000..5613a5f740c28f4a074f7575dd9aa541ab939300 --- /dev/null +++ b/tasks/install-docker-machine.yml @@ -0,0 +1,9 @@ +--- + +- name: Get Docker Machine + get_url: + url: "https://github.com/docker/machine/releases/download/{{ docker_machine_version | default('v0.15.0') }}/docker-machine-{{ ansible_system }}-{{ ansible_architecture }}" + dest: /tmp/docker-machine + +- name: Install Docker Machine + command: 'install /tmp/docker-machine {{ docker_machine_binary_dest }}' diff --git a/tasks/install-redhat.yml b/tasks/install-redhat.yml deleted file mode 100644 index f2be5e4596896ec3444fb25e3e90c6b0150dbdbf..0000000000000000000000000000000000000000 --- a/tasks/install-redhat.yml +++ /dev/null @@ -1,29 +0,0 @@ ---- - -- name: RHEL | Install GitLab GPG key - rpm_key: - state: present - key: https://packages.gitlab.com/gpg.key - when: ansible_os_family == 'RedHat' - tags: - - gitlab - - gitlab-gpg - -- name: Install GitLab Runner dependencies - yum: - name: '{{ item }}' - state: present - with_items: - - pygpgme - - yum-utils - -- name: Add GitLab Runner rpm repo - template: - src: "runner.repo.j2" - dest: "/etc/yum.repos.d/runner_{{ gitlab_runner_package_name }}.repo" - -- name: Install GitLab Runner - yum: - name: "{{ gitlab_runner_package_name }}" - state: latest - update_cache: yes diff --git a/tasks/install-debian.yml b/tasks/install-runner.yml similarity index 100% rename from tasks/install-debian.yml rename to tasks/install-runner.yml diff --git a/tasks/main.yml b/tasks/main.yml index afd14acd65e6e3a1ed54a2fd10c3158e878320bb..7a1f40549bc33e79d71112a4a8ca5eb485266134 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,15 +1,13 @@ --- -- name: Install GitLab Runner (Debian) - import_tasks: install-debian.yml - when: ansible_os_family == 'Debian' +- name: Install GitLab Runner + import_tasks: install-runner.yml -- name: Install GitLab Runner (RedHat) - import_tasks: install-redhat.yml - when: ansible_os_family == 'RedHat' - -- name: Set global options - import_tasks: global-setup.yml +- name: Install Docker Machine + import_tasks: install-docker-machine.yml - name: Register GitLab Runner import_tasks: register-runner.yml when: gitlab_runner_registration_token != '' + +- name: Set global options + import_tasks: global-setup.yml diff --git a/tasks/register-runner.yml b/tasks/register-runner.yml index 04d187f56a8ff87a5967a8303a516f3e76b1efa7..1ebf0087b1fa3f36bd5eedeffce69d74b382a358 100644 --- a/tasks/register-runner.yml +++ b/tasks/register-runner.yml @@ -9,12 +9,12 @@ command: gitlab-runner register > --non-interactive --url '{{ gitlab_runner_coordinator_url }}' - --registration-token '{{ gitlab_runner_registration_token }}' - --description '{{ gitlab_runner_description }}' - --tag-list '{{ gitlab_runner_tags | join(",") }}' + --registration-token '{{ item.token | default( gitlab_runner_registration_token ) }}' + --description '{{ item.description }}' + --tag-list '{{ ( item.tags | default( gitlab_runner_tags ) ) | join(",") }}' --executor '{{ gitlab_runner_executor }}' --limit '{{ gitlab_runner_concurrent_specific }}' - --locked='{{gitlab_runner_locked}}' + --locked='{{ item.locked | default( gitlab_runner_locked ) }}' --docker-image '{{ gitlab_runner_docker_image }}' {% if gitlab_runner_docker_volumes is defined %} --docker-volumes "{{ gitlab_runner_docker_volumes | join('" "') }}" @@ -30,7 +30,18 @@ --cache-s3-access-key '{{ gitlab_runner_cache_s3_access_key }}' --cache-s3-secret-key '{{ gitlab_runner_cache_s3_secret_key }}' --cache-s3-bucket-name '{{ gitlab_runner_cache_s3_bucket_name }}' - --cache-s3-insecure '{{ gitlab_runner_cache_s3_insecure }}' - --cache-cache-shared '{{ gitlab_runner_cache_cache_shared }}' + --cache-s3-insecure='{{ gitlab_runner_cache_s3_insecure }}' + --cache-cache-shared='{{ gitlab_runner_cache_cache_shared }}' {% endif %} - when: configured_runners.stderr.find('\n' + gitlab_runner_description) == -1 + --machine-idle-nodes '{{ gitlab_runner_machine_idle_nodes }}' + --machine-idle-time '{{ gitlab_runner_machine_idle_time }}' + --machine-machine-driver 'digitalocean' + --machine-machine-name 'gitlab-%s' + --machine-machine-options 'digitalocean-image=coreos-stable' + --machine-machine-options 'digitalocean-ssh-user=core' + --machine-machine-options 'digitalocean-access-token={{ gitlab_runner_digital_ocean_token }}' + --machine-machine-options 'digitalocean-region={{ gitlab_runner_digital_ocean_region }}' + --machine-machine-options 'digitalocean-size={{ gitlab_runner_digital_ocean_size }}' + --machine-machine-options 'digitalocean-private-networking' + with_items: '{{ gitlab_runners }}' + when: configured_runners.stderr.find('\n' + item.description) == -1 diff --git a/templates/runner.repo.j2 b/templates/runner.repo.j2 deleted file mode 100644 index 97da48259a9cdd86dfdebd6bf8e90c7bff266e39..0000000000000000000000000000000000000000 --- a/templates/runner.repo.j2 +++ /dev/null @@ -1,19 +0,0 @@ -[runner_{{ gitlab_runner_package_name }}] -name=runner_{{ gitlab_runner_package_name }} -baseurl=https://packages.gitlab.com/runner/{{ gitlab_runner_package_name }}/el/{{ ansible_distribution_major_version }}/$basearch -repo_gpgcheck=1 -gpgcheck=0 -enabled=1 -gpgkey=https://packages.gitlab.com/gpg.key -sslverify=1 -sslcacert=/etc/pki/tls/certs/ca-bundle.crt - -[runner_{{ gitlab_runner_package_name }}-source] -name=runner_{{ gitlab_runner_package_name }}-source -baseurl=https://packages.gitlab.com/runner/{{ gitlab_runner_package_name }}/el/{{ ansible_distribution_major_version }}/SRPMS -repo_gpgcheck=1 -gpgcheck=0 -enabled=1 -gpgkey=https://packages.gitlab.com/gpg.key -sslverify=1 -sslcacert=/etc/pki/tls/certs/ca-bundle.crt