github-actionsgithub-pagesbadgegithub-flavored-markdown

GitHub Actions environment var not picked up


I implemented a code coverage badge that should be a dynamic one for https://github.com/CosmicDNA/ImportDotEnv/tree/development, where it is using a JSON file hosted at GH pages.

Here is the badge code in README.md:

[![Coverage](https://img.shields.io/endpoint?url=https://cosmicdna.github.io/ImportDotEnv/coverage.json)](https://cosmicdna.github.io/ImportDotEnv/)

The deployment to GH pages is done automatically with tagged pushes with the following workflow:

name: Release
env:
  PSGALLERY_API_KEY: ${{ secrets.POWER_SHELL_GALLERY_API_KEY }}
on:
  push:
    tags:
      - 'v*'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

  # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  test:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: windows-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install Pester
        run: |
          Install-Module -Name Pester -Force -Scope CurrentUser

      - name: Run Pester tests
        run: |
          # Run Pester tests with coverage
          $COVERAGE = & .\scripts\RunTests.ps1 -EnableCoverage
          # Update env var with the coverage information
          echo "COVERAGE=$COVERAGE" >> $GITHUB_ENV

      - name: Publish and Save Module
        run: |
          pwsh -Command "`$apiKey = `$env:PSGALLERY_API_KEY; Publish-Module -Path . -NuGetApiKey `$apiKey"

      - name: Setup .NET 9
        uses: actions/setup-dotnet@v2
        with:
          dotnet-version: '9.0.x'

      - name: Install ReportGenerator
        run: |
          $userTempFolder = [System.IO.Path]::GetTempPath()
          $guid = [guid]::NewGuid()
          $userTempDir="$userTempFolder$guid"
          certutil.exe -urlcache -split -f "https://github.com/danielpalme/ReportGenerator/releases/download/v5.4.4/ReportGenerator_5.4.4.zip" "${userTempDir}RG.zip"
          Expand-Archive "${userTempDir}RG.zip" -DestinationPath "${userTempDir}reportgenerator"
          Copy-Item -Path "${userTempDir}reportgenerator\net9.0" -Destination reportgenerator -Recurse

      - name: Generate code coverage report
        run: |
          ./reportgenerator/ReportGenerator.exe -reports:coverage.xml -targetdir:reports -reporttypes:'Latex;Html' -sourcedirs:.

      - name: Save coverage to JSON file
        run: |
          $json = @{"schemaVersion"=1; "label"="coverage"; "message"="$env:COVERAGE%"; "color"="green"} | ConvertTo-Json
          $json | Out-File -FilePath reports/coverage.json -Encoding utf8

      - name: Setup Pages
        uses: actions/configure-pages@v5
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: './reports'
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

Unfortunately, the JSON file is not picking up the correct COVERAGE from the RunTests.ps1 script. Locally I can see that it does, but I am not sure why on the server, the reference to "$env:COVERAGE%" on the Save coverage to JSON file step is not working. Any ideas of why that may be so?


Solution

  • The fix was to use:

    $json = @{"schemaVersion"=1; "label"="coverage"; "message"="${{ env.COVERAGE }}%"; "color"="green"} | ConvertTo-Json
    

    instead.

    For any reason, Github Actions does not pick up "$env:COVERAGE%". But it does with "${{ env.COVERAGE }}%" here.