新聞中心
GitHub Actions 是一項(xiàng)為快速建立持續(xù)集成和交付(CI/CD)工作流程而提供的服務(wù)。這些工作流程在被稱為“運(yùn)行器runner”的主機(jī)上運(yùn)行。GitHub 提供的 托管運(yùn)行器 的操作系統(tǒng)的選擇是有限的(Windows Server、Ubuntu、MacOS)。

創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),西秀網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:西秀等地區(qū)。西秀做網(wǎng)站價(jià)格咨詢:18982081108
另一個(gè)選擇是使用 自托管 的運(yùn)行器,這讓倉庫管理員對(duì)運(yùn)行器有更多控制。自托管的運(yùn)行程序是專門為某個(gè)存儲(chǔ)庫或組織服務(wù)的。下面的文章介紹了使用 Fedora CoreOS 配置自托管運(yùn)行程序的步驟。
入門
Fedora CoreOS 是一個(gè)精簡的操作系統(tǒng),旨在便于大規(guī)模的部署和維護(hù)。該操作系統(tǒng)會(huì)自動(dòng)更新,并默認(rèn)提供運(yùn)行容器所需的工具。由于這些原因,F(xiàn)edora CoreOS 是運(yùn)行 CI/CD 工作流程的一個(gè)極佳選擇。
配置和配備 Fedora CoreOS 機(jī)器的第一步是生成一個(gè) Ignition 文件。Butane 允許你使用更友好的格式(YAML)生成 Ignition 文件。
配置一個(gè) Fedora CoreOS 運(yùn)行器
要在 Fedora CoreOS 上執(zhí)行 GitHub Actions,托管主機(jī)需要用于注冊和運(yùn)行該運(yùn)行器的二進(jìn)制文件和腳本。從 Actions 運(yùn)行器項(xiàng)目 下載二進(jìn)制文件和腳本,并部署在 /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
注冊和刪除令牌
為一個(gè)項(xiàng)目配置運(yùn)行器需要一個(gè)“令牌token”。這可以防止在沒有正確權(quán)限的情況下從項(xiàng)目中注冊或刪除自托管的運(yùn)行器。GitHub 提供的令牌有一個(gè)小時(shí)的過期時(shí)間。如果運(yùn)行器在這個(gè)時(shí)間之后重新啟動(dòng),它將需要一個(gè)新的注冊令牌。
該令牌可能出問題,特別是在 Fedora CoreOS 自動(dòng)更新時(shí)。更新過程希望托管主機(jī)在收到新數(shù)據(jù)后至少每隔幾周重啟一次。
幸運(yùn)的是,可以使用 GitHub REST API 來獲取這些令牌,并在托管主機(jī)每次重啟時(shí)自動(dòng)配置運(yùn)行器。下面的 manage-runner.sh 腳本使用 API 來獲取令牌,刪除任何已經(jīng)配置好的運(yùn)行器,并用新的令牌注冊運(yùn)行器。
#!/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
上面的腳本使用了一些環(huán)境變量,包含 GitHub 用戶名和用于驗(yàn)證 REST API 請求的 個(gè)人訪問令牌Personal Access Token。個(gè)人訪問令牌需要存儲(chǔ)庫權(quán)限,以便成功檢索運(yùn)行器的注冊和移除令牌。該令牌是安全敏感信息,所以最好將其存儲(chǔ)在一個(gè)具有更嚴(yán)格權(quán)限的不同文件中。在這個(gè)例子中,這個(gè)文件是 actions-runner。
GITHUB_USER=
GITHUB_REPO=
GITHUB_TOKEN=
以下是創(chuàng)建這兩個(gè)文件 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
在 Fedora CoreOS 上運(yùn)行 Actions
最后,創(chuàng)建用于配置和啟動(dòng)運(yùn)行器的 systemd 服務(wù)。在 Butane 配置文件中定義這些服務(wù)。
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
這將創(chuàng)建兩個(gè)服務(wù):github-runner-configure.service(在主機(jī)啟動(dòng)完成后運(yùn)行一次)和 github-runner.service(運(yùn)行 Actions 運(yùn)行器二進(jìn)制文件并等待新的 CI/CD 作業(yè))。
現(xiàn)在 Butane 配置已經(jīng)完成,從中生成一個(gè) Ignition 文件并配備一個(gè) Fedora CoreOS Actions 運(yùn)行器。
$ 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 CoreOS 的平臺(tái)上配備一個(gè)運(yùn)行器。
配置一個(gè) Action 來使用一個(gè)自托管的運(yùn)行器
下面的測試 Action 工作流程將測試 FCOS 的自托管的運(yùn)行器。在你的 git 存儲(chǔ)庫中創(chuàng)建以下文件 .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 !
請注意,runs-on 的配置被設(shè)置為使用標(biāo)簽為 fcos 的運(yùn)行器。
本文介紹的代碼可以在 這里 中找到。
當(dāng)前名稱:在FedoraCoreOS上運(yùn)行GitHubActions
本文網(wǎng)址:http://www.fisionsoft.com.cn/article/djjjgec.html


咨詢
建站咨詢
