Github Action你值得了解的~

3,139 阅读3分钟

github action

概念介绍

能力介绍

  • 支持分支 build, test, package, release, or deploy
  • 支持 end-to-end continuous integration (CI) and continuous deployment (CD)
  • 支持在第三方云平台、github平台、以及开发者自己的服务器构建
  • 支持的action有三类: 同repository下action、public action repository 以及github action market
  • 支持事件触发特定构建
  • 支持邮件发送运行状态通知
  • 仅github平台服务器存在限制

术语介绍

  • action : workflow最小执行单元
  • Artifact : workflow运行,产生的中间文件,包括日志、测试结果等
  • Continuous integration (CI):自动构建、测试
  • Continuous deployment (CD): 自动部署
  • Event: 触发workflow
  • GitHub-hosted runner:githut平台的虚拟机
  • Job: workflow的分解,可串行存在依赖;可并行
  • Runner: 运行环境
  • Self-hosted runner: 开发者自己的运新环境
  • Step: job的分解

github平台服务器限制

  • 一个repo,可最多同时开20个workflows;超过20,则排队等待
  • 一个workflow下的每个job,最多运行6小时;超过,直接结束
  • 所有分支下的job,根据github级别不同,限制不同;超过,进入队列等待。
  • 1小时内,最多1000 次 请求,也就是1.5api/1m
  • 乱用action,可能会被github限制账户哦
GitHub planTotal concurrent jobsMaximum concurrent macOS jobs
Free205
Pro405
Team605
Enterprise18015

action yml文件

语法结构

name: Greet Everyone
# This workflow is triggered on pushes to the repository.
on: [push]

jobs:
  build:
    # Job name is Greeting
    name: Greeting
    # This job runs on Linux
    runs-on: ubuntu-latest
    steps:
      # This step uses GitHub's hello-world-javascript-action: https://github.com/actions/hello-world-javascript-action
      - name: Hello world
        uses: actions/hello-world-javascript-action@v1
        with:
          who-to-greet: 'Mona the Octocat'
        id: hello
      # This step prints an output (time) from the previous step's action.
      - name: Echo the greeting's time
        run: echo 'The time was ${{ steps.hello.outputs.time }}.'

如何创建yml文件

* .github/workflows 创建.yml or .yaml 后缀,名称随意
* GITHUB_ 前缀的变量名,为github使用;否则会抛出错误
*  环境变量区分大小写
* action secrets必须放在环境变量或者作为输入,

step secret

steps:
  - name: Hello world
    run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
    env:
      FIRST_NAME: Mona
      middle_name: The
      Last_Name: Octocat
steps:
- name: My first action
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    FIRST_NAME: Mona
    LAST_NAME: Octocat
steps:
- name: Hello world action
  with: # Set the secret as an input
    super_secret: ${{ secrets.SuperSecret }}
  env: # Or as an environment variable
    super_secret: ${{ secrets.SuperSecret }}

yml语法规则

express 语法

使用技巧

step

 steps:
  - uses: actions/setup-node@74bc508 # Reference a specific commit
  - uses: actions/setup-node@v1      # Reference the major version of a release
  - uses: actions/setup-node@v1.2    # Reference a minor version of a release
  - uses: actions/setup-node@master  # Reference a branch

触发时机

# push 
name: descriptive-workflow-name
on: push
# 每小时
on:
schedule:
  - cron:  '0 * * * *'
#特定分支、tag、路径
on:
push:
  branches:
    - master
  tags:
    - v1
  # file paths to consider in the event. Optional; defaults to all.
  paths:
    - 'test/*'

github host运行环境

runs-on: ubuntu-latest 

github 服务器采用如下统一配置:

* 2-core CPU
* 7 GB of RAM memory
* 14 GB of SSD disk space

推荐使用linux,资源消耗存在不同比率。

* linux:1,
* window:2 
* mac:10.

self-host

runs-on: [self-hosted, linux, ARM32]
引用action
  • public repo
{owner}/{repo}@{ref} or {owner}/{repo}/{path}@{ref}. 
  • same repo
{owner}/{repo}@{ref} or ./path/to/dir

|-- hello-world (repository)
|   |__ .github
|       └── workflows
|           └── my-first-workflow.yml
|       └── actions
|           |__ hello-world-action
|               └── action.yml

jobs:
build:
 runs-on: ubuntu-latest
 steps:
   # This step checks out a copy of your repository.
   - uses: actions/checkout@v1
   # This step references the directory that contains the action.
   - uses: ../github/actions/hello-world-action
  • docker container
docker://{image}:{tag}

显示workflow status

![](https://github.com/actions/hello-world/workflows/Greet%20Everyone/badge.svg)

使用命令行

jobs:
my_first_job:
  steps:
    - name: My first step
      uses: docker://gcr.io/cloud-builders/gradle
    - name: Install Dependencies
      run: npm install
      shell: bash

with传参

jobs:
my_first_job:
  steps:
    - name: My first step
      uses: actions/hello_world@master
      with:
        first_name: Mona
        middle_name: The
        last_name: Octocat   

first_name ,会被转化为INPUT_FIRST_NAME使用

GITHUB_TOKEN不满足,可以使用personal accesss_token

action 管理

读权限可以看日志

写权限才可以停止action

step debug

ACTIONS_STEP_DEBUG = true

runner debug

ACTIONS_RUNNER_DEBUG = true

更多推荐

Angular 本地json文件读取的2种方法

原生js开发中存在的点滴坑

20个你值得了解的Angular开源项目

参考文献

trying-github-actions

environment-variables