自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

10 個你該了解的 GitHub Actions 進階技巧

系統(tǒng)
在執(zhí)行 workflow 時, 允許在 GitHub Actions 頁面輸入?yún)?shù),控制執(zhí)行邏輯。我們可以將人工處理的邏輯,在 GitHub Actions 參數(shù)化執(zhí)行,適用于持續(xù)部署場景。

[[379607]]

 本文轉載自微信公眾號「問其」,作者陳少文。轉載本文請聯(lián)系問其公眾號。

1. workflow 執(zhí)行時,傳入?yún)?shù)

在執(zhí)行 workflow 時, 允許在 GitHub Actions 頁面輸入?yún)?shù),控制執(zhí)行邏輯。我們可以將人工處理的邏輯,在 GitHub Actions 參數(shù)化執(zhí)行,適用于持續(xù)部署場景。

  1. on:  
  2.   workflow_dispatch: 
  3.     inputs: 
  4.       logLevel: 
  5.         description: 'Log level'      
  6.         required: true 
  7.         default'warning' 
  8.       tags: 
  9.         description: 'Test scenario tags'   
  10. jobs: 
  11.   printInputs: 
  12.     runs-on: ubuntu-latest 
  13.     steps: 
  14.     - run: | 
  15.         echo "Log level: ${{ github.event.inputs.logLevel }}" 
  16.         echo "Tags: ${{ github.event.inputs.tags }}"  

上面的 workflow 執(zhí)行時,會彈出如下對話框。

2. Job 編排控制執(zhí)行順序

一個 workflow 由很多個 job 組成,借助于 needs 參數(shù),我們可以管理這些 job 之間的依賴,控制其執(zhí)行流程。

  1. on: push 
  2. jobs: 
  3.   job1: 
  4.     runs-on: ubuntu-latest 
  5.     steps: 
  6.       - run: echo "job1" 
  7.   job2: 
  8.     runs-on: ubuntu-latest 
  9.     steps: 
  10.       - run: sleep 5 
  11.     needs: job1 
  12.   job3: 
  13.     runs-on: ubuntu-latest 
  14.     steps: 
  15.       - run: sleep 10 
  16.     needs: job1 
  17.   job4: 
  18.     runs-on: ubuntu-latest 
  19.     steps: 
  20.       - run: echo "job4" 
  21.     needs: [job2, job3] 

上面的 workflows 執(zhí)行時,job2 和 job3 會等 job1 執(zhí)行成功時才執(zhí)行,job4 會等 job2 和 job3 執(zhí)行成功時才執(zhí)行。

3. 用于項目管理

Kubernetes 基于 ChatOps 使用 Prow 協(xié)調社區(qū)有序協(xié)作。但并不是每個團隊,都愿意搭建并維護一套 Prow 機器人系統(tǒng)。ChatOps 實現(xiàn)的核心是事件驅動,這在 GitHub 中使用 Actions 也能實現(xiàn)。

下面是幾個項目管理相關的 action

  • 根據(jù)修改的目錄添加標簽
  1. - uses: actions/labeler@main 
  2.   with
  3.     repo-token: "${{ secrets.GITHUB_TOKEN }}" 

在配置文件 .github/workflows/labeler.yml 中添加規(guī)則,給對 docs 目錄進行修改的 Pull Requests(以下簡稱 PR) 自動添加 docs_label 標簽:

  1. docs_label: 
  2.   - ./docs/* 
  • 根據(jù)標簽添加 Issues 到 Projects

使用 srggrs/assign-one-project-github-action , 我們可以將新增的 Issues 或者 PR 添加到指定的 Projects 中。

  1. name: Assign NEW issues and NEW pull requests to project 2 
  2.   uses: srggrs/assign-one-project-github-action@1.2.0 
  3.   if: github.event.action == 'opened' 
  4.   with
  5.     project: 'https://github.com/srggrs/assign-one-project-github-action/projects/2' 

也可以將包含指定標簽的 Issues 或 PR 添加到指定 Project 的指定 Column 中。

  1. name: Assign issues and pull requests with `bug` label to project 3 
  2.   uses: srggrs/assign-one-project-github-action@1.2.0 
  3.   if: | 
  4.     contains(github.event.issue.labels.*.name'bug') || 
  5.     contains(github.event.pull_request.labels.*.name'bug'
  6.   with
  7.     project: 'https://github.com/srggrs/assign-one-project-github-action/projects/3' 
  8.     column_name: 'Labeled' 
  • 清理長時間無人跟進的 Issues

如果一個 Issue 長達 30 天沒有更新,那么下面的 workflow 將會再等 5 天,然后將其關閉。

  1. name'Close stale issues and PRs' 
  2. on
  3.   schedule: 
  4.     - cron: '30 1 * * *' 
  5. jobs: 
  6.   stale: 
  7.     runs-on: ubuntu-latest 
  8.     steps: 
  9.       - uses: actions/stale@v3 
  10.         with
  11.           stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.' 
  12.           days-before-stale: 30 
  13.           days-before-close: 5 

GitHub 上的項目管理,主要是圍繞 Issues、Projects、Labels、Pull Requests 展開,可以在 GitHub Actions 的 Marketplace 中搜索相關的 Action 使用。

4. 在線調試

在使用 GitHub Actions 的過程中,如果需要登錄到 Runner 上調試命令,那么下面這個技巧你一定會感興趣。

  1. - uses: shaowenchen/debugger-action@v2 
  2.   name: debugger 
  3.   timeout-minutes: 30 
  4.   continue-on-error: true 
  5.   with
  6.     ngrok_token: ${{ secrets.NGROK_TOKEN }} 

只需要去 Ngrok 官網(wǎng)申請一個 token,就可以通過 ssh 遠程登錄到 Runner。當然,也可以暴露 Runner 上的服務,提供外網(wǎng)訪問的鏈接,最長可達 6 小時。

在執(zhí)行日志中,我們可以找到 ssh 的登錄鏈接,使用 root/root 即可登錄 Runner。如果配置了 web 的端口映射,還可以查看到相關的服務鏈接。

5. 設置緩存

緩存能有效地加快構建速度,減少網(wǎng)絡請求,復用中間碼。這對于 Java、Nodejs、Python 等項目,非常有用。

  1. name: Get yarn cache directory path 
  2.   id: yarn-cache-dir-path 
  3.   run: echo "::set-output name=dir::$(yarn cache dir)" 
  4. - uses: actions/cache@v2 
  5.   id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) 
  6.   with
  7.     path: ${{ steps.yarn-cache-dir-path.outputs.dir }} 
  8.     key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} 
  9.     restore-keys: | 
  10.       ${{ runner.os }}-yarn- 

6. 檢測項目中的問題鏈接

項目維護時間長了之后,最令人頭疼的就是文檔。研發(fā)、測試跟進的是代碼、功能,而文檔卻時常無人更新。缺少維護的文檔,會讓潛在參與者流失。下面這個 Action 能檢測文檔中的 Broken 鏈接。

  1. nameCheck Markdown links 
  2. on: push 
  3. jobs: 
  4.   markdown-link-check
  5.     runs-on: ubuntu-latest 
  6.     steps: 
  7.     - uses: actions/checkout@master 
  8.     - uses: gaurav-nelson/github-action-markdown-link-check@v1 
  9.       with
  10.         use-quiet-mode: 'yes' 
  11.         config-file: '.github/workflows/checklink_config.json' 
  12.         max-depth: 3 

gaurav-nelson/github-action-markdown-link-check 支持自定義配置,非常靈活易用,堪稱必備 Action。

下面是一個 .github/workflows/checklink_config.json 的示例:

  1.   "replacementPatterns": [ 
  2.     { 
  3.       "pattern""^/"
  4.       "replacement""/github/workspace/" 
  5.     } 
  6.   ], 
  7.   "aliveStatusCodes": [ 
  8.     429, 
  9.     200 
  10.   ] 

最后在 GitHub Actions 日志頁面,會輸出這樣的檢測結果:

  1. =========================> MARKDOWN LINK CHECK <========================= 
  2. FILE: ./docs/governance.md 
  3. 4 links checked. 
  4. FILE: ./docs/configuration/cri.md 
  5. [✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable 
  6. 7 links checked. 
  7. ERROR: 1 dead links found! 
  8. [✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable → Status: 404 
  9. FILE: ./docs/configuration/kubeedge.md 
  10. 21 links checked. 
  11. ========================================================================= 

7. Job 批量執(zhí)行,參數(shù)排列組合執(zhí)行任務

數(shù)據(jù)驅動測試的場景下,可以通過輸入的參數(shù)控制測試的流程。在 GitHub Actions 中,我們也可以通過參數(shù)化的方式,批量地執(zhí)行或編排流程。

GitHub Actions 會將 matrix 中的每個參數(shù)排列組合,產(chǎn)生一個新的運行實例。

  1. on: push 
  2. jobs: 
  3.   node: 
  4.     runs-on: ${{ matrix.os }} 
  5.     strategy: 
  6.       matrix: 
  7.         os: [ubuntu-16.04, ubuntu-18.04] 
  8.         node: [6, 8, 10] 
  9.     steps: 
  10.       - uses: actions/setup-node@v1 
  11.         with
  12.           node-version: ${{ matrix.node }} 
  13.       - run: node --version 

上面的 workflow 執(zhí)行時, 會執(zhí)行 6 個 job。

無論是用來測試兼容性, 還是批量執(zhí)行 Job, 都是非常好的。

8. 拷貝 Action 的 Badge 狀態(tài)顯示在文檔中

通常,我們使用 GitHub Actions 對項目進行代碼分析、執(zhí)行測試、編譯、打包、構建、推送鏡像等。這些行為對于保證項目的穩(wěn)定,至關重要。

但并不是每個人都會關注 Actions 的執(zhí)行細節(jié)。我們可以在顯眼的地方,給出這些過程的最終實時狀態(tài),以提醒用戶和開發(fā)者。如果 main 分支構建失敗了,能提醒用戶謹慎使用,能提醒研發(fā)盡快修復問題。

在 GitHub Actions 頁面中, 點擊 Create status badge。

將彈框中的 URL 鏈接,增加在 Readme 文檔中,即可實時快速地查看到 workflow 的執(zhí)行結果。

9. 精準 hook GitHub 上的行為

workflow 通過 on 關鍵字定義觸發(fā)條件。主要有三類觸發(fā)事件:

  • 人工觸發(fā)
  1. on: workflow_dispatch 
  • 定時觸發(fā)

每隔 15 分鐘觸發(fā)一次 workflows。

  1. on
  2.   schedule: 
  3.     - cron:  '*/15 * * * *' 
  • Webhook 觸發(fā)

我們在 GitHub 上的操作,比如創(chuàng)建 Issues、新增 Deployment 等,都能夠通過 API 獲取到相關的事件。通過這些事件,我們可以精準地定制 workflow 的行為。通常我們都是基于 push 或者 pull requests 觸發(fā),下面列舉幾個不常見的示例:

當有人 fork 倉庫時觸發(fā)

  1. on
  2.   fork 

當有人 star 倉庫時觸發(fā)

  1. on
  2.   watch: 
  3.     types: [started] 

當有新建的 Issue 時觸發(fā)

  1. on
  2.   issues: 
  3.     types: [opened] 

10. 開發(fā)一個 Action 很簡單

如果在 Marketplace 找不到合適的 Action,那么自己開發(fā) Action 也是一個不錯的選擇。

其實,開發(fā)一個 Action 沒有想象中那么難。一個 Action 就是一個處理邏輯,接收輸入?yún)?shù),執(zhí)行一定的邏輯,然后輸出參數(shù)。有三種類型的 Action:

  • Docker container, 適用 Linux 系統(tǒng)

通過 Docker 容器,提供 Action 的執(zhí)行邏輯處理。比如下面這個例子:

Dockerfile

  1. FROM appleboy/drone-scp:1.6.2-linux-amd64 
  2. ADD entrypoint.sh /entrypoint.sh 
  3. RUN chmod +x /entrypoint.sh 
  4. ENTRYPOINT ["/entrypoint.sh"

entrypoint.sh

  1. #!/bin/sh 
  2. set -eu 
  3. [ -n "$INPUT_STRIP_COMPONENTS" ] && export INPUT_STRIP_COMPONENTS=$((INPUT_STRIP_COMPONENTS + 0)) 
  4. sh -c "/bin/drone-scp $*" 

通過 dron-scp 鏡像,快速開發(fā)了一個提供 scp 文件拷貝的 Action。

  • JavaScript, 適用 Linux、macOS、Windows 系統(tǒng)

通過執(zhí)行 JavaScript 處理 Action 邏輯。官方提供了 JavaScript 和 TypeScript 的 Action 模板。在創(chuàng)建項目時,使用模板創(chuàng)建,然后編寫處理邏輯,發(fā)布自己的 Action 即可。

GitHub Actions 提供了工具包,以支持這種方式的擴展,例如執(zhí)行命令、操作 GitHub 等,都可以通過引用包,直接調用相關函數(shù)實現(xiàn)。下面是其中幾個工具包:

  1. @actions/exec, 執(zhí)行命令 
  2. @actions/core, 輸入、輸出、日志、秘鑰相關 
  3. @actions/io, 操作文件 
  • Composite run steps, 適用 Linux, macOS, Windows 系統(tǒng)

這種類型,允許將一連串的 Shell 操作作為一個 Action 使用。

  1. name'Hello World' 
  2. description: 'Greet someone' 
  3. inputs: 
  4.   who-to-greet:  # id of input 
  5.     description: 'Who to greet' 
  6.     required: true 
  7.     default'World' 
  8. outputs: 
  9.   random-number: 
  10.     description: "Random number" 
  11.     value: ${{ steps.random-number-generator.outputs.random-id }} 
  12. runs: 
  13.   using: "composite" 
  14.   steps: 
  15.     - run: echo Hello ${{ inputs.who-to-greet }}. 
  16.       shell: bash 
  17.     - id: random-number-generator 
  18.       run: echo "::set-output name=random-id::$(echo $RANDOM)" 
  19.       shell: bash 
  20.     - run: ${{ github.action_path }}/goodbye.sh 
  21.       shell: bash 

11. 參考

?https://github.com/actions/typescript-action

?https://github.com/shaowenchen/debugger-action

 

責任編輯:武曉燕 來源: 問其
相關推薦

2021-01-18 18:30:49

服務器開發(fā)工具

2021-01-19 05:26:22

Github ActiJenkinsDevOps

2020-06-04 15:55:54

GitHub代碼開發(fā)者

2021-01-05 05:15:02

Github 前端倉庫

2022-05-27 08:55:15

工具自動化軟件

2014-03-04 09:35:45

JavaScript調試

2017-01-16 15:12:36

Linuxwatch命令命令

2023-09-05 08:00:00

開源GreptimeDB

2022-12-21 08:20:01

2015-10-20 10:10:51

隱藏功能Windows 10微軟

2020-10-29 10:26:28

DevOps軟件自動化

2020-05-26 08:38:57

JavaScript語言

2017-05-18 09:16:54

前端CSS技巧

2020-11-29 17:32:01

EmacsLinux

2020-04-08 17:10:03

GitHub代碼開源

2017-01-09 16:40:07

React NatiAndroid 開發(fā)

2015-03-19 11:15:16

云備份云存儲

2018-04-18 07:21:29

2011-04-28 16:55:07

電子商務網(wǎng)站設計網(wǎng)站

2018-10-23 10:15:03

MySQL數(shù)據(jù)庫技巧
點贊
收藏

51CTO技術棧公眾號