1

I had this step in a macOS lane:

jobs:
  macOS_build:
    runs-on: macOS-latest
    steps:
    - uses: actions/checkout@v1
    - name: Build in DEBUG and RELEASE mode
      run: ./configure.sh && make DEBUG && make RELEASE

Then I successfully split it up this way:

jobs:
  macOS_build:
    runs-on: macOS-latest
    steps:
    - name: Build in DEBUG and RELEASE mode
      run: |
        ./configure.sh
        make DEBUG
        make RELEASE

This conversion works because if make DEBUG fails, make RELEASE won't be executed and the whole step is marked as FAILED by GitHubActions.

However, trying to convert this from the Windows lane:

jobs:
  windows_build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v1
    - name: Build in DEBUG and RELEASE mode
      shell: cmd
      run: configure.bat && make.bat DEBUG && make.bat RELEASE

To this:

jobs:
  windows_build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v1
    - name: Build in DEBUG and RELEASE mode
      shell: cmd
      run: |
        configure.bat
        make.bat DEBUG
        make.bat RELEASE

Doesn't work, because strangely enough, only the first line is executed. So I tried trying to change the shell attribute to powershell:

jobs:
  windows_build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v1
    - name: Build in DEBUG and RELEASE mode
      shell: powershell
      run: |
        configure.bat
        make.bat DEBUG
        make.bat RELEASE

However this fails with:

configure.bat : The term 'configure.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Then I saw this other SO answer, so I converted it to:

jobs:
  windows_build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v1
    - name: Build in DEBUG and RELEASE mode
      shell: powershell
      run: |
        & .\configure.bat
        & .\make.bat DEBUG
        & .\make.bat RELEASE

This finally launches all batch files independently, however it seems to ignore the exit code (so if configure.bat fails, it still runs the next lines).

Any idea how to separate lines in a GithubActions workflow properly?

1 Answer 1

1

In PowerShell, you'll have to check the automatic $LASTEXITCODE variable after each call if you want to take action on the (nonzero) exit code of the most recently executed external program or script:

if ($LASTEXITCODE) { exit $LASTEXITCODE }

If you want to keep the code small, you could check for intermediate success vs. failure via the automatic $? variable, which is a Boolean that contains $true if the most recent command or expression succeeded, which in the case of external programs is inferred if the exit code is 0:

.\configure.bat
if ($?) { .\make.bat DEBUG }
if ($?) { .\make.bat RELEASE }
exit $LASTEXITCODE

Note that if you were to use PowerShell (Core) 7+, you could use the bash-like approach, since && and ||, the pipeline-chain operators, are now supported - as long as you end each statement-internal line with &&, you can place each call on its own line:

# PSv7+
.\configure.bat && 
.\make.bat DEBUG && 
.\make.bat RELEASE

However, note that any nonzero exit code is mapped onto 1 when the PowerShell CLI is called via -Command, which is what I presume happens behind the scenes, and assuming that an external program is called last. That is, the specific nonzero exit code is lost. If it is of interest, append an exit $LASTEXITCODE line to the above.

Sign up to request clarification or add additional context in comments.

3 Comments

your PSv7+ suggestion would leave to square-one, because the initial motivation is to split the line, not leaving it long as you have put it
@knocte, if this is about readability, you should be able to spread the statement across multiple lines - please seem my update, which now also shows a more concise WinPS solution.
well it's about readability but without losing functionality (hence my problem with the exit code); thanks, will test your solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.