monolithprojects.github_actions_runner

GitHub Actions Runner

awesome-runners Role version Role downloads Molecule test License

此角色将在Linux和macOS系统上部署/重新部署/卸载并注册/注销本地GitHub Actions Runner(请参见兼容性列表)。
支持企业、组织和代码库的Runner。

角色安装

requirements.yml

roles:
  - name: monolithprojects.github_actions_runner
    version: 1.21.1
    src: https://github.com/MonolithProjects/ansible-github_actions_runner

要求

  • 系统必须能够访问GitHub API。

  • 此角色需要个人访问令牌以访问GitHub。令牌可以设置为PERSONAL_ACCESS_TOKEN环境变量。

注意
令牌必须具有repo作用域(当创建代码库运行器时)、admin:org(当为组织创建运行器时)、 manage_runners:enterprise(当创建企业运行器时)。可以在这里创建GitHub帐户的个人访问令牌。

警告
切勿将个人访问令牌存储在GitHub代码库中。请使用GitHub Secrets或其他保密服务。

  • 必须预先创建运行器用户。
    推荐角色:monolithprojects.user_management

  • CentOS系统需要EPEL仓库。
    推荐角色:robertdebock.epel

支持的CPU架构

  • ARM,ARM64(此角色未涵盖依赖关系安装)
  • AMD64,x86_64

支持的操作系统

  • Red Hat Enterprise Linux 7
  • CentOS 7
  • Rocky Linux 8+
  • Fedora 29+
  • Debian 9+
  • Ubuntu 16.04+
  • MacOS High Sierra +

每周测试:

  • Debian 11
  • Fedora 39
  • Rocky Linux 9
  • Ubuntu 20,22

角色变量

这是来自defaults/main.yml的副本

---
# 运行者用户 - 本地运行服务所在的用户
runner_user: "{{ lookup('env', 'USER') }}"

# 本地运行器将安装在此目录中
runner_dir: /opt/actions-runner

# GitHub Actions Runner的版本
runner_version: "latest"

# 角色完成后运行器服务的状态(启动、停止、缺失)
runner_state: "started"

# 如果在服务器上找到,删除已有的运行器服务并重新安装
reinstall_runner: false

# 不显示可能包含敏感数据的Ansible日志(注册令牌)
hide_sensitive_logs: true

# GitHub地址
github_url: "https://github.com"

# GitHub API
github_api_url: "https://api.github.com"

# 每页列出的运行器数量
github_api_runners_per_page: 100

# 您的GitHub帐户的个人访问令牌
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"

# 这是为组织的运行器吗?
runner_org: false

# 要应用于运行器的标签
runner_labels: []

# 将组织运行器添加到的组
runner_group: ""

# GitHub Actions Runner库(如果要使用自定义的Actions Runner分支,请更改)
runner_download_repository: "actions/runner"

# 传递给`config.sh`的额外参数。  
# 多个参数必须作为一个字符串设置(即"--ephemeral --my_special_fork")
runner_extra_config_args: ""

# 在GitHub中分配给此运行器的名称(系统主机名为默认值)
runner_name: "{{ ansible_facts.hostname }}"

# 如果在单个角色中为多个代码库配置运行器,则设置为false
all_runners_in_same_repo: true

# GitHub Repository用户或组织所有者,用于注册运行器
# github_account: "youruser"

# GitHub代码库所有者名称(如果与github_account不同)
# github_owner: "yourorg"

# GitHub代码库名称
# github_repo: "yourrepo"

# GitHub企业名称
# github_enterprise: "yourenterprise"

# 配置自定义.env文件
# custom_env: |
# http_proxy=YOUR_URL_HERE
# ftp_proxy=YOUR_URL_HERE
# HTTPS_PROXY=YOUR_URL_HERE
# https_proxy=YOUR_URL_HERE
# no_proxy=localhost,127.0.0.1,127.0.0.2
# HTTP_PROXY=

示例剧本

在此示例中,Ansible角色将安装(或更新)GitHub Actions Runner服务(最新可用版本)。运行器将为my_awesome_repo GitHub代码库注册。
运行器服务将启动,并将在与Ansible使用的SSH连接用户(ansible)下运行。

---
- name: 安装 GitHub Actions Runner
  hosts: all
  user: ansible
  become: yes
  vars:
    - github_account: github-access-user
    - github_repo: my_awesome_repo
  roles:
    - role: monolithprojects.github_actions_runner

与上面相同的示例,但运行器将添加到组织并部署在GitHub企业服务器上。

---
- name: 安装 GitHub Actions Runner
  hosts: all
  user: ansible
  become: yes
  vars:
    - github_account: my_awesome_org
    - runner_org: yes
    - runner_on_ghes: yes
  roles:
    - role: monolithprojects.github_actions_runner

如果您拥有GitHub企业云许可证,并且希望从企业管理所有自托管的运行器:

---
- name: 安装 GitHub Actions Runner
  hosts: all
  user: automation
  become: yes
  vars:
    - github_enterprise: my_awesome_enterprise
    - runner_org: no
  roles:
    - role: monolithprojects.github_actions_runner

在此示例中,Ansible角色将部署(或更新)GitHub Actions Runner服务(版本2.165.2)并注册用于GitHub代码库的运行器。
运行器服务将在用户runner-user下运行。运行器将注册两个标签。
运行器服务将被停止并禁用。运行器将使用自定义环境变量(来自自托管运行器应用程序目录中的.env文件)。

---
- name: 停止 GitHub Actions Runner
  hosts: all
  become: yes
  vars:
    - runner_version: "2.165.2"
    - runner_user: runner-user
    - github_account: github-access-user
    - github_repo: my_awesome_repo
    - runner_state: "stopped"
    - runner_labels:
        - production
        - west
    - custom_env: |
        HTTP_PROXY=http://proxy.local:8080
        http_proxy=http://proxy.local:8080
        HTTPS_PROXY=http://proxy.local:8080
        https_proxy=http://proxy.local:8080
        no_proxy=localhost,127.0.0.1,127.0.0.2

  roles:
    - role: monolithprojects.github_actions_runner

在此示例中,Ansible角色将卸载运行器服务并从GitHub代码库中注销。

---
- name: 卸载 GitHub Actions Runner
  hosts: all
  become: yes
  vars:
    - github_account: github-access-user
    - github_repo: my_awesome_repo
    - runner_state: "absent"
  roles:
    - role: monolithprojects.github_actions_runner

使用Molecule进行测试

Molecule是Ansible的测试框架。本节针对代码贡献者。

先决条件

  • Python
  • Docker
  • Ansible
  • Molecule

安装

  1. 如果尚未安装Python、Docker和Ansible,请安装它们。
  2. 使用pip安装Molecule及其Docker驱动程序:
pip install molecule[docker]

好的,这里有一个基本示例,说明您如何构建README以解释如何使用Molecule测试monolithprojects.github_actions_runner Ansible角色:

# monolithprojects.github_actions_runner

这是一个用于设置GitHub Actions运行器的Ansible角色。

## 使用Molecule进行测试

[Molecule](https://molecule.readthedocs.io/)是Ansible的测试框架,我们用它来测试`monolithprojects.github_actions_runner`角色。

### 先决条件

- Python
- Docker
- Ansible
- Molecule

### 安装

1. 如果尚未安装Python、Docker和Ansible,请安装它们。
2. 使用pip安装Molecule及其Docker驱动程序:

```bash
pip install molecule[docker]

运行测试

  1. 导航到角色目录:
cd path/to/monolithprojects.github_actions_runner
  1. 设置环境变量:
export PERSONAL_ACCESS_TOKEN=your_github_pat # 您的GitHub个人访问令牌
export GITHUB_ACCOUNT=your_account # 您的GitHub帐户
export GITHUB_REPO=your_repository # 您希望设置运行器的GitHub代码库
  1. 运行Molecule:
molecule test

这将运行Molecule测试,创建一个Docker容器,在其中运行角色,运行任何相关的default测试(请参阅molecule/default目录),然后销毁容器。

有关使用Molecule的更多信息,请参见Molecule文档

许可证

MIT

作者信息

由Michal Muransky于2020年创建

关于项目

Deploy Github Actions private runner

安装
ansible-galaxy install monolithprojects.github_actions_runner
许可证
mit
下载
304.1k
拥有者
Full-time Cloud Engineer, part-time adventurer