Masmoudi
Masmoudi

Reputation: 163

Problem with Github Actions IF expression syntax

I need to check if steps.get_acc.outputs.acc < 1 but right now the steps.get_acc.outputs.acc is a string how to make it an integer?

cml.yaml

name: MOPS
on: [push]
jobs:
  run:
    runs-on: [ubuntu-latest]
    steps:
      - uses: actions/checkout@v2
      - uses: iterative/setup-cml@v1
      - uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      - name: cml
        id: get_acc
        shell: pwsh  
        env:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
        run: |
          pip3 install -r requirements.txt
          python train.py
          $firstLine = Get-Content -Path 'metrics.txt' -TotalCount 1
          $digits = $firstline.Split(':')[-1]
          $acc = [int]$digits
          Write-Output "::set-output name=acc::$acc"        
    
      - name: Check acc
        if: ${{steps.get_acc.outputs.acc <1 }}
        uses: actions/github-script@v3
        with:
         script: |
          core.setFailed('Accuracy dropped')

Upvotes: 2

Views: 6677

Answers (2)

Mark Phippard
Mark Phippard

Reputation: 10419

It looks like you have been given an answer on how to handle it as an integer, but given that you are setting the output via code. Why not just evaluate the integer in your code and then set a true/false boolean value or a Y/N string? If all you need to do is use the value as an If condition why bother to set the integer value? You could just evaluate it and set the condition at the source.

Upvotes: 1

GuiFalourd
GuiFalourd

Reputation: 22970

The Github Documentation for output explained that:

The value that the output parameter will be mapped to can be set to a string or an expression with context. For example, you can use the steps context to set the value of an output to the output value of a step.

Taking a look at the Literals Github documentation, we can note that:

As part of an expression, we can use boolean, null, number, or string data types. Boolean literals are not case sensitive, so we can use true or True.

enter image description here

Therefore, it all depends on the way you set the output variable.

Example to set a number as output:

outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
  using: "composite"
  steps:
    - id: random-number-generator
      run: echo "::set-output name=random-id::$(echo $RANDOM)"
      shell: bash

EDIT:

Moreover the syntax you used for the if condition isn't correct, you need to remove the ${{ }} to make it work.

Example with if condition:

  job:
    runs-on: ubuntu-latest
    steps:
      
      - name: step-1
        id: xyz
        run: echo "::set-output name=acc::$(echo $RANDOM)"
      
      - name: step-2
        if: steps.xyz.outputs.acc < 1
        run: |
          echo "Number lower than 1"
          echo "${{ steps.xyz.outputs.acc }}"
      
      - name: step-3
        if: steps.xyz.outputs.acc > 1
        run: |
          echo "Number higher than 1"
          echo "${{ steps.xyz.outputs.acc }}"

Upvotes: 3

Related Questions