偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

10 個(gè)你該了解的 GitHub Actions 進(jìn)階技巧

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

[[379607]]

 本文轉(zhuǎn)載自微信公眾號(hào)「問其」,作者陳少文。轉(zhuǎn)載本文請(qǐng)聯(lián)系問其公眾號(hào)。

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

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

  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í)行時(shí),會(huì)彈出如下對(duì)話框。

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

一個(gè) workflow 由很多個(gè) 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í)行時(shí),job2 和 job3 會(huì)等 job1 執(zhí)行成功時(shí)才執(zhí)行,job4 會(huì)等 job2 和 job3 執(zhí)行成功時(shí)才執(zhí)行。

3. 用于項(xiàng)目管理

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

下面是幾個(gè)項(xiàng)目管理相關(guān)的 action

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

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

  1. docs_label: 
  2.   - ./docs/* 
  • 根據(jù)標(biāo)簽添加 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' 

也可以將包含指定標(biāo)簽的 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' 
  • 清理長時(shí)間無人跟進(jìn)的 Issues

如果一個(gè) Issue 長達(dá) 30 天沒有更新,那么下面的 workflow 將會(huì)再等 5 天,然后將其關(guān)閉。

  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 上的項(xiàng)目管理,主要是圍繞 Issues、Projects、Labels、Pull Requests 展開,可以在 GitHub Actions 的 Marketplace 中搜索相關(guān)的 Action 使用。

4. 在線調(diào)試

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

  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)申請(qǐng)一個(gè) token,就可以通過 ssh 遠(yuǎn)程登錄到 Runner。當(dāng)然,也可以暴露 Runner 上的服務(wù),提供外網(wǎng)訪問的鏈接,最長可達(dá) 6 小時(shí)。

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

5. 設(shè)置緩存

緩存能有效地加快構(gòu)建速度,減少網(wǎng)絡(luò)請(qǐng)求,復(fù)用中間碼。這對(duì)于 Java、Nodejs、Python 等項(xiàng)目,非常有用。

  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. 檢測(cè)項(xiàng)目中的問題鏈接

項(xiàng)目維護(hù)時(shí)間長了之后,最令人頭疼的就是文檔。研發(fā)、測(cè)試跟進(jìn)的是代碼、功能,而文檔卻時(shí)常無人更新。缺少維護(hù)的文檔,會(huì)讓潛在參與者流失。下面這個(gè) Action 能檢測(cè)文檔中的 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。

下面是一個(gè) .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 日志頁面,會(huì)輸出這樣的檢測(cè)結(jié)果:

  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í)行任務(wù)

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

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

  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í)行時(shí), 會(huì)執(zhí)行 6 個(gè) job。

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

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

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

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

在 GitHub Actions 頁面中, 點(diǎn)擊 Create status badge。

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

9. 精準(zhǔn) hook GitHub 上的行為

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

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

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

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

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

當(dāng)有人 fork 倉庫時(shí)觸發(fā)

  1. on
  2.   fork 

當(dāng)有人 star 倉庫時(shí)觸發(fā)

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

當(dāng)有新建的 Issue 時(shí)觸發(fā)

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

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

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

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

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

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

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ā)了一個(gè)提供 scp 文件拷貝的 Action。

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

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

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

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

這種類型,允許將一連串的 Shell 操作作為一個(gè) 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

 

責(zé)任編輯:武曉燕 來源: 問其
相關(guān)推薦

2021-01-18 18:30:49

服務(wù)器開發(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

工具自動(dòng)化軟件

2014-03-04 09:35:45

JavaScript調(diào)試

2017-01-16 15:12:36

Linuxwatch命令命令

2015-10-20 10:10:51

隱藏功能Windows 10微軟

2020-10-29 10:26:28

DevOps軟件自動(dòng)化

2020-05-26 08:38:57

JavaScript語言

2023-09-05 08:00:00

開源GreptimeDB

2022-12-21 08:20:01

2020-11-29 17:32:01

EmacsLinux

2017-05-18 09:16:54

前端CSS技巧

2020-04-08 17:10:03

GitHub代碼開源

2017-01-09 16:40:07

React NatiAndroid 開發(fā)

2015-03-19 11:15:16

云備份云存儲(chǔ)

2011-04-28 16:55:07

電子商務(wù)網(wǎng)站設(shè)計(jì)網(wǎng)站

2018-04-18 07:21:29

2009-04-14 21:38:05

LinuxUbuntu技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)