GitHubActions是一项为快速构建持续集成和交付(CI/CD)工作流程而提供的服务。
(本文字数:6029,阅读时长大概:6分钟)
GitHubActions是一项为快速构建持续集成和交付(CI/CD)工作流程而提供的服务。这种工作流程在被称为“运行器(runner)”的主机上运行。GitHub提供的托管运行器的操作系统的选择是有限的(WindowsServer、Ubuntu、MacOS)。
另一个选择是使用自托管的运行器,这让库房管理员对运行器有更多控制。自托管的运行程序是专门为某个储存库或组织服务的。下边的文章介绍了使用FedoraCoreOS配置自托管运行程序的步骤。
入门
FedoraCoreOS是一个精简的操作系统fedora linux系统下载,借以以便大规模的布署和维护。该操作系统会手动更新,并默认提供运行容器所需的工具。因为这种诱因,FedoraCoreOS是运行CI/CD工作流程的一个绝佳选择。
配置和配备FedoraCoreOS机器的第一步是生成一个Ignition文件。Butane容许你使用更友好的格式(YAML)生成Ignition文件。
配置一个FedoraCoreOS运行器
要在FedoraCoreOS上执行GitHubActions,托管主机须要用于注册和运行该运行器的二补码文件和脚本。从Actions运行器项目下载二补码文件和脚本,并布署在/usr/local/sbin/actions-runner下。
version: "1.3.0"
variant: fcos
storage:
directories:
- path: /usr/local/sbin/actions-runner
mode: 0755
user:
name: core
group:
name: core
files:
- path: /usr/local/sbin/actions-runner/actions-runner-linux.tar.gz
overwrite: true
contents:
source: https://github.com/actions/runner/releases/download/v2.278.0/actions-runner-linux-x64-2.278.0.tar.gz
mode: 0755
user:
name: core
group:
name: core
注册和删掉令牌
为一个项目配置运行器须要一个“令牌(token)”。这可以避免在没有正确权限的情况下从项目中注册或删掉自托管的运行器。GitHub提供的令牌有一个小时的过期时间。假如运行器在这个时间以后重新启动,它将须要一个新的注册令牌。
该令牌可能出问题,非常是在FedoraCoreOS手动更新时。更新过程希望托管主机在收到新数据后起码每隔几周重启一次。
辛运的是,可以使用GitHubRESTAPI来获取这种令牌linux系统入门学习,并在托管主机每次重启时手动配置运行器。下边的manage-runner.sh脚本使用API来获取令牌,删掉任何早已配置好的运行器,并用新的令牌注册运行器。
#!/bin/bash
# Handles the Github Action runner configuration.
# Remove and Registration token expires after 1 hour, if we want our runner
# to work after a reboot (auto update) we need to refresh the tokens.
# First remove the runner with a fresh remove token
REMOVE_TOKEN=$(curl -u ${GITHUB_USER}:${GITHUB_TOKEN} -X POST -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${GITHUB_USER}/${GITHUB_REPO}/actions/runners/remove-token | jq -r '.token')
/usr/local/sbin/actions-runner/config.sh remove --token ${REMOVE_TOKEN}
# Then register the runner with a fresh registration token
REGISTRATION_TOKEN=$(curl -u ${GITHUB_USER}:${GITHUB_TOKEN} -X POST -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${GITHUB_USER}/${GITHUB_REPO}/actions/runners/registration-token | jq -r '.token')
/usr/local/sbin/actions-runner/config.sh --url https://github.com/cverna/fcos-actions-runner --token ${REGISTRATION_TOKEN} --labels fcos --unattended
里面的脚本使用了一些环境变量,包含GitHub用户名和用于验证RESTAPI恳求的个人访问令牌(PersonalAccessToken)。个人访问令牌须要储存库权限,便于成功检索运行器的注册和移除令牌。该令牌是安全敏感信息,所以最好将其储存在一个具有更严格权限的不同文件中。在这个反例中,这个文件是actions-runner。
GITHUB_USER=
GITHUB_REPO=
GITHUB_TOKEN=
以下是创建这两个文件manage-runner.sh和actions-runner的Butane片断。
- path: /usr/local/sbin/actions-runner/manage-runner.sh
contents:
local: manage-runner.sh
mode: 0755
user:
name: core
group:
name: core
- path: /etc/actions-runner
contents:
local: actions-runner
mode: 0700
user:
name: core
group:
name: core
在FedoraCoreOS上运行Actions
最后,创建用于配置和启动运行器的systemd服务。在Butane配置文件中定义这种服务。
systemd:
units:
- name: github-runner-configure.service
enabled: true
contents: |
[Unit]
Description=Configure the github action runner for a repository
After=network-online.target boot-complete.target
Requires=boot-complete.target
[Service]
EnvironmentFile=/etc/actions-runner
Type=oneshot
RemainAfterExit=yes
User=core
WorkingDirectory=/usr/local/sbin/actions-runner
ExecStartPre=tar xvf actions-runner-linux.tar.gz --no-same-owner
ExecStart=/usr/local/sbin/actions-runner/manage-runner.sh
[Install]
WantedBy=multi-user.target
- name: github-runner.service
enabled: true
contents: |
[Unit]
Description=Run the github action runner
After=github-runner-configure.service
[Service]
WorkingDirectory=/usr/local/sbin/actions-runner
User=core
ExecStart=/usr/local/sbin/actions-runner/run.sh
[Install]
WantedBy=multi-user.target
这将创建两个服务:github-runner-configure.service(在主机启动完成后运行一次)和github-runner.service(运行Actions运行器二补码文件并等待新的CI/CD作业)。
如今Butane配置已然完成,从中生成一个Ignition文件并配备一个FedoraCoreOSActions运行器。
$ podman run -i --rm -v $PWD:/code:z --workdir /code quay.io/coreos/butane:release --pretty --strict --files-dir /code config.yaml -o config.ignition
一旦Ignition文件生成fedora linux系统下载,它就可以拿来在支持FedoraCoreOS的平台上配备一个运行器。
配置一个Action来使用一个自托管的运行器
下边的测试Action工作流程将测试FCOS的自托管的运行器。在你的git储存库中创建以下文件.github/workflows/main.yml。
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: fcos
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Runs a single command using the runners shell
- name: Run a one-line script
run: podman run --rm fedora-minimal:34 echo Hello World !
请注意linux命令行和shell脚本编程宝典,runs-on的配置被设置为使用标签为fcos的运行器。
本文介绍的代码可以在这儿中找到。
via:/run-github-actions-on-fedora-coreos/
作者:ClémentVerna选题:lujun9972译者:wxy校对:wxy
本文由LCTT原创编译,Linux中国荣誉推出