docs icon indicating copy to clipboard operation
docs copied to clipboard

Explain how to unset failure code in composite actions

Open jsoref opened this issue 1 year ago • 5 comments

Code of Conduct

What article on docs.github.com is affected?

https://docs.github.com/en/actions/creating-actions/setting-exit-codes-for-actions

What part(s) of the article would you like to see updated?

In this article About exit codes Setting a failure exit code in a JavaScript action Setting a failure exit code in a Docker container action

Add a section about composite actions -- specifically how to set a 0 exit code for cases where an intermediate step (actions/download-artifact being my canonical example) fail

Additional information

Here's a sample run: https://github.com/check-spelling-sandbox/composite-failures-0/actions/runs/7717700123/job/21037354247

workflow:

name: CI

on:
  push:
  pull_request:
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Run action
        uses: ./

action

name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash
    - run: echo "${{ github.action_path }}" >> $GITHUB_PATH
      shell: bash
    - run: goodbye.sh
      shell: bash
image

https://github.com/check-spelling-sandbox/composite-failures-0/actions/runs/7717700123/job/21037354247 image

jsoref avatar Jan 30 '24 21:01 jsoref

@jsoref Thanks for opening this issue! I'll get this triaged for review ✨

nguyenalex836 avatar Jan 31 '24 16:01 nguyenalex836

I know that you cannot unset an failure code, however composite action support continue-on-error for all run + uses steps (has been added way after uses support)

Then you can query the outcome yourself

SRC

https://docs.github.com/en/actions/learn-github-actions/contexts#steps-context

name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash
    - run: echo "${{ github.action_path }}" >> $GITHUB_PATH
      shell: bash
    - run: goodbye.sh
      shell: bash
      continue-on-error: true
      id: may-fail
    - run: echo ${{ steps.may-fail.outcome }}
      shell: bash
Prepare all required actions
##[group]Run ./
with:
  who-to-greet: World
##[endgroup]
##[group]Run echo Hello World.
echo Hello World.
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
##[endgroup]
Hello World.
##[group]Run echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
##[endgroup]
##[group]Run echo "/home/ubuntu/actions-runner-v2.283.4/_work/composite-action-with-continue-on-error/composite-action-with-continue-on-error/./" >> $GITHUB_PATH
echo "/home/ubuntu/actions-runner-v2.283.4/_work/composite-action-with-continue-on-error/composite-action-with-continue-on-error/./" >> $GITHUB_PATH
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
##[endgroup]
##[group]Run goodbye.sh
goodbye.sh
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
##[endgroup]
/home/ubuntu/actions-runner-v2.283.4/_work/_temp/31e0fff1-f95b-41a1-be92-bf1be82162fd.sh: line 1: goodbye.sh: command not found
##[error]Process completed with exit code 127.
##[group]Run echo failure
echo failure
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
##[endgroup]
failure

This step has conclusion success unless you decide to make another step let it fail

For me it looks like that composite action have almost no good documentation

ChristopherHX avatar Feb 17 '24 11:02 ChristopherHX

Thanks for calling this out @jsoref!

I agree that it makes sense to add an example for how to set exit codes in composite actions to the end of this article. You or anyone is welcome to open a pull request to make that change!

SiaraMist avatar Mar 05 '24 22:03 SiaraMist

Ok, here's the best I've managed: https://github.com/check-spelling-sandbox/composite-failures-0/actions/runs/8173963274

image

https://github.com/check-spelling-sandbox/composite-failures-0/actions/runs/8173963274/job/22347615235

Prepare all required actions
Run ./
Run echo Hello World.
Hello World.
Run exit $(( $RANDOM % 2 ))
Error: Process completed with exit code 1.
Run echo step maybe-die outcome: $out
step maybe-die outcome: failure
Run echo goodbye
goodbye

Prepare all required actions
Run ./
Run echo Hello World.
Hello World.
Run exit $(( $RANDOM % 2 ))
Error: Process completed with exit code 1.
Run echo step maybe-die outcome: $out
step maybe-die outcome: failure
Run echo goodbye
goodbye

Prepare all required actions
Run ./
Run echo Hello World.
Hello World.
Run exit $(( $RANDOM % 2 ))
Run echo step maybe-die outcome: $out
step maybe-die outcome: success
Run echo goodbye
goodbye

Prepare all required actions
Run ./
Run echo Hello World.
Hello World.
Run exit $(( $RANDOM % 2 ))
Run echo step maybe-die outcome: $out
step maybe-die outcome: success
Run echo goodbye
goodbye


workflow:

name: CI

on:
  push:
  pull_request:
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Debug action
        run: grep -n . ./action.yml

      - name: Run action
        uses: ./
      - name: Run action
        uses: ./
      - name: Run action
        uses: ./
      - name: Run action
        uses: ./
      - name: Run action
        uses: ./
      - name: Run action
        uses: ./

action

name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: maybe-die
      run: exit $(( $RANDOM % 2 ))
      shell: bash
      continue-on-error: true
    - run: echo true
      if: failure()
      shell: bash
    - if: failure() || success()
      shell: bash
      run: |
        echo step maybe-die outcome: $out
      env:
        out: ${{ steps.maybe-die.outcome }}
    - run: echo goodbye
      shell: bash

Note that as an action author, I do not want my random subprocesses' exit codes to appear in Annotations:

Annotations 3 errors build Process completed with exit code 1. build Process completed with exit code 1. build Process completed with exit code 1.

And I have no idea how to suppress that.

Or rather, I'm 99% confident that I just plain can't, which sucks: https://github.com/actions/runner/blob/2c8c9416224ad5e9926626d1360311568eefefd1/src/Runner.Worker/Handlers/ScriptHandler.cs#L409

I think an extra flag for steps to suppress this output would go a long way. My action consumers would definitely appreciate that.

jsoref avatar Mar 06 '24 14:03 jsoref

Here's the version of things I'm going to use as the basis of my documentation https://github.com/check-spelling-sandbox/composite-failures-0/actions/runs/8174941452

jsoref avatar Mar 06 '24 15:03 jsoref