亚洲成A人片在线观看网站_成年网站免费视频A在线双飞_日日日日做夜夜夜夜无码_久久夜色撩人精品国产小说

Github Actions 備忘清單

本備忘單總結了 常用的配置說明,以供快速參考。

入門

介紹

GitHub 的倉庫中自動化、自定義和執行軟件開發工作流程,有四個基本的概念,如下:

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

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

配置文件

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

# 任務
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

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

指定觸發

push 事件觸發 workflow

on: push

push 事件或 pull_request 事件都可以觸發 workflow

on: [push, pull_request]

只有在 main 分支 push 事件觸發 workflow

on:
  push:
    branches:
      - main

push 事件觸發 workflowdocs 目錄下的更改 push 事件不觸發 workflow

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

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

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

版本發布為 published 時運行工作流程。

on:
  release:
    types: [published]

多項任務

jobs:
  my_first_job:  # 第一個任務
    name: My first job

  my_second_job: # 第二個任務
    name: My second job

通過 jobs (jobs.<job_id>.name)字段,配置一項或多項需要執行的任務

多項任務依賴關系

通過 needs (jobs.<job_id>.needs)字段,指定當前任務的依賴關系

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

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

多項任務傳遞參數

jobs:
  job1:
    runs-on: ubuntu-latest
    # 將步驟輸出映射到作業輸出
    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}}

指定每項任務的虛擬機環境

runs-on: ubuntu-latest

指定運行所需要的虛擬機環境,?? 它是必填字段

jobs:
  build: # 任務名稱
    runs-on: ubuntu-latest # 虛擬機環境配置

  • 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)

另見:

指定每項任務的步驟

每個步驟都可以指定以下三個字段

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

steps 字段指定每個 Job 的運行步驟,可以包含一個或多個步驟(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

環境變量

jobs.<job_id>.environment

使用單一環境名稱的示例

environment: staging_environment

使用環境名稱和 URL 的示例

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

自定義環境變量

GitHub 會保留 GITHUB_ 環境變量前綴供 GitHub 內部使用。設置有 GITHUB_ 前綴的環境變量或密碼將導致錯誤。

- name: 測試 nodejs 獲取環境變量
  env:
    API_TOKEN: ${{ secrets.API_TOKEN }}

//github.com/<用戶名>/<項目名稱>/settings/secrets 中添加 secrets API_TOKEN,在工作流中設置環境變量

表達式

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

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

設置環境變量的示例

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

操作符

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

Github 上下文

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

是訪問有關工作流運行、運行器環境、作業和步驟的信息的一種方式

默認環境變量

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

另見:

直接常量

作為表達式的一部分,可以使用 boolean, null, numberstring數據類型

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

函數 contains

使用字符串的示例

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

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

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

另見:

函數 startsWith

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

另見: ,此函數不區分大小寫

函數 format

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

另見:

函數 join

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

另見:

函數 toJSON

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

另見:

函數

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

函數 success()

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

函數 failure()

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

常用實例

獲取版本信息

- 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))'

使用 修改 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.//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

步驟依賴作業

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

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

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

? job1
? job2
? job3

配置如下

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

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

同步 Gitee

- name: Sync to Gitee
  run: |
    mirror() {
      git clone "//github.com/$1/$2"
      cd "$2"
      git remote add gitee "//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,可以通過 賬號創建 token

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

可以使用 提交

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

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

步驟作業文件共享

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

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

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 不同版本中運行

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 鏡像

# //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

創建一個 tag

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

根據 package-path 指定的 package.json 檢測 version 是否發生變化來創建 tag

生成 git 提交日志

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

- 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: '標記 docker 鏡像并發布到 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: 生成一個文件,并將它提交到 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"
    # 將版本輸出到當前 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 }}

作業之間共享數據

創建一個文件,然后將其作為構件上傳

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

指定運行命令的工作目錄

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

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

defaults.run

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

作業中的所有 run 步驟提供默認的 shellworking-directory

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

使用 bash 運行腳本

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

運行 python 腳本

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

您可以使用 shell 關鍵字覆蓋運行器操作系統中的默認 shell 設置

一些 actions 推薦

:-:-
根據 package.json 創建 Tag / Release
生成 changelog 日志
修改倉庫文件內容
生成貢獻(contributors.svg)圖片
生成徽章(Badges)圖片
生成覆蓋率徽章(Badges)圖片
基于 ejs 生成 HTML
修改 JSON 文件內容
讀取文件內容
Markdown 轉換成 HTML
創建 Release
將文件或文件夾內容提交到 gh-pages 分支

在 Github 中創建 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: 構建并推送 image:latest
  uses: docker/build-push-action@v3
  with:
    push: true
    context: .
    platforms: linux/amd64,linux/arm64
    tags: ghcr.io/jaywcjlove/reference:latest

- name: 構建并推送 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 中創建 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: 構建并推送 image:latest
  uses: docker/build-push-action@v3
  with:
    push: true
    context: .
    platforms: linux/amd64,linux/arm64
    tags: ${{ secrets.DOCKER_USER }}/reference:latest

- name: 構建并推送 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

生成貢獻者頭像列表

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

npm 推送包失敗不影響整個流程,可用于自動發包

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"

運行程序

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!"

在不同的平臺上運行作業

Docker 映像

GitLab CI/CD

my_job:
  image: node:10.16-jessie

GitHub Actions

jobs:
  my_job:
    container: node:10.16-jessie

條件和表達式語法

GitLab CI/CD

deploy_prod:
  stage: deploy
  script:
    - echo "部署到生產服務器"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'

GitHub Actions

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

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

作業之間的依賴關系

GitLab CI/CD

stages:
  - build
  - test
  - deploy

build_a: stage: build script:
    - echo "該作業將首先運行"

build_b: stage: build script:
    - echo "該作業將首先運行,與 build_a 并行"

test_ab: stage: test script:
    - echo "此作業將在 build_a 和 build_b 完成后運行"

deploy_ab: stage: deploy script:
    - echo "此作業將在 test_ab 完成后運行"

GitHub Actions

jobs:
  build_a:
    runs-on: ubuntu-latest
    steps:
      - run: echo "該作業將首先運行"

  build_b:
    runs-on: ubuntu-latest
    steps:
      - run: echo "該作業將首先運行,與 build_a 并行"

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

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

緩存

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-

數據庫和服務容器

GitLab CI/CD

container-job:
  variables:
    POSTGRES_PASSWORD: postgres
    # PostgreSQL 服務容器通信的主機名
    POSTGRES_HOST: postgres
    # 默認的 PostgreSQL 端口
    POSTGRES_PORT: 5432
  image: node:10.18-jessie
  services:
    - postgres
  script:
    # 執行 package.json 文件中
    # 所有依賴項的全新安裝
    - npm ci
    # 運行創建 PostgreSQL 客戶端的腳本,
    # 用數據填充客戶端,并檢索數據
    - 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

      # 執行 package.json 文件中
      # 所有依賴項的全新安裝
      - name: Install dependencies
        run: npm ci

      - name: Connect to PostgreSQL
        # 運行創建 PostgreSQL 客戶端的腳本,
        # 用數據填充客戶端,并檢索數據
        run: node client.js
        env:
          # PostgreSQL 服務容器通信的主機名
          POSTGRES_HOST: postgres
          # 默認的 PostgreSQL 端口
          POSTGRES_PORT: 5432

另見

  • (jaywcjlove.github.io)
  • (docs.github.com)
  • (docs.github.com)