iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0

目錄

摘要

在上一篇我們學到如何設定各種自動、手動、排程事件觸發workflow

在這篇我們會來了解一下各種workflow command

workflow command是什麼

workflow command 是 GitHub Actions 提供的用於與runner溝通的特殊指令,透過它們可以設置變數、output、隱藏敏感資訊

印出註解(訊息)

Github Actions提供一些可以印出註解(訊息)的command,方便debug

不過這些command只會印出訊息,並沒有離開(exit)step的功能,所以說用了::error並不會使得step失敗

https://ithelp.ithome.com.tw/upload/images/20240813/20135568z9at5iD5Dh.png
https://ithelp.ithome.com.tw/upload/images/20240813/201355689eqwPo29VJ.png

# 雖然官方標示title是required,但其實不傳也可以
run: |
    echo "::debug::Set the Octocat variable"
    echo "::warning file=app.js,line=1,col=5,endColumn=7::Missing semicolon"
    echo "::error file=app.js,line=1,col=5,endColumn=7::Missing semicolon"

不過需要注意workflow內印出的註解有上限數量超過了就會被顯示

  • 每個step最多10個warning、10個error
  • 每個job最多50個註解(debug、warning、error、notice合計)
  • 每個workflow最多50個註解(debug、warning、error、notice合計)

如果你的註解很長的是話也可以試著拆成幾行,然後group起來

run: |
    echo "::group::Report Error"
    echo "Inside group"
    echo "::endgroup::"

為敏感資訊設置mask

Day8曾經提到過Github Actions會自動幫secret上mask,事實上如果需要的話我們也可以自行為變數設置mask,但不建議手動上mask

如下面的例子,我們會發現在log中去看設置mask的那一個step你就會發現還是被印出來了...

https://ithelp.ithome.com.tw/upload/images/20240815/20135568ifw0n7KbKq.png
https://ithelp.ithome.com.tw/upload/images/20240815/201355682HREF7APxe.png

jobs:
  start-to-debug:
    env:
      idNumber: A123456789
    runs-on: ubuntu-latest
    steps:
      - name: Give a try
        run: |
          echo "::add-mask::$idNumber"

      - name: add mask to string
        run: echo "::add-mask::some thing I want to hide"

讀寫環境變數

這邊的環境變數指的不是你的repo中的.env檔中的環境變數,沒辦法使用workflow command直接對它進行操作

當workflow在執行時,runner會產生暫時的檔案,環境變數就會被放在這些檔案中,以利workflow執行,等workflow執行完,就會把這些檔案丟掉

https://ithelp.ithome.com.tw/upload/images/20240815/201355689JhTI2Ndye.png

run: |
  echo "idNumber=A123456" >> $GITHUB_ENV

你可能在別的文章中看過有人使用::set-env

那是以前的寫法,但是已經在2020年時因為可能導致安全風險而被廢棄

多行字串

如果你要將一個多行字串寫入GITHUB_ENV,那需要使用多行字串語法

EOF是分隔符,End Of Line的意思

事實上不一定要使用EOF,它是一個convention,只要確保使用的分隔符不會出現在要寫入的字串中,那麼用什麼字串當分隔符都可以

run: |
    {
      echo 'loremString<<EOF'
      echo Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      echo EOF
    } >> $GITHUB_ENV

job summary

雖然說只要查log就能看到印出的內容,但是當你的output有一些可讀性較低的內容時,這個功能就相當方便其他開發者查看

https://ithelp.ithome.com.tw/upload/images/20240815/20135568lZ6f0insuI.png

env:
  nestObject: '{"profile":{"name":"Tempura", "job":"front-end developer", "experience": 3}}'
steps:
  - name: Summary
    # 物件不能存在env中,所以這邊只能重複呼叫fromJSON
    run: |
      echo "This is the summary" >> $GITHUB_STEP_SUMMARY
      echo "" >> $GITHUB_STEP_SUMMARY # this is a blank line
      echo "- Name: ${{fromJSON(env.nestObject).profile.name}}" >> $GITHUB_STEP_SUMMARY
      echo "- Job: ${{fromJSON(env.nestObject).profile.job}}" >> $GITHUB_STEP_SUMMARY
      echo "- Experience: ${{fromJSON(env.nestObject).profile.experience}}" >> $GITHUB_STEP_SUMMARY

上一篇
Day 10 - 觸發workflow
下一篇
Day 12 - 常用的shell command
系列文
菜逼八用Github Actions29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言