Search code examples
github-actions

Github actions: fail and step output


Is there any way to "fail-fast" and get commands like echo "::error::Failure!" to be shown in the build summary or execution details?

Consider this example:

      - name: Fetch repo
        id: 'checkout'
        uses: actions/checkout@v3

      - name: Get tags
        run: git fetch --tags origin

      - name: Check if provided version number has been released
        run: |
          release_tag_exists=$(git tag | grep ${{ inputs.version }})
          
          if [ -z "$release_tag_exists" ]; then
            echo "::error::Failing, reason: could not find release in git, version specified was: ${{ inputs.version }}"
            exit 1
          else
            echo "::notice::Found version ${{ inputs.version }} in git, all good"
          fi
        shell: bash
 

The output is:

  release_tag_exists=$(git tag | grep jkhkjh)
  
  if [ -z "$release_tag_exists" ]; then
    echo "::error::Failing, reason: could not find release in git, version specified was: jkhkjh"
    exit 1
  else
    echo "::notice::Found version jkhkjh in git, all good"
  fi
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
Error: Process completed with exit code 1.

So the runner just barfs up a "Process completed with exit code 1", omitting the github commands issued inside the bash script. Is there any prettier solution here than capturing the output from the first step and moving "exit 1" to a second step?


Solution

  • Just to clarify: echo "::error" works as expected, so the question is wrong, but I am keeping it for further reference.

    The problem was the same as discussed here: Github Actions: Why an intermediate command failure in shell script would cause the whole step to fail?

    Which means that the intermediate command "grep" would fail if it found no match. The solution was to disable the "errexit" option -e (https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html)

    Working version:

          - name: Check if provided version number has been released
            id: check_version
            run: |
              set +e
              release_tag_exists=$(git tag | grep ${{ inputs.version }})
              
              if [ -z "$release_tag_exists" ]; then
                echo "::error::Failing, reason: could not find release in git, version specified was: ${{ inputs.version }}"
                exit 1
              else
                echo "::notice::Found version ${{ inputs.version }} in git, all good"
              fi
    
              echo "::set-output name=release_tag_exists::$release_tag_exists"
            shell: bash