Github Action入門

github action

概念介紹

能力介紹

  • 支持分支 build, test, package, release, or deploy
  • 支持 end-to-end continuous integration (CI) and continuous deployment (CD)
  • 支持在第三方雲平臺、github平臺、以及[開發者自己的服務器]構建(https://help.github.com/en/actions/automating-your-workflow-with-github-actions/about-self-hosted-runners)
  • 支持的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 plan
Total concurrent jobs
Maximum concurrent macOS jobs
Free 20 5
Pro
40
5
Team 60 5
Enterprise
180
15


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 }}

jobsjob_idstepsenv)

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/[email protected]    # 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 Everyone/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   

firstname ,會被轉化爲INPUTFIRST_NAME使用

GITHUBTOKEN不滿足,可以使用[personal accessstoken](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token)

action 管理

讀權限可以看日誌

寫權限纔可以停止action

step debug

   ACTIONS_STEP_DEBUG = true

runner debug

  ACTIONS_RUNNER_DEBUG = true

參考文獻

trying-github-actions

environment-variables

歡迎關注我們【前端漫漫】,瞭解最新文章動態聯繫郵箱:[email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章