Действия Github печатают пустую переменную среды при печати для проверки

Ниже приведен рабочий процесс GitHub, который я использую для создания проекта и получения SHA и тегов git commit:

name: proj-build

on: [workflow_call, workflow_dispatch]

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release
  CYGWIN_HOME: C:\cygwin64
  PERL_HOME: C:\strawberry-perl\perl
  CMAKE_BUILD_ARTIFACT_NAME: artifact-build
  COMMIT_SHA: ''
  TAG_NAME: ''

jobs:
  build-cmake:
    runs-on: [ self-hosted, Windows ]
    timeout-minutes: 500
    steps:
    # Authenticate for accessing repos in the organization
    - name: Setup git
      uses: myorg/common-actions/setup-git@master
      id: setup-git
      with:
        app-id: ${{ vars.IRA_APP_ID }}
        private-key: ${{ secrets.IRA_PRIVATE_KEY }}

    - name: Checkout proj repo
      uses: actions/checkout@v4
      with:
        token: ${{ steps.setup-git.outputs.token }}
        fetch-tags: 'true'
        fetch-depth: 0
        path: myproj

    - name: Get Commit SHA
      id: get-sha
      shell: pwsh
      run: |
        Write-Output "SHA=${{ github.sha }}"
        echo "COMMIT_SHA=${{ github.sha }}" >> $GITHUB_ENV

    - name: Get Tag
      id: get-tag
      shell: pwsh
      continue-on-error: true
      if: startsWith(github.ref, 'refs/tags/')
      run: |
        Write-Output "TAG=${{ github.ref }}"
        $tagName = "${{ github.ref }}" -replace 'refs/tags/', ''
        Write-Output "tagName=$tagName"
        echo "TAG_NAME=$tagName" >> $GITHUB_ENV

    - name: Handle untagged commits
      shell: pwsh
      if: env.TAG_NAME == ''
      run: echo "TAG_NAME=none" >> $GITHUB_ENV

    - name: Verify commit & tag info in the environment
      id: set-outputs
      shell: pwsh
      run: |
        Write-Output "COMMIT_SHA=${{ env.COMMIT_SHA }}"
        Write-Output "TAG_NAME=${{ env.TAG_NAME }}"

    - name: Listing contents of repo directory before CMake build
      shell: powershell
      run: |
        cd ${{ github.workspace }}\myproj\.github\workflows\
        . ./get-contents.ps1
        Write-Output "Getting the contents of the myproj repo directory before CMake build..."
        Get-Directory-Contents -Path ${{ github.workspace }}\myproj

    - name: Build myproj
      shell: cmd
      run: |
        set PATH=%PATH%;%CYGWIN_HOME%
        cd ${{ github.workspace }}\myproj
        cmake -S . -B build
        cmake --build build --target install

    - name: Listing contents of repo directory after CMake build
      shell: powershell
      run: |
        cd ${{ github.workspace }}\myproj\.github\workflows\
        . ./get-contents.ps1
        Write-Output "Getting the contents of the myproj repo directory after CMake build..."
        Get-Directory-Contents -Path ${{ github.workspace }}\myproj
    
    - name: Creating an info file for build directory
      shell: powershell
      run: |
        tree ${{ github.workspace }}\myproj\build | `
          Out-File -FilePath ${{ github.workspace }}\myproj\build\build.info -Append -Encoding utf8; `
          Get-ChildItem ${{ github.workspace }}\myproj\build -Recurse | `
          Measure-Object -Property Length -Sum | ForEach-Object { 'Total Size: {0} MB' -f ($_.Sum / 1MB) } | `
          Out-File -FilePath ${{ github.workspace }}\myproj\build\build.info -Append -Encoding utf8

    - name: Save environment variables to file
      shell: cmd
      run: |
        echo COMMIT_SHA=${{ env.COMMIT_SHA }}
        echo TAG_NAME=${{ env.TAG_NAME }}
        echo COMMIT_SHA=${{ env.COMMIT_SHA }} > ${{ github.workspace }}\myproj\build\environment.info
        echo TAG_NAME=${{ env.TAG_NAME }} >> ${{ github.workspace }}\myproj\build\environment.info
        echo %COMMIT_SHA%
        echo %TAG_NAME%

    - name: Upload Artifact
      if: always()
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.CMAKE_BUILD_ARTIFACT_NAME }}
        retention-days: 2
        path: |
            ${{ github.workspace }}\myproj\build

Я хотел бы использовать переменные среды, хранящиеся в файле .info, который будет загружен как артефакт для использования в последующих рабочих процессах. Однако шаги «Проверить информацию о фиксации и тегах в среде» и «Сохранить переменные среды в файл» дают пустые выходные данные, как

COMMIT_SHA=
TAG_NAME=

Можно ли это исправить? Что именно я делаю не так?


51
1

Ответ:

Решено

В Windows синтаксис $env:GITHUB_ENV вместо просто $GITHUB_ENV:

echo "COMMIT_SHA=${{ github.sha }}" >> $env:GITHUB_ENV

run: echo "TAG_NAME=none" >> $env:GITHUB_ENV