Github Actions的简单使用
Xhofe Lv3

Github Actions

CI/CD

要使用Github Actions,首先要了解CI/CD的概念,在软件工程中,CI / CD或CICD通常是指持续集成以及持续交付或持续部署的组合实践(from wiki)。具体而言,CI/CD 可让持续自动化和持续监控贯穿于应用的整个生命周期(从集成和测试阶段,到交付和部署)。这些关联的事务通常被统称为“CI/CD 管道”,由开发和运维团队以敏捷方式协同支持。

GitHub Actions

GitHub Actions 就是 GitHub 的持续集成服务,于2018年10月推出,类似于Travis-ci可以帮助我们做持续集成。

基本概念

先来看一个基础的actions文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
name: CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.

GitHub Actions 有一些自己的术语。

(1)workflow (工作流程):持续集成一次运行的过程,就是一个 workflow。

(2)job (任务):一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务。

(3)step(步骤):每个 job 由多个 step 构成,一步步完成。

(4)action (动作):每个 step 可以依次执行一个或多个命令(action)。

文件详解

name

首先就是name,name就是这个actions的名字;

on

on:是指什么时候会触发这个action,常用的是push

也可指定分支或路径:

1
2
3
4
on:
push:
branches: [master]
paths: [src/*]

或者指定时间除出发(每15分钟执行一次)

1
2
3
4
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '*/15 * * * *'

jobs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
jobs:
job_name:
runs-on: 运行actions的环境,如 ubuntu-latest
container: #在容器中运行
image: node:10.16-jessie
env:
NODE_ENV: development
ports:
- 80
volumes:
- my_docker_volume:/volume_mount
options: --cpus 1
services: #用于为工作流程中的作业托管服务容器
nginx:
image: nginx
# Map port 8080 on the Docker host to port 80 on the nginx container
ports:
- 8080:80
needs: 在这之前需要执行的job,如pre_job
strategy: 多策略
matrix:
node: [6, 8, 10]
timeout-minutes: 360 #让作业运行的最大分钟数。 默认值:360
steps:
- uses: actions/checkout@v2 #下载git库中的文件,uses复用流程
with: #传入环境变量
env: env_val
node-version: ${{ matrix.node }}
- name: step_name
run: run_scrpits
env: #环境变量
name: x
val: h
pre_job:
runs-on: macos-latest
outputs: #作业的输出 map。 作业输出可用于所有依赖此作业的下游作业。
output1: ${{ steps.step1.outputs.test }}
output2: ${{ steps.step2.outputs.test }}

可用的runs-on:

虚拟环境 YAML 工作流程标签
Windows Server 2019 windows-latestwindows-2019
Ubuntu 18.04 ubuntu-latestubuntu-18.04
Ubuntu 16.04 ubuntu-16.04
macOS Catalina 10.15 macos-latestmacos-10.15

添加密钥

repo->settings->secrets:

如{name: MYSECRET, value: my_val}

就可以用 ${{ secrets.MYSECRETS }}取出。

上传build完成的文件

Upload an Individual File

1
2
3
4
5
6
7
8
9
10
11
steps:
- uses: actions/checkout@v2

- run: mkdir -p path/to/artifact

- run: echo hello > path/to/artifact/world.txt

- uses: actions/upload-artifact@v2
with:
name: my-artifact
path: path/to/artifact/world.txt

Upload an Entire Directory

1
2
3
4
- uses: actions/upload-artifact@v2
with:
name: my-artifact
path: path/to/artifact/ # or path/to/artifact

Upload using a Wildcard Pattern:

1
2
3
4
- uses: actions/upload-artifact@v2
with:
name: my-artifact
path: path/**/[abc]rtifac?/*

示例:生成hexo静态文件并部署到gh-pages分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
name: HEXO

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
watch:
types: started

# 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: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: use nodejs
uses: actions/setup-node@v2-beta
with:
node-version: '12'
- name: install hexo
run: npm install hexo-cli -g

- name: install package
run: |
npm install
npm install hexo-deployer-git --save

- name: hexo clean&generate
run: |
hexo clean
hexo generate

- name: config git
run: |
git config user.name "yourname"
git config user.email "[email protected]"
git config --global user.name "yourname"
git config --global user.email "[email protected]"

- name: deploy
run: |
git config user.name "yourname"
git config user.email "[email protected]"
sed -i "s/gh_token/${{ secrets.GH_TOKEN }}/g" ./_config.yml
hexo deploy

参考

Awesome Actions

官方帮助文档

Github Actions入门

  • 本文标题:Github Actions的简单使用
  • 本文作者:Xhofe
  • 创建时间:2020-04-13 17:20:18
  • 本文链接:https://nn.ci/posts/github-actions.html
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论