diff --git a/README.md b/README.md index 3609804..8bd5b86 100644 --- a/README.md +++ b/README.md @@ -1 +1,60 @@ # autoware-github-runner-ansible + +## Installation + +### Install ansible + +```bash +sudo apt update +sudo apt dist-upgrade -y + +# Remove apt installed ansible (In Ubuntu 22.04, ansible the version is old) +sudo apt-get purge ansible + +# Install pipx +sudo apt-get -y install pipx + +# Add pipx to the system PATH +python3 -m pipx ensurepath + +# Install ansible +pipx install --include-deps --force ansible +``` + +### Install ansible collections + +```bash +ansible-galaxy install -f -r requirements.yaml +``` + +### Playbooks + +#### Docker setup + +```bash +ansible-playbook autoware.github_runner.docker_setup --ask-become-pass + +# Restart to apply post-installation changes +sudo reboot +``` + +#### Runner setup + +- 🔴 Modify the PAT according to . +- 🔴 Modify the runner name. +- 🔴 Modify the GitHub account. + +```bash +export PERSONAL_ACCESS_TOKEN= + +ansible-playbook autoware.github_runner.runner_setup --ask-become-pass --extra-vars "runner_name=ovh-runner-01 reinstall_runner=true github_account=xmfcx" +``` + +Set up the clean-up script. + +```bash +ansible-playbook autoware.github_runner.runner_configuration --ask-become-pass + +# Restart and check if everything is working +sudo reboot +``` diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..acbea38 --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,15 @@ +namespace: autoware +name: github_runner +version: 0.1.0 +readme: README.md +authors: + - M. Fatih Cırıt +description: Sets up the GitHub runner for Autoware repositories +license: + - Apache-2.0 +tags: + - autoware + - github-runner +repository: https://github.com/autowarefoundation/autoware-github-runner-ansible +homepage: https://www.autoware.org/ +issues: https://github.com/autowarefoundation/autoware-github-runner-ansible/issues diff --git a/playbooks/docker_setup.yaml b/playbooks/docker_setup.yaml new file mode 100644 index 0000000..7cc2039 --- /dev/null +++ b/playbooks/docker_setup.yaml @@ -0,0 +1,4 @@ +- name: Install docker engine + hosts: localhost + roles: + - autoware.github_runner.docker_engine diff --git a/playbooks/runner_configuration.yaml b/playbooks/runner_configuration.yaml new file mode 100644 index 0000000..58045e6 --- /dev/null +++ b/playbooks/runner_configuration.yaml @@ -0,0 +1,4 @@ +- name: Configure the runner + hosts: localhost + roles: + - autoware.github_runner.runner_configuration diff --git a/playbooks/runner_setup.yaml b/playbooks/runner_setup.yaml new file mode 100644 index 0000000..717ebe5 --- /dev/null +++ b/playbooks/runner_setup.yaml @@ -0,0 +1,11 @@ +- name: Install GitHub Actions Runner + hosts: localhost + runner_user: ubuntu + become: true + vars: + - github_owner: autowarefoundation + - runner_org: true + - runner_group: Default + - runner_labels: [self-hosted, Linux, X64] + roles: + - role: monolithprojects.github_actions_runner diff --git a/requirements.yaml b/requirements.yaml new file mode 100644 index 0000000..7272ecd --- /dev/null +++ b/requirements.yaml @@ -0,0 +1,7 @@ +roles: + - name: monolithprojects.github_actions_runner + version: 1.21.1 + src: https://github.com/MonolithProjects/ansible-github_actions_runner +collections: + - source: ./ + type: dir diff --git a/roles/docker_engine/README.md b/roles/docker_engine/README.md new file mode 100644 index 0000000..a79159c --- /dev/null +++ b/roles/docker_engine/README.md @@ -0,0 +1,63 @@ +# docker_engine + +This role installs [Docker Engine](https://docs.docker.com/engine/) following the [installation guide](https://docs.docker.com/engine/install/ubuntu/) and sets up execution from non-root users following the [manual](https://docs.docker.com/engine/install/linux-postinstall/). + +## Inputs + +None. + +## Manual Installation + +Install Docker Engine: + +```bash +# Taken from: https://docs.docker.com/engine/install/ubuntu/ +# And: https://docs.docker.com/engine/install/linux-postinstall/ + +# Uninstall old versions +sudo apt-get remove docker docker-engine docker.io containerd runc + +# Install using the repository +sudo apt-get update + +sudo apt-get install \ + ca-certificates \ + curl \ + gnupg \ + lsb-release + +# Add Docker’s official GPG key: +sudo mkdir -p /etc/apt/keyrings +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg + +# Use the following command to set up the repository: +echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ + $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + +# Install Docker Engine +sudo apt-get update +sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin + +# Verify that Docker Engine is installed correctly by running the hello-world image. +sudo docker run hello-world +# Note: This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits. +``` + +Perform the post-installation steps: + +```bash +# Post-installation steps for Linux + +# Create the docker group. +sudo groupadd docker + +# Add your user to the docker group. +sudo usermod -aG docker $USER + +# Log out and log back in so that your group membership is re-evaluated. + +# Verify that you can run docker commands without sudo +docker run hello-world +# Note: This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits. +``` diff --git a/roles/docker_engine/defaults/main.yaml b/roles/docker_engine/defaults/main.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/docker_engine/meta/main.yaml b/roles/docker_engine/meta/main.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/docker_engine/tasks/main.yaml b/roles/docker_engine/tasks/main.yaml new file mode 100644 index 0000000..fa180a6 --- /dev/null +++ b/roles/docker_engine/tasks/main.yaml @@ -0,0 +1,91 @@ +- name: Uninstall old versions + become: true + ansible.builtin.apt: + name: + - docker + - docker-engine + - docker.io + - containerd + - runc + - docker.io + - docker-compose + - docker-compose-v2 + - docker-doc + - podman-docker + state: absent + update_cache: true + +- name: Install dependencies for setting up apt sources + become: true + ansible.builtin.apt: + name: + - ca-certificates + - curl + - gnupg + - lsb-release + update_cache: true + +# sudo mkdir -p /etc/apt/keyrings +- name: Create the directory for keyrings + become: true + ansible.builtin.file: + state: directory + path: /etc/apt/keyrings + mode: 0755 + +# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg +- name: Authorize Docker GPG key + become: true + ansible.builtin.apt_key: + url: https://download.docker.com/linux/ubuntu/gpg + keyring: /etc/apt/keyrings/docker.gpg + +- name: Save result of 'dpkg --print-architecture' + ansible.builtin.command: dpkg --print-architecture + register: docker_engine__deb_architecture + changed_when: false + +- name: Save result of 'lsb_release -cs' + ansible.builtin.command: lsb_release -cs + register: docker_engine__lsb_release_cs + changed_when: false + +- name: Save result of 'lsb_release -is' + ansible.builtin.command: lsb_release -is + register: docker_engine__lsb_release_is + changed_when: false + +# echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null +- name: Add Docker apt repository to source list + become: true + ansible.builtin.apt_repository: + repo: deb [arch={{ docker_engine__deb_architecture.stdout }} signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/{{ docker_engine__lsb_release_is.stdout | lower }} {{ docker_engine__lsb_release_cs.stdout }} stable + filename: docker + state: present + update_cache: true + +- name: Install Docker Engine + become: true + ansible.builtin.apt: + name: + - docker-ce + - docker-ce-cli + - containerd.io + - docker-buildx-plugin + - docker-compose-plugin + update_cache: true + +# sudo groupadd docker +- name: Add docker group + become: true + ansible.builtin.group: + name: docker + state: present + +# sudo usermod -aG docker $USER +- name: Add user to docker group + become: true + ansible.builtin.user: + name: "{{ ansible_user_id }}" + groups: docker + append: true diff --git a/roles/runner_configuration/README.md b/roles/runner_configuration/README.md new file mode 100644 index 0000000..98f2293 --- /dev/null +++ b/roles/runner_configuration/README.md @@ -0,0 +1 @@ +# runner_configuration diff --git a/roles/runner_configuration/defaults/main.yaml b/roles/runner_configuration/defaults/main.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/runner_configuration/files/cleanup_script.sh b/roles/runner_configuration/files/cleanup_script.sh new file mode 100644 index 0000000..1409209 --- /dev/null +++ b/roles/runner_configuration/files/cleanup_script.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +keep_last_x=4 +# List all images, sort by creation date, get the image IDs, skip the last x, and remove the rest +docker images --format "{{.CreatedAt}} {{.ID}}" | sort -r | awk '{print $5}' | tail -n +$((keep_last_x + 1)) | xargs -r docker rmi -f diff --git a/roles/runner_configuration/meta/main.yaml b/roles/runner_configuration/meta/main.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/runner_configuration/tasks/main.yaml b/roles/runner_configuration/tasks/main.yaml new file mode 100644 index 0000000..8458d09 --- /dev/null +++ b/roles/runner_configuration/tasks/main.yaml @@ -0,0 +1,20 @@ +- name: Create directory for runner scripts + become: true + ansible.builtin.file: + path: /opt/runner-scripts + state: directory + mode: "0755" + +- name: Install the cleanup script + become: true + ansible.builtin.copy: + src: "{{ role_path }}/files/cleanup_script.sh" + dest: /opt/runner-scripts/cleanup_script.sh + mode: "0755" + +- name: Append cleanup script to actions-runner .env + become: true + ansible.builtin.lineinfile: + path: /opt/actions-runner/.env + line: ACTIONS_RUNNER_HOOK_JOB_STARTED=/opt/runner-scripts/cleanup_script.sh + create: true