Initial commit

This commit is contained in:
2024-02-25 01:14:24 +03:00
commit 1f9873579c
12 changed files with 174 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
*.log
*.pyc
**/__pycache__/
.env
.secrets

10
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,10 @@
---
stages:
- linter
linter:
stage: linter
image:
name: osshelp/drone-linter
entrypoint: [""]
script: [entrypoint.sh]

11
.yamllint Normal file
View File

@@ -0,0 +1,11 @@
extends: default
rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length: disable
truthy: disable

26
README.md Normal file
View File

@@ -0,0 +1,26 @@
# gitlab-runner
Simple Ansible role for gitlab-runner installation.
## Usage (example)
```yaml
- role: gitlab-runner
gitlab_runner_registration_token: "secret_string"
gitlab_runner_executor: "shell"
```
## Available parameters
### Main
| Param | Default | Description |
| -------- | -------- | -------- |
| `gitlab_runner_registration_token` | `""` | GitLab runner registration token. |
| `gitlab_runner_executor` | `""` | GitLab runner executor. Support: `shell` or `docker` |
| `gitlab_runner_user` | `gitlab-runner` | Running GitLab runner under the user |
| `gitlab_runner_instance_url` | `https://gitlab.com` | GitLab instance URL |
| `gitlab_runner_working_directory` | `/home/gitlab-runner` | GitLab Runner working directory |
| `gitlab_runner_runner_description` | `The runner was registered using ansible.` | GitLab runner description |
| `gitlab_runner_docker_default_image` | `alpine` | GitLab runner default image |
| `gitlab_runner_tags` | `""` | GitLab runner tags |

10
defaults/main.yml Normal file
View File

@@ -0,0 +1,10 @@
---
gitlab_runner_architecture: "amd64"
gitlab_runner_binary_path: "/usr/local/bin/gitlab-runner"
gitlab_runner_download_url: "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-{{ gitlab_runner_architecture }}"
gitlab_runner_instance_url: "https://gitlab.com"
gitlab_runner_user: "gitlab-runner"
gitlab_runner_working_directory: "/home/gitlab-runner"
gitlab_runner_config_path: "/etc/gitlab-runner/config.toml"
gitlab_runner_runner_description: "The runner was registered using ansible."
gitlab_runner_docker_default_image: "alpine"

6
handlers/main.yml Normal file
View File

@@ -0,0 +1,6 @@
---
- name: reload gitlab runner unit
systemd: daemon_reload=yes
- name: restart reload gitlab runner service
systemd: name=gitlab-runner state=restarted

12
meta/main.yml Normal file
View File

@@ -0,0 +1,12 @@
---
galaxy_info:
author: OldTyT
description: GitLab runner install
license: GPLv3
min_ansible_version: 2.8
platforms:
- name: Ubuntu
versions:
- jammy
galaxy_tags:
- gitlabrunnerinstall

24
tasks/config.yml Normal file
View File

@@ -0,0 +1,24 @@
---
- name: Create gitlab runner user
user:
name: "{{ gitlab_runner_user }}"
comment: "User for gitlab runner"
state: present
create_home: false
when: gitlab_runner_user != 'root'
- name: create gitlab runner service
template:
src: gitlab-runner.service.j2
dest: /etc/systemd/system/gitlab-runner.service
register: gitlab_runner_service
- name: Create GitLab runner work directory
ansible.builtin.file:
path: "{{ gitlab_runner_working_directory }}"
state: directory
owner: "{{ gitlab_runner_user }}"
group: "{{ gitlab_runner_user }}"
recurse: yes
- meta: flush_handlers

6
tasks/install.yml Normal file
View File

@@ -0,0 +1,6 @@
---
- name: Download GitLab runner
get_url:
url: "{{ gitlab_runner_download_url }}"
dest: "{{ gitlab_runner_binary_path }}"
mode: 0755

16
tasks/main.yml Normal file
View File

@@ -0,0 +1,16 @@
---
- name: gitlab runner installation
include_tasks: install.yml
- name: gitlab runner configuration
include_tasks: config.yml
- name: gitlab runner registration
include_tasks: registration.yml
- name: GitLab Runner started service
systemd_service:
name: "gitlab-runner"
state: started
daemon_reload: true
enabled: true

30
tasks/registration.yml Normal file
View File

@@ -0,0 +1,30 @@
---
- name: Check if GitLab Runner is already registered
ansible.builtin.stat:
path: /etc/gitlab-runner/config.toml
register: gitlab_runner_config
- name: Register GitLab Runner with Docker executor
ansible.builtin.shell:
gitlab-runner register
--non-interactive
--url {{ gitlab_runner_instance_url }}
--registration-token {{ gitlab_runner_registration_token }}
--executor docker
--description "{{ gitlab_runner_runner_description }}"
--docker-image "{{ gitlab_runner_docker_default_image }}"
--tag-list "{{ gitlab_runner_tags | default('') }}"
when: gitlab_runner_config.stat.exists == False and gitlab_runner_executor == 'docker'
become: yes
- name: Register GitLab Runner with Shell executor
ansible.builtin.shell: >
gitlab-runner register
--non-interactive
--url {{ gitlab_runner_instance_url }}
--registration-token {{ gitlab_runner_registration_token }}
--executor shell
--description "{{ gitlab_runner_runner_description }}"
--tag-list "{{ runner_tags | default('') }}"
when: gitlab_runner_config.stat.exists == False and gitlab_runner_executor == 'shell'
become: yes

View File

@@ -0,0 +1,18 @@
{{ ansible_managed | comment }}
[Unit]
Description=GitLab Runner
ConditionFileIsExecutable={{ gitlab_runner_binary_path }}
After=syslog.target network.target
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart={{ gitlab_runner_binary_path }} "run" "--working-directory" "{{ gitlab_runner_working_directory }}" "--config" "{{ gitlab_runner_config_path }}" "--service" "gitlab-runner"{% if not gitlab_runner_user == "root" %} "--user" "{{ gitlab_runner_user }}" {% endif %}
Restart=always
RestartSec=120
#EnvironmentFile=-/etc/sysconfig/gitlab-runner
[Install]
WantedBy=multi-user.target