在 GitHub Actions 設定動態環境變數
在 GitHub Actions 中,將值寫入 GITHUB_ENV 檔案後,同一 job 的後續步驟即可讀取。寫入值的步驟本身無法讀取新值。以下 Windows PowerShell 範例透過手動觸發建立 GitHub Release,並假設先前的建置步驟已將執行檔放入 dist。
在 Windows runner 建立 GitHub Release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| name: Create Release
on:
workflow_dispatch:
inputs:
version:
description: Release tag, for example v1.2.3
required: true
type: string
permissions:
contents: write
jobs:
release:
runs-on: windows-latest
steps:
- uses: actions/checkout@v7
- name: Set release variables
shell: pwsh
env:
RELEASE_VERSION_INPUT: ${{ inputs.version }}
run: |
if ($env:RELEASE_VERSION_INPUT -notmatch '^v[0-9]+(\.[0-9]+){2}$') { throw 'Enter a tag such as v1.2.3' }
"RELEASE_VERSION=$env:RELEASE_VERSION_INPUT" >> $env:GITHUB_ENV
"ASSET_NAME=sample-tool" >> $env:GITHUB_ENV
- name: Check the built asset
shell: pwsh
run: |
if (-not (Test-Path ".\dist\$env:ASSET_NAME.exe")) { throw 'Build the executable in dist before creating the release' }
- name: Create release and upload asset
shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
run: gh release create "$env:RELEASE_VERSION" ".\dist\$env:ASSET_NAME.exe" --target "$env:GITHUB_SHA" --title "$env:RELEASE_VERSION" --generate-notes
|
GITHUB_ENV 的適用範圍
使用 PowerShell 7(pwsh)時,將 NAME=value 附加到 $env:GITHUB_ENV。Linux Bash 則使用 printf 'NAME=value\n' >> "$GITHUB_ENV"。GITHUB_ENV 不會在 job 之間傳值;跨 job 請使用步驟與 job 輸出。參閱工作流程命令及job 輸出。
Official references: workflow contexts, job outputs, gh release create, GitHub CLI authentication and GH_TOKEN, checkout releases, and the archived create-release action.