Github Actions E04运行环境-runner

前言

一个可以直接运行的CICD环境,用起来无疑节省了搭建环境的过程。但对于一个我们陌生的运行环境,最好还是提前了解一些基础的东西,比如:机器的操作系统及版本、内存、CPU、有多少磁盘可用、当前的用户是什么、权限有没有限制,以及workspace在什么目录,不然一旦遇到问题,就无从下手,或者找错方向。

##### CICD环境的灵魂3问:

  • 我是谁 -> 操作系统,具体版本,当前用户及权限
  • 我在哪里 -> 工作目录在哪里
  • 我能做什么 —> 有什么工具已经安装了,还是要自己安装

1. GitHub-hosted runner 介绍

一个 GitHub-hosted runner 是由GitHub托管并安装了GitHub Actions运行程序应用程序的虚拟机。 GitHub为运行Linux,Windows和macOS操作系统的用户提供帮助。

个人总结:GitHub Actions runner 是一个 application

官网也提及 “The GitHub Actions runner application is open source. You can contribute and file issues in the runner repository.”

2. 硬件资源

GitHub hosts Linux and Windows runners on Standard_DS2_v2 virtual machines in Microsoft Azure.

GitHub uses MacStadium to host the macOS runners.

每个虚拟机有相同的硬件资源可使用。拿一台同配置的个人电脑,或者云上的服务器相比,GA 的服务器运行起来会快一些。

  • 2-core CPU
  • 7 GB of RAM memory
  • 14 GB of SSD disk space
Virtual environment YAML workflow label
Windows Server 2019 windows-latest or windows-2019
Ubuntu 18.04 ubuntu-latest or ubuntu-18.04
Ubuntu 16.04 ubuntu-16.04
macOS Catalina 10.15 macos-latest or macos-10.15

Inbound ICMP packets are blocked for all Azure virtual machines, so ping or traceroute commands might not work. For more information about the Standard_DS2_v2 machine resources, see “Dv2 and DSv2-series” in the Microsoft Azure documentation.

3. 运行用户的权限

可以写一个demo查看一下当前用户,例子见下面 👇

Linux和macOS虚拟机均使用无密码sudo运行。 当您需要执行命令或安装需要比当前用户更多特权的工具时,可以使用sudo而不需要提供密码。

Windows虚拟机被配置为以禁用了用户帐户控制(UAC)的管理员身份运行。

4. 文件系统(Filesystems on GitHub-hosted runners)

GitHub在虚拟机上的特定目录中执行操作和shell命令, 虚拟机上的文件路径不是静态的。

Docker container filesystem 后面讲到时再说

关于查看当前用户和工作目录的demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
name: CI

on: [push]

jobs:
linux:
runs-on: ubuntu-latest
steps:
#我是谁
- run: whoami
#我在那儿
- run: |
pwd
echo $HOME $GITHUB_WORKSPACE $GITHUB_EVENT_PATH

在我的repo中运行的输出为:

1
2
3
runner
/home/runner/work/api-test-demo/api-test-demo
/home/runner /home/runner/work/api-test-demo/api-test-demo /home/runner/work/_temp/_github_workflow/event.json

GitHub为每个工作流创建的环境变量有很多,后面会单独讲到,也可以提前看一下有哪些环境变量。这样类似的默认的环境变量就是类似于我们在jenkins上用的 BUILD_NUMBER,JOB_NAME,当然也可以自定义环境变量。

5. 软件安装

不同的操作系统上都预先安装好了常用软件,详情可见下方的链接 https://help.github.com/en/actions/reference/software-installed-on-github-hosted-runners

Ubuntu 18.04 LTS
Ubuntu 16.04 LTS
Windows Server 2019
Windows Server 2016
MacOS 10.15

如果不想看看长长的readme,可以在运行job中先检查以下软件是否安装及具体版本;如果最后发现没有安装,就添加安装的命令即可。比如

1
2
3
4
5
6
7
8
9
10
name: CI

on: [push]

jobs:
linux:
runs-on: ubuntu-latest
steps:
- run: sudo apt-get install ninja-build
- run: ninja --version

思考个问题: 自定义 runner,以及用私有的服务器运行 workflow,怎么实现?

方法肯定是有的,相信github官方可考虑了runner私有化的场景(毕竟gitlab CI runner在那里摆着),后续有时间再研究


More Ref:

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~