使用 GitHub Actions 也有近兩年了,然後今天發現了兩個警告信息;
Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: actions/checkout@v2. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.
↑ 這個好解決,actions/checkout@v2
更新為actions/checkout@v3
就好;
The
set-output
command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
↑ 這個感覺需要研究一下,所以在此記錄下,雖然實際發現只是照新的寫法改一句就好……
工作流配置中的主要步驟如下,完整的配置文件見文末鏈接:
steps:
# Checkout
- name: Checkout
uses: actions/checkout@v3
# Build Release
- name: Copy && Gen ZIP
env:
PUB_NAME: ${{ env.PUB_NAME }}
run: |
# 這裡用來生成 ${PUB_NAME}.tar.gz 文件
# Get Tag For Release
- name: Get Tag
id: get_tag
env:
REF: ${{ github.ref }} # e.g. refs/tags/v1.0.0
run: |
TAG=${REF/refs\/tags\/v}
echo "::set-output name=tag::${TAG}"
# Publish Release
- name: Publish Release
id: release
uses: softprops/action-gh-release@v1
env:
TAG: ${{ steps.get_tag.outputs.tag }} # 獲取上一步截取到的版本號,既 1.0.0
with:
name: ${{ env.PUB_NAME }} Build ${{ env.TAG }}
body: ${{ env.PUB_NAME }} Build ${{ env.TAG }} Release.
files: ${{ env.PUB_NAME }}.tar.gz
功能概述為,在每次打 Tag 的時候,自動構建並發布 Release,同時提取 Tag 作為 Release 的版本號;
所以其中用於提取 Tag 的步驟為:
# Get Tag For Release
- name: Get Tag
id: get_tag
env:
REF: ${{ github.ref }} # e.g. refs/tags/v1.0.0
run: |
TAG=${REF/refs\/tags\/v}
echo "tag=${TAG}" >> $GITHUB_OUTPUT
其中TAG=${REF/refs\/tags\/v}
將變量REF
中指定部分替換為空,然後將結果賦值給TAG
,${TAG}
內容既為1.0.0
;
只是這個變量只在當前run
區塊內有效,需要將其導出以便後續步驟使用;
舊的導出命令如上,只是這種寫法被廢棄了……
↓ 新的寫法:
# Get Tag For Release
- name: Get Tag
id: get_tag
env:
REF: ${{ github.ref }} # e.g. refs/tags/v1.0.0
run: |
TAG=${REF/refs\/tags\/v}
echo "tag=${TAG}" >> $GITHUB_OUTPUT
官方文檔:Setting an output parameter
中文文檔:設置輸出參數
完整的工作流配置文件:wdssmq/rollup-plugin-monkey/blob/main/.github/workflows/push_def.yml
GitHub Actions 運行結果(舊):wdssmq/rollup-plugin-monkey/actions/runs/4051596042