Definir variáveis de ambiente dinâmicas no GitHub Actions
No GitHub Actions, grave um valor no arquivo GITHUB_ENV para disponibilizá-lo às etapas seguintes do mesmo job. A etapa que grava o valor não consegue ler o novo valor. Este exemplo de PowerShell para Windows cria uma release do GitHub quando executado manualmente. Ele pressupõe que uma compilação anterior colocou o executável em dist.
Exemplo: criar uma release em um runner Windows
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
|
Escopo de GITHUB_ENV
Com PowerShell 7 (pwsh), acrescente NAME=value a $env:GITHUB_ENV. No Linux com Bash, use printf 'NAME=value\n' >> "$GITHUB_ENV". GITHUB_ENV não compartilha valores entre jobs; use outputs de etapas e jobs para isso. Consulte os comandos de workflow e os outputs de jobs.
Official references: workflow contexts, job outputs, gh release create, GitHub CLI authentication and GH_TOKEN, checkout releases, and the archived create-release action.