usan
usan

Reputation: 135

how to check failure of a command in github action

I integrate STM32CubeProgrammer with github action for deploy.

Although the command exit with error, github action workflow did not fail so that the result is success.

Because the command print "Error" at console log, I just want to grep this and make the workflow fail. how can I do that?

I am using self-hosted runner(Windows 10)

jobs:
  build:...
    
  deployment:
    # The type of runner that the job will run on
    runs-on: [ self-hosted, deploy ]
    needs: build
    environment: production

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Runs a set of commands using the runners shell
      - name: deploy
        run: |
          STM32_Programmer_CLI.exe -c port=SWD -w ${{ secrets.ELF_FILE }} ${{ secrets.START_ADDR }}
      
      - name: start
        run: |
          STM32_Programmer_CLI.exe -c port=SWD -c port=SWD -s ${{ secrets.START_ADDR }}

Upvotes: 3

Views: 1821

Answers (1)

usan
usan

Reputation: 135

I found out that the CLI program prints 'File download complete' to standard output when it runs without errors.

So, I decided to determine whether my script failed or not by checking if the string 'File download complete' is present in the standard output log.

I use a Windows self-hosted runner with PowerShell.

There is a -match operator in PowerShell. If you're not familiar with it, please check this link: https://learn.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_comparison_operators.

I execute the CLI program and save the standard output to the deploy_result variable, as shown in the code below:

$deploy_result = $(STM32_Programmer_CLI.exe -c port=SWD -w ${{ env.ELF_FILE }} ${{ env.START_ADDR }})

Then, I check whether the string "File download complete" is present. If it is not found, the script exits with a failure status:

if ( $deploy_result -match "File download complete" ) {} else { exit 1 }

This is my full action yaml file:

jobs:
  build:...
    
  deployment:
    # The type of runner that the job will run on
    runs-on: [ self-hosted, deploy ]
    needs: build
    environment: production
    outputs:
      result: ${{ steps.deploy.outputs.DEPLOY_RESULT }}

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: deploy
        # based on Windows Powershell batch script language
        run: |
          $deploy_result = $(STM32_Programmer_CLI.exe -c port=SWD -w ${{ env.ELF_FILE }} ${{ env.START_ADDR }})
          echo $deploy_result
          if ( $deploy_result -match "File download complete" ) {} else { exit 1 }

Upvotes: 0

Related Questions