Search code examples
powershellgithubyamlgithub-actions

How to save command result in GitHub


I want to be able to save the result from any command I run so that I can decide what I want to do next in my YAML file

Here is an example of a non working example of something similar to what I want

- name: Run script
        shell: powershell
        run: |
          status = script\\outputZero.ps1
          if: status == 0
              echo "output was 0" 

I also tried doing this

- name: Run script
        shell: powershell
        run: |
          if: script\\outputZero.ps1
              echo "output was 0" 

but it gave me the error The term 'if:' is not recognized as the name of a cmdlet, function, script file, or operable program.


Solution

  • Examples from Building and testing PowerShell rather use shell: pwsh (might be a synonym for powershell)

    See for instance:

      lint-with-PSScriptAnalyzer:
        name: Install and run PSScriptAnalyzer
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: Install PSScriptAnalyzer module
            shell: pwsh
            run: |
              Set-PSRepository PSGallery -InstallationPolicy Trusted
              Install-Module PSScriptAnalyzer -ErrorAction Stop
          - name: Lint with PSScriptAnalyzer
            shell: pwsh
            run: |
              Invoke-ScriptAnalyzer -Path *.ps1 -Recurse -Outvariable issues
              $errors   = $issues.Where({$_.Severity -eq 'Error'})
              $warnings = $issues.Where({$_.Severity -eq 'Warning'})
              if ($errors) {
                  Write-Error "There were $($errors.Count) errors and $($warnings.Count) warnings total." -ErrorAction Stop
              } else {
                  Write-Output "There were $($errors.Count) errors and $($warnings.Count) warnings total."
              }
    

    Note the lack of : after if.