Featured image of post Cómo configurar dinámicamente variables de entorno en GitHub Actions 【Entorno Windows】

Cómo configurar dinámicamente variables de entorno en GitHub Actions 【Entorno Windows】

Pasa valores a pasos posteriores de GitHub Actions con GITHUB_ENV y crea una versión desde PowerShell en Windows.

Definir variables de entorno dinámicas en GitHub Actions

En GitHub Actions, escribe un valor en el archivo GITHUB_ENV para que esté disponible en los pasos posteriores del mismo trabajo. El paso que lo escribe no puede leer el nuevo valor. Este ejemplo de PowerShell para Windows crea una versión de GitHub al ejecutarse manualmente. Supone que una compilación anterior dejó el ejecutable en dist.

Ejemplo: crear una versión en un 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

Alcance de GITHUB_ENV

Con PowerShell 7 (pwsh), añade NAME=value a $env:GITHUB_ENV. En Linux con Bash, usa printf 'NAME=value\n' >> "$GITHUB_ENV". GITHUB_ENV no comparte valores entre trabajos; para ello usa salidas de pasos y trabajos. Consulta los comandos de flujo y las salidas de trabajos.

Official references: workflow contexts, job outputs, gh release create, GitHub CLI authentication and GH_TOKEN, checkout releases, and the archived create-release action.

comments powered by Disqus