Use `success()` value as parameter of action or script?

I can control jobs with if: ${{ success() }}.
Is there also a way to use the value of success() as a parameter for an action or script?
I would love to have a “report to slack” job that can both report success and failure.

Best
Jan

1 Like

Couldn’t you just:

if: failure()
  #send failure msg to slack
if: success()
  #send success msg to slack

At the end of the job?

@janpio ,

You can try like as this:

jobs:
  job1:
    . . .

  job2:
    . . .

  job3:
    name: report to slack
    needs: [job1, job2]
    if: ${{ always() }}
    runs-on: ubuntu-latest
    steps:
      - name: Get job status
        run: |
          echo "job1 status: ${{ needs.job1.result }}"
          echo "job2 status: ${{ needs.job2.result }}"

In this way, job3 will always run after job1 and job2 completed, then you can use the expression (${{ needs.<job id>.result }}) to get the statuses of job1 and job2.

Related docs:

Thanks, that is what we are doing right now. As one version of it takes 25 lines, that twice makes ~50 lines which I was hoping to be able to reduce: https://github.com/prisma/e2e-tests/blob/fb87468598d36759f83179dc4a013b23c8aa08a2/.github/workflows/test.yaml#L6-L62

1 Like

Thanks @brightran, that is closer to what I am looking for. Can I somehow calculate one value out of ${{ needs.job1.result }} and ${{ needs.job2.result }} that I can give to my reporting script?

That variation unfortunately also introduces another location where I need to remember to add job1 and job2 (… job27) which I really want to avoid - we forget that most of the time just for the Slack job already :frowning:

1 Like

Ya I think that can be simplified (de-duplicated), by moving the conditional check into the report to slack step:

report-to-slack:
    runs-on: ubuntu-latest
    needs:
      - os
      - node
      - packagers
      - frameworks
      - platforms
      - bundlers
      - libraries
      - binaries
      - databases
    if: (github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/patch-dev' || github.ref == 'refs/heads/latest')
    steps:
      - uses: actions/checkout@v1

      - name: Install Dependencies
        run: yarn install

      - uses: actions/setup-node@v1
        with:
          node-version: 12

      - name: report to slack
        env:
          CI: 1
          SLACK_WEBHOOK_URL_WORKFLOWS: ${{ secrets.SLACK_WEBHOOK_URL_WORKFLOWS }}
        if: failure()
          run: bash .github/scripts/slack-workflow-status.sh ":x:"
        if: success()
          run: bash .github/scripts/slack-workflow-status.sh ":white_check_mark:"

Spacing might not be perfect

1 Like

Ohhh, we’ll try that - thanks!

@kingthorin, @janpio,

When use the status check functions in a step’s if conditional in a job, it can only check the status of the previous steps in the same job, and won’t check the status of the previous jobs, even if the job needs the previous jobs.

You can print the needs context of a job, and check if it contains job status which is failure or cancelled.
For example, there are 3 jobs (job1, job2 and job3) in the workflow, job3 needs job1 and job2.

jobs:
  job1:
    . . .
  job2:
    . . .
  job3:
    needs: [job1, job2]
    if: ${{ always() }}
    runs-on: ubuntu-latest
    steps:
      - name: Print needs context
        run: |
          echo "$NEEDS_CONTEXT"
        env:
          NEEDS_CONTEXT: ${{ toJson(needs) }}

      - name: Summarize jobs status
        run: |
          result_failure="\"result\": \"failure\""
          result_cancelled="\"result\": \"cancelled\""
          
          if [[ $NEEDS_CONTEXT == *$result_failure* || $NEEDS_CONTEXT == *$result_cancelled* ]]; then
            echo "Not all jobs are success."
          else
            echo "All jobs are success."
          fi
        env:
          NEEDS_CONTEXT: ${{ toJson(needs) }}

In this example, the value of the needs context (NEEDS_CONTEXT) likes as this:

{
  "job1": {
    "result": "success",
    "outputs": {}
  },
  "job2": {
    "result": "failure",
    "outputs": {}
  }
}

Thanks @brightran, learn something every day :grinning:

@janpio ,

How are things going?
Is the suggestion in my previous reply helpful to you?
Please reference to my above example workflow to have a try on your side. If you have any update, feel free to tell us.