国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片

Github Actions 備忘清單

本備忘單總結(jié)了 Github Actions 常用的配置說明,以供快速參考。

入門

介紹

GitHub Actions 的倉庫中自動化、自定義和執(zhí)行軟件開發(fā)工作流程,有四個(gè)基本的概念,如下:

:-:-
workflow (工作流程)持續(xù)集成一次運(yùn)行的過程,就是一個(gè) workflow
job (任務(wù))一個(gè) workflow 由一個(gè)或多個(gè) jobs 構(gòu)成,含義是一次持續(xù)集成的運(yùn)行,可以完成多個(gè)任務(wù)
step (步驟)每個(gè) job 由多個(gè) step 構(gòu)成,一步步完成
action (動作)每個(gè) step 可以依次執(zhí)行一個(gè)或多個(gè)命令(action)

  • 采用 YAML 格式定義配置文件
  • 存放在代碼倉庫的 .github/workflows 目錄中
  • 后綴名統(tǒng)一為 .yml,比如 ci.yml
  • 一個(gè)庫可以有多個(gè) workflow 文件
  • 根據(jù)配置事件自動運(yùn)行配置文件

配置文件

name: GitHub Actions Demo
on:
  push:
    branches:
      - main

# 任務(wù)
jobs:
  build:
    runs-on: ubuntu-latest
    # 步驟 根據(jù)步驟執(zhí)行任務(wù)
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - run: npm install
      - run: npm run build

存放到 .github/workflows 目錄中,命名為 ci.yml,當(dāng) push 代碼到倉庫 main 分支中,該配置自動運(yùn)行配置。

指定觸發(fā)

push 事件觸發(fā) workflow

on: push

push 事件或 pull_request 事件都可以觸發(fā) workflow

on: [push, pull_request]

只有在 main 分支 push 事件觸發(fā) workflow

on:
  push:
    branches:
      - main

push 事件觸發(fā) workflow,docs 目錄下的更改 push 事件不觸發(fā) workflow

on:
  push:
    paths-ignore:
      - 'docs/**'

push 事件觸發(fā) workflow,包括 sub-project 目錄或其子目錄中的文件觸發(fā) workflow,除非該文件在 sub-project/docs 目錄中,不觸發(fā) workflow

on:
  push:
    paths:
      - 'sub-project/**'
      - '!sub-project/docs/**'

版本發(fā)布為 published 時(shí)運(yùn)行工作流程。

on:
  release:
    types: [published]

多項(xiàng)任務(wù)

jobs:
  my_first_job:  # 第一個(gè)任務(wù)
    name: My first job

  my_second_job: # 第二個(gè)任務(wù)
    name: My second job

通過 jobs (jobs.<job_id>.name)字段,配置一項(xiàng)或多項(xiàng)需要執(zhí)行的任務(wù)

多項(xiàng)任務(wù)依賴關(guān)系

通過 needs (jobs.<job_id>.needs)字段,指定當(dāng)前任務(wù)的依賴關(guān)系

jobs:
  job1:
  job2:
    needs: job1
  job3:
    needs: [job1, job2]

上面配置中,job1 必須先于 job2 完成,而 job3 等待 job1job2 的完成才能運(yùn)行。因此,這個(gè) workflow 的運(yùn)行順序依次為:job1job2、job3

多項(xiàng)任務(wù)傳遞參數(shù)

jobs:
  job1:
    runs-on: ubuntu-latest
    # 將步驟輸出映射到作業(yè)輸出
    outputs:
      output1: ${{ steps.step1.outputs.test }}
      output2: ${{ steps.step2.outputs.test }}
    steps:
      - id: step1
        run: echo "::set-output name=test::hello"
      - id: step2
        run: echo "::set-output name=test::world"
  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - run: echo ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}}

指定每項(xiàng)任務(wù)的虛擬機(jī)環(huán)境

runs-on: ubuntu-latest

指定運(yùn)行所需要的虛擬機(jī)環(huán)境,?? 它是必填字段

jobs:
  build: # 任務(wù)名稱
    runs-on: ubuntu-latest # 虛擬機(jī)環(huán)境配置

  • Windows Server 2022 (windows-latest)(windows-2022)
  • Ubuntu 20.04 (ubuntu-latest)(ubuntu-20.04)
  • macOS Monterey 12 (macos-12)
  • macOS Big Sur 11 (macos-latest),(macos-11)

另見: 選擇 GitHub 托管的運(yùn)行器

指定每項(xiàng)任務(wù)的步驟

每個(gè)步驟都可以指定以下三個(gè)字段

jobs.<job_id>.steps.name # 步驟名稱
# 該步驟運(yùn)行的命令或者 action
jobs.<job_id>.steps.run
# 該步驟所需的環(huán)境變量
jobs.<job_id>.steps.env

steps 字段指定每個(gè) Job 的運(yùn)行步驟,可以包含一個(gè)或多個(gè)步驟(steps)

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - run: npm install
      - run: npm run build

環(huán)境變量

jobs.<job_id>.environment

使用單一環(huán)境名稱的示例

environment: staging_environment

使用環(huán)境名稱和 URL 的示例

environment:
  name: production_environment
  url: https://github.com

自定義環(huán)境變量

GitHub 會保留 GITHUB_ 環(huán)境變量前綴供 GitHub 內(nèi)部使用。設(shè)置有 GITHUB_ 前綴的環(huán)境變量或密碼將導(dǎo)致錯(cuò)誤。

- name: 測試 nodejs 獲取環(huán)境變量
  env:
    API_TOKEN: ${{ secrets.API_TOKEN }}

https://github.com/<用戶名>/<項(xiàng)目名稱>/settings/secrets 中添加 secrets API_TOKEN,在工作流中設(shè)置環(huán)境變量 API_TOKEN

表達(dá)式

if 條件下使用表達(dá)式時(shí),可以省略表達(dá)式語法 (${{ }}),因?yàn)?GitHub 會自動將 if 條件作為表達(dá)式求值

steps:
  - uses: actions/hello-world-action@v1.1
    if: github.repository == 'uiw/uiw-repo'
    # if: ${{ <expression> }}

設(shè)置環(huán)境變量的示例

env:
  MY_ENV_VAR: ${{ <expression> }}

操作符

  • ( ) (邏輯分組)
  • [ ] (指數(shù))
  • . (屬性取消引用)
  • ! (不是)
  • < (少于)
  • <= (小于或等于)
  • > (比...更棒)
  • >= (大于或等于)
  • == (平等的)
  • != (不相等)
  • && (和)
  • || (或者)

Github 上下文

屬性名稱類型描述
github (object)工作流程中任何作業(yè)或步驟期間可用的頂層上下文。
github.event (object)完整事件 web 掛鉤有效負(fù)載。 更多信息請參閱“觸發(fā)工作流程的事件”。
github.event_path (string)運(yùn)行器上完整事件 web 掛鉤有效負(fù)載的路徑。
github.workflow (string)工作流程的名稱。 如果工作流程文件未指定 name,此屬性的值將是倉庫中工作流程文件的完整路徑。
github.job (string)當(dāng)前作業(yè)的 job_id。
github.run_id (string)倉庫中每個(gè)運(yùn)行的唯一編號。 如果您重新執(zhí)行工作流程運(yùn)行,此編號不變。
github.run_number (string)倉庫中特定工作流程每個(gè)運(yùn)行的唯一編號。 此編號從 1(對應(yīng)于工作流程的第一個(gè)運(yùn)行)開始,然后隨著每個(gè)新的運(yùn)行而遞增。 如果您重新執(zhí)行工作流程運(yùn)行,此編號不變。
github.actor (string)發(fā)起工作流程運(yùn)行的用戶的登錄名。
github.repository (string)所有者和倉庫名稱。 例如 Codertocat/Hello-World。
github.repository_owner (string)倉庫所有者的名稱。 例如 Codertocat。
github.event_name (string)觸發(fā)工作流程運(yùn)行的事件的名稱。
github.sha (string)觸發(fā)工作流程的提交 SHA。
github.ref (string)觸發(fā)工作流程的分支或標(biāo)記參考。
github.head_ref (string)工作流程運(yùn)行中拉取請求的 head_ref 或來源分支。 此屬性僅在觸發(fā)工作流程運(yùn)行的事件為 pull_request 時(shí)才可用。
github.base_ref (string)工作流程運(yùn)行中拉取請求的 base_ref 或目標(biāo)分支。 此屬性僅在觸發(fā)工作流程運(yùn)行的事件為 pull_request 時(shí)才可用。
github.token (string)代表倉庫上安裝的 GitHub 應(yīng)用程序進(jìn)行身份驗(yàn)證的令牌。 這在功能上等同于 GITHUB_TOKEN 密碼。 更多信息請參閱“使用 GITHUB_TOKEN 驗(yàn)證身份”。
github.workspace (string)使用 checkout 操作時(shí)步驟的默認(rèn)工作目錄和倉庫的默認(rèn)位置。
github.action (string)正在運(yùn)行的操作的名稱。 在當(dāng)前步驟運(yùn)行腳本時(shí),GitHub 刪除特殊字符或使用名稱 run。 如果在同一作業(yè)中多次使用相同的操作,則名稱將包括帶有序列號的后綴。 例如,運(yùn)行的第一個(gè)腳本名稱為 run1,則第二個(gè)腳本將命名為 run2。 同樣,actions/checkout 第二次調(diào)用時(shí)將變成 actionscheckout2。

Github 上下文是訪問有關(guān)工作流運(yùn)行、運(yùn)行器環(huán)境、作業(yè)和步驟的信息的一種方式

默認(rèn)環(huán)境變量

環(huán)境變量描述
CI始終設(shè)置為 true
HOME用于存儲用戶數(shù)據(jù)的 GitHub 主目錄路徑。 例如 /github/home
GITHUB_WORKFLOW工作流程的名稱。
GITHUB_RUN_ID倉庫中每個(gè)運(yùn)行的唯一編號。 如果您重新執(zhí)行工作流程運(yùn)行,此編號不變。
GITHUB_RUN_NUMBER倉庫中特定工作流程每個(gè)運(yùn)行的唯一編號。 此編號從 1(對應(yīng)于工作流程的第一個(gè)運(yùn)行)開始,然后隨著每個(gè)新的運(yùn)行而遞增。 如果您重新執(zhí)行工作流程運(yùn)行,此編號不變。
GITHUB_ACTION操作唯一的標(biāo)識符 (id)。
GITHUB_ACTIONS當(dāng) GitHub 操作 運(yùn)行工作流程時(shí),始終設(shè)置為 true。 您可以使用此變量來區(qū)分測試是在本地運(yùn)行還是通過 GitHub 操作 運(yùn)行。
GITHUB_ACTION_PATHGitHub 操作所在的路徑
GITHUB_ACTOR發(fā)起工作流程的個(gè)人或應(yīng)用程序的名稱。 例如 octocat
GITHUB_API_URL返回 API URL。例如:https://api.github.com
GITHUB_REPOSITORY所有者和倉庫名稱。 例如 octocat/Hello-World
GITHUB_EVENT_NAME觸發(fā)工作流程的 web 掛鉤事件的名稱
GITHUB_EVENT_PATH具有完整 web 掛鉤事件有效負(fù)載的文件路徑。 例如 /github/workflow/event.json
GITHUB_WORKSPACEGitHub 工作空間目錄路徑。 如果您的工作流程使用 actions/checkout 操作,工作空間目錄將包含存儲倉庫副本的子目錄。 如果不使用 actions/checkout 操作,該目錄將為空。 例如 /home
GITHUB_SHA觸發(fā)工作流程的提交 SHA。 例如 ffac537e6cbbf9
GITHUB_REF觸發(fā)工作流程的分支或標(biāo)記參考。 例如 refs/heads/feature-branch-1。 如果分支或標(biāo)記都不適用于事件類型,則變量不會存在
GITHUB_HEAD_REF僅為復(fù)刻的倉庫設(shè)置。頭部倉庫的分支
GITHUB_BASE_REF僅為復(fù)刻的倉庫設(shè)置?;A(chǔ)倉庫的分支

另見: 默認(rèn)環(huán)境變量

直接常量

作為表達(dá)式的一部分,可以使用 boolean, null, numberstring數(shù)據(jù)類型

env:
  myNull: ${{ null }}
  myBoolean: ${{ false }}
  myIntegerNumber: ${{ 711 }}
  myFloatNumber: ${{ -9.2 }}
  myHexNumber: ${{ 0xff }}
  myExponentialNumber: ${{ -2.99e-2 }}
  myString: Mona the Octocat
  myStringInBraces: ${{ 'It''s source!' }}

函數(shù) contains

使用字符串的示例

contains('Hello world', 'llo') // 返回 true

使用對象過濾器的示例返回 true

contains(github.event.issue.labels.*.name, 'bug')

另見: 函數(shù) contains

函數(shù) startsWith

startsWith('Hello world', 'He') // 返回 true

另見: 函數(shù) startsWith,此函數(shù)不區(qū)分大小寫

函數(shù) format

format('{{Hello {0} {1} {2}!}}', 'Mona', 'the', 'Octocat')
// 返回 '{Hello Mona the Octocat!}'.

另見: 函數(shù) format

函數(shù) join

join(github.event.issue.labels.*.name, ', ')
// 也許返回 'bug, help wanted'.

另見: 函數(shù) join

函數(shù) toJSON

toJSON(job)
// 也許返回 { "status": "Success" }.

另見: 函數(shù) toJSON

函數(shù)

:-:-
fromJSON返回 JSON 對象或 JSON 數(shù)據(jù)類型的值 #
hashFiles返回與路徑模式匹配的文件集的單個(gè)哈希 #
success當(dāng)前面的步驟都沒失敗或被取消時(shí)返回 true #
always使步驟始終執(zhí)行,返回 true 即使取消也是如此 #
cancelled如果工作流被取消,則返回 true #
failure當(dāng)作業(yè)的任何先前步驟失敗時(shí)返回 true #

函數(shù) success()

steps:
  ...
  - name: 作業(yè)已成功
    if: ${{ success() }}

函數(shù) failure()

steps:
  ...
  - name: 作業(yè)失敗
    if: ${{ failure() }}

常用實(shí)例

獲取版本信息

- name: Test
  run: |
    # Strip git ref prefix from version
    echo "${{ github.ref }}"
    # VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')

    # # Strip "v" prefix from tag name
    # [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
    echo "$VERSION"

提交到 gh-pages 分支

- name: Deploy
  uses: peaceiris/actions-gh-pages@v3
  with:
    github_token: ${{secrets.GITHUB_TOKEN}}
    publish_dir: ./build

修改 package.json

- name: Modify Version
  shell: bash
  run: |
    node -e 'var pkg = require("./package.json"); pkg.version= (new Date().getFullYear().toString().substr(2)) + "." + (new Date().getMonth() + 1) + "." + (new Date().getDate()); require("fs").writeFileSync("./package.json", JSON.stringify(pkg, null, 2))'

使用 github-action-package 修改 name 字段

- name: package.json info
  uses: jaywcjlove/github-action-package@main
  with:
    rename: '@wcj/github-package-test'

克隆帶有 Submodule 的倉庫

- name: Checkout
  uses: actions/checkout@v3
  with:
    path: main
    submodules: true

submodulestrue 檢出子模塊或 recursive 遞歸檢出子模塊

- name: Clone sub repository
  shell: bash
  run: |
    auth_header="$(git config --local --get http.https://github.com/.extraheader)"
    # git submodule sync --recursive
    # git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --remote --force --recursive --checkout ant.design

步驟依賴作業(yè)

使用 jobs.<job_id>.needs 識別在此作業(yè)運(yùn)行之前必須成功完成的任何作業(yè)。它可以是一個(gè)字符串,也可以是字符串?dāng)?shù)組。 如果某個(gè)作業(yè)失敗,則所有需要它的作業(yè)都會被跳過,除非這些作業(yè)使用讓該作業(yè)繼續(xù)的條件表達(dá)式。

jobs:
  job1:
  job2:
    needs: job1
  job3:
    needs: [job1, job2]

在此示例中,job1 必須在 job2 開始之前成功完成,而 job3 要等待 job1job2 完成。此示例中的作業(yè)按順序運(yùn)行:

? job1
? job2
? job3

配置如下

jobs:
  job1:
  job2:
    needs: job1
  job3:
    if: ${{ always() }}
    needs: [job1, job2]

在此示例中,job3 使用 always() 條件表達(dá)式,因此它始終在 job1job2 完成后運(yùn)行,不管它們是否成功。

同步 Gitee

- name: Sync to Gitee
  run: |
    mirror() {
      git clone "https://github.com/$1/$2"
      cd "$2"
      git remote add gitee "https://jaywcjlove:${{ secrets.GITEE_TOKEN }}@gitee.com/uiw/$2"
      git remote set-head origin -d
      git push gitee --prune +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*
      cd ..
    }
    mirror uiwjs uiw

提交 NPM 包

- run: npm publish --access public
  env:
    NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

獲取 NPM_TOKEN,可以通過 npm 賬號創(chuàng)建 token

npm token list [--json|--parseable] # 查看
npm token create [--read-only] [--cidr=1.1.1.1/24,2.2.2.2/16] # 創(chuàng)建
npm token revoke <id|token> # 撤銷

可以使用 JS-DevTools/npm-publish 提交

- name:  ?? @province-city-china/data
  uses: JS-DevTools/npm-publish@v1
  with:
    token: ${{ secrets.NPM_TOKEN }}
    package: packages/data/package.json

它有個(gè)好處,檢測 package.json 中版本號是否發(fā)生變更,來決定是否提交版本,不會引發(fā)流程錯(cuò)誤。

步驟作業(yè)文件共享

Artifacts 是 GitHub Actions 為您提供持久文件并在運(yùn)行完成后使用它們或在作業(yè)(文檔)之間共享的一種方式。

要創(chuàng)建工件并使用它,您將需要不同的操作:上傳和下載。 要上傳文件或目錄,您只需像這樣使用它:

steps:
  - uses: actions/checkout@v2
  - run: mkdir -p path/to/artifact
  - run: echo hello > path/to/artifact/a.txt
  - uses: actions/upload-artifact@v2
    with:
      name: my-artifact
      path: path/to/artifact/a.txt

然后下載 artifact 以使用它:

steps:
  - uses: actions/checkout@v2
  - uses: actions/download-artifact@v2
    with:
      name: my-artifact

Node.js

- name: Setup Node
  uses: actions/setup-node@v2
  with:
    node-version: 14

使用矩陣策略 在 nodejs 不同版本中運(yùn)行

strategy:
  matrix:
    node-version: [10.x, 12.x, 14.x]

steps:
  - uses: actions/checkout@v2
  - name: 使用 Node ${{ matrix.node-version }}
    uses: actions/setup-node@v1
    with:
      node-version: ${{ matrix.node-version }}
  - run: npm ci
  - run: npm run build --if-present
  - run: npm test

提交 docker 鏡像

# https://www.basefactor.com/github-actions-docker
- name: Docker login
  run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }}

- name: Build ant.design image
  run: |
    cd ./ant\.design
    docker build -t ant.design .
- name: Tags & Push docs
  run: |
    # Strip git ref prefix from version
    VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')

    # Strip "v" prefix from tag name
    [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')

    docker tag ant.design ${{ secrets.DOCKER_USER }}/ant.design:$VERSION
    docker tag ant.design ${{ secrets.DOCKER_USER }}/ant.design:latest
    docker push ${{ secrets.DOCKER_USER }}/ant.design:$VERSION
    docker push ${{ secrets.DOCKER_USER }}/ant.design:latest

創(chuàng)建一個(gè) tag

- name: Create Tag
  id: create_tag
  uses: jaywcjlove/create-tag-action@main
  with:
    package-path: ./package.json

根據(jù) package-path 指定的 package.json 檢測 version 是否發(fā)生變化來創(chuàng)建 tag

生成 git 提交日志

- name: Generate Changelog
  id: changelog
  uses: jaywcjlove/changelog-generator@main
  with:
    filter-author: (小弟調(diào)調(diào)?)

- name: Get the changelog
  run: echo "${{ steps.changelog.outputs.changelog }}"

提交到 GitHub docker 鏡像倉庫

- name: '登錄到 GitHub 注冊表'
  run: echo ${{ github.token }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: '編譯 docker image'
  run: docker build -t ghcr.io/jaywcjlove/reference:latest .

- name: '推送到 GitHub 注冊表中'
  run: docker push ghcr.io/jaywcjlove/reference:latest

- name: '標(biāo)記 docker 鏡像并發(fā)布到 GitHub 注冊表'
  if: steps.create_tag.outputs.successful
  run: |
    echo "version: v${{ steps.changelog.outputs.version }}"
    docker tag ghcr.io/jaywcjlove/reference:latest ghcr.io/jaywcjlove/reference:${{steps.changelog.outputs.version}}
    docker push ghcr.io/jaywcjlove/reference:${{steps.changelog.outputs.version}}

提交 commit 到 master 分支

- name: 生成一個(gè)文件,并將它提交到 master 分支
  run: |
    # Strip git ref prefix from version
    VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
    COMMIT=released-${VERSION}
    # Strip "v" prefix from tag name
    [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
    echo "輸出版本號:$VERSION"
    # 將版本輸出到當(dāng)前 VERSION 文件中
    echo "$VERSION" > VERSION
    echo "1. 輸出Commit:$commit"
    echo "2. Released $VERSION"
    git fetch
    git config --local user.email "action@github.com"
    git config --local user.name "GitHub Action"
    git add .
    git commit -am $COMMIT
    git branch -av
    git pull origin master

- name: 將上面的提交 push 到 master 分支
  uses: ad-m/github-push-action@master
  with:
    github_token: ${{ secrets.GITHUB_TOKEN }}

作業(yè)之間共享數(shù)據(jù)

創(chuàng)建一個(gè)文件,然后將其作為構(gòu)件上傳

jobs:
  example-job:
    name: Save output
    steps:
      - shell: bash
        run: |
          expr 1 + 1 > output.log
      - name: Upload output file
        uses: actions/upload-artifact@v3
        with:
          name: output-log-file
          path: output.log

可以下載名為 output-log-file 的工件

jobs:
  example-job:
    steps:
      - name: Download a single artifact
        uses: actions/download-artifact@v3
        with:
          name: output-log-file

指定運(yùn)行命令的工作目錄

- name: Clean temp directory
  run: rm -rf *
  working-directory: ./temp

使用 working-directory 關(guān)鍵字,您可以指定運(yùn)行命令的工作目錄(./temp)

defaults.run

jobs:
  job1:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
        working-directory: scripts

作業(yè)中的所有 run 步驟提供默認(rèn)的 shellworking-directory

jobs.<job_id>.steps[*].shell

使用 bash 運(yùn)行腳本

steps:
  - name: Display the path
    run: echo $PATH
    shell: bash

運(yùn)行 python 腳本

steps:
  - name: Display the path
    run: |
      import os
      print(os.environ['PATH'])
    shell: python

您可以使用 shell 關(guān)鍵字覆蓋運(yùn)行器操作系統(tǒng)中的默認(rèn) shell 設(shè)置

一些 actions 推薦

:-:-
create-tag-action根據(jù) package.json 創(chuàng)建 Tag / Release
changelog-generator生成 changelog 日志
github-action-modify-file-content修改倉庫文件內(nèi)容
github-action-contributors生成貢獻(xiàn)(contributors.svg)圖片
generated-badges生成徽章(Badges)圖片
coverage-badges-cli生成覆蓋率徽章(Badges)圖片
action-ejs基于 ejs 生成 HTML
github-action-package修改 JSON 文件內(nèi)容
github-action-read-file讀取文件內(nèi)容
markdown-to-html-cliMarkdown 轉(zhuǎn)換成 HTML
ncipollo/release-action創(chuàng)建 Release
peaceiris/actions-gh-pages將文件或文件夾內(nèi)容提交到 gh-pages 分支

在 Github 中創(chuàng)建 Docker 鏡像

- name: Set up Docker Buildx
  uses: docker/setup-buildx-action@v2
- name: 登錄 GitHub 容器注冊表
  uses: docker/login-action@v2
  with:
    registry: ghcr.io
    username: ${{ github.actor }}
    password: ${{ secrets.GITHUB_TOKEN }}

- name: 構(gòu)建并推送 image:latest
  uses: docker/build-push-action@v3
  with:
    push: true
    context: .
    platforms: linux/amd64,linux/arm64
    tags: ghcr.io/jaywcjlove/reference:latest

- name: 構(gòu)建并推送 image:tags
  uses: docker/build-push-action@v3
  if: steps.create_tag.outputs.successful
  with:
    push: true
    context: .
    platforms: linux/amd64,linux/arm64
    tags: ghcr.io/jaywcjlove/reference:${{steps.changelog.outputs.version}}

在 Docker Hub 中創(chuàng)建 Docker 鏡像

- name: Set up Docker Buildx
  uses: docker/setup-buildx-action@v2
- name: 登錄到 Docker Hub
  uses: docker/login-action@v2
  with:
    username: ${{ secrets.DOCKER_USER }}
    password: ${{ secrets.DOCKER_PASSWORD }}

- name: 構(gòu)建并推送 image:latest
  uses: docker/build-push-action@v3
  with:
    push: true
    context: .
    platforms: linux/amd64,linux/arm64
    tags: ${{ secrets.DOCKER_USER }}/reference:latest

- name: 構(gòu)建并推送 image:tags
  uses: docker/build-push-action@v3
  if: steps.create_tag.outputs.successful
  with:
    push: true
    context: .
    platforms: linux/amd64,linux/arm64
    tags: ${{ secrets.DOCKER_USER }}/reference:${{steps.changelog.outputs.version}}

檢查簽出倉庫并安裝 nodejs

- uses: actions/checkout@v3
- uses: actions/setup-node@v3
  with:
    node-version: 16

生成貢獻(xiàn)者頭像列表

- name: Generate Contributors Images
  uses: jaywcjlove/github-action-contributors@main
  id: contributors
  with:
    output: dist/CONTRIBUTORS.svg
    avatarSize: 42

忽略失敗

- run: npm publish
  continue-on-error: true
  env:
    NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

當(dāng) npm 推送包失敗不影響整個(gè)流程,可用于自動發(fā)包

GitLab CI/CD 遷移到 GitHub Actions

語法示例

GitLab CI/CD

job1:
  variables:
    GIT_CHECKOUT: "true"
  script:
    - echo "Run your script here"

GitHub Actions

jobs:
  job1:
    steps:
      - uses: actions/checkout@v3
      - run: echo "Run your script here"

運(yùn)行程序

GitLab CI/CD

windows_job:
  tags:
    - windows
  script:
    - echo Hello, %USERNAME%!

linux_job: tags:
    - linux script:
    - echo "Hello, $USER!"

GitHub Actions

windows_job:
  runs-on: windows-latest
  steps:
    - run: echo Hello, %USERNAME%!

linux_job:
  runs-on: ubuntu-latest
  steps:
    - run: echo "Hello, $USER!"

在不同的平臺上運(yùn)行作業(yè)

Docker 映像

GitLab CI/CD

my_job:
  image: node:10.16-jessie

GitHub Actions

jobs:
  my_job:
    container: node:10.16-jessie

條件和表達(dá)式語法

GitLab CI/CD

deploy_prod:
  stage: deploy
  script:
    - echo "部署到生產(chǎn)服務(wù)器"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'

GitHub Actions

jobs:
  deploy_prod:
    if: contains( github.ref, 'master')
    runs-on: ubuntu-latest
    steps:
      - run: echo "部署到生產(chǎn)服務(wù)器"

Artifacts

GitLab CI/CD

script:
artifacts:
  paths:
    - math-homework.txt

GitHub Actions

- name: Upload math result for job 1
  uses: actions/upload-artifact@v3
  with:
    name: homework
    path: math-homework.txt

作業(yè)之間的依賴關(guān)系

GitLab CI/CD

stages:
  - build
  - test
  - deploy

build_a: stage: build script:
    - echo "該作業(yè)將首先運(yùn)行"

build_b: stage: build script:
    - echo "該作業(yè)將首先運(yùn)行,與 build_a 并行"

test_ab: stage: test script:
    - echo "此作業(yè)將在 build_a 和 build_b 完成后運(yùn)行"

deploy_ab: stage: deploy script:
    - echo "此作業(yè)將在 test_ab 完成后運(yùn)行"

GitHub Actions

jobs:
  build_a:
    runs-on: ubuntu-latest
    steps:
      - run: echo "該作業(yè)將首先運(yùn)行"

  build_b:
    runs-on: ubuntu-latest
    steps:
      - run: echo "該作業(yè)將首先運(yùn)行,與 build_a 并行"

  test_ab:
    runs-on: ubuntu-latest
    needs: [build_a,build_b]
    steps:
      - run: echo "此作業(yè)將在 build_a 和 build_b 完成后運(yùn)行"

  deploy_ab:
    runs-on: ubuntu-latest
    needs: [test_ab]
    steps:
      - run: echo "此作業(yè)將在 test_ab 完成后運(yùn)行"

緩存

GitLab CI/CD

image: node:latest

cache: key: $CI_COMMIT_REF_SLUG paths:
    - .npm/

before_script:
  - npm ci --cache .npm --prefer-offline

test_async: script:
    - node ./specs/start.js ./specs/async.spec.js

GitHub Actions

jobs:
  test_async:
    runs-on: ubuntu-latest
    steps:
    - name: Cache node modules
      uses: actions/cache@v3
      with:
        path: ~/.npm
        key: v1-npm-deps-${{ hashFiles('**/package-lock.json') }}
        restore-keys: v1-npm-deps-

數(shù)據(jù)庫和服務(wù)容器

GitLab CI/CD

container-job:
  variables:
    POSTGRES_PASSWORD: postgres
    # PostgreSQL 服務(wù)容器通信的主機(jī)名
    POSTGRES_HOST: postgres
    # 默認(rèn)的 PostgreSQL 端口
    POSTGRES_PORT: 5432
  image: node:10.18-jessie
  services:
    - postgres
  script:
    # 執(zhí)行 package.json 文件中
    # 所有依賴項(xiàng)的全新安裝
    - npm ci
    # 運(yùn)行創(chuàng)建 PostgreSQL 客戶端的腳本,
    # 用數(shù)據(jù)填充客戶端,并檢索數(shù)據(jù)
    - node client.js
  tags:
    - docker

GitHub Actions

jobs:
  container-job:
    runs-on: ubuntu-latest
    container: node:10.18-jessie

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres

    steps:
      - name: Check out repository code
        uses: actions/checkout@v3

      # 執(zhí)行 package.json 文件中
      # 所有依賴項(xiàng)的全新安裝
      - name: Install dependencies
        run: npm ci

      - name: Connect to PostgreSQL
        # 運(yùn)行創(chuàng)建 PostgreSQL 客戶端的腳本,
        # 用數(shù)據(jù)填充客戶端,并檢索數(shù)據(jù)
        run: node client.js
        env:
          # PostgreSQL 服務(wù)容器通信的主機(jī)名
          POSTGRES_HOST: postgres
          # 默認(rèn)的 PostgreSQL 端口
          POSTGRES_PORT: 5432

另見