Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put conditional job in need of another job in Github Action

Below is my GitHub workflow

name: APP Build
on:
  push:
    branches:
      - feature/test

jobs:
  test-1:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' && contains( github.event.head_commit.message, 'test1') }}
    steps:
      - name: test-fail
        run: echo "test1"
  test-2:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' && contains( github.event.head_commit.message, 'test2') }}
    steps:
      - name: test-fail
        run: echo "test1"
  notify-slack:
    name: Slack Notification
    runs-on: ubuntu-latest
    needs: [test-1, test-2]
    steps:
      - name: Slack Notification
        uses: rtCamp/[email protected]
        env:
          SLACK_CHANNEL: alert
          SLACK_COLOR: "${{ job.status == 'success' && 'good' || 'danger' }}"

Question: test1 and test2 job is conditional as per my commit message but notify slack need either test1 or test2. I can't put both because if either the job skip notify-slack will skip as well. How to make this work?

like image 926
Vikas Rathore Avatar asked Nov 28 '25 10:11

Vikas Rathore


2 Answers

There is an issue on Github about this. You need to add condition like below:

  test-1:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' && contains( github.event.head_commit.message, 'test1') }}
    steps:
      - name: test-fail
        run: echo "test1"
  test-2:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' && contains( github.event.head_commit.message, 'test2') }}
    steps:
      - name: test-fail
        run: echo "test1"
  notify-slack:
    name: Slack Notification
    runs-on: ubuntu-latest
    needs: [test-1, test-2]
    if: |
      always() && 
      (needs.test-1.result == 'success' || needs.test-1.result == 'skipped') && 
      (needs.test-2.result == 'success' || needs.test-2.result == 'skipped') && 
      !(needs.test-1.result == 'skipped' && needs.test-2.result == 'skipped')
    steps:
      - name: Slack Notification
        uses: rtCamp/[email protected]
        env:
          SLACK_CHANNEL: alert
          SLACK_COLOR: "${{ job.status == 'success' && 'good' || 'danger' }}"
like image 96
Krzysztof Madej Avatar answered Nov 30 '25 05:11

Krzysztof Madej


Implementation such condition can be achieved by using following workflow logic

jobs:
  A:
    runs-on: ubuntu-latest
    steps:
      - run: echo "success always"
  B:
    runs-on: ubuntu-latest
    if: false
    steps:
      - run: echo "this will never run"
  C:
    runs-on: ubuntu-latest
    steps:
      - run: |
          exho "here to fail"
          exit 1

  D:
    runs-on: ubuntu-latest
    needs: [A, B, C]
    if: |
      always() &&
      !contains(needs.*.result, 'cancelled')
    # you can add conditions if needed e.g.
    # !contains(needs.*.result, 'failure')
    steps:
      - run: echo "${{ toJSON(needs) }}"

Your need to modify also your SLACK_COLOR logic. job.status == 'success' will not work since it will always be success for this job regardless did any of the dependencies failed. Use needs for your logic e.g. job D output is:

{
  A: {
    result: success,
    outputs: {}
  },
  B: {
    result: skipped,
    outputs: {}
  },
  C: {
    result: failure,
    outputs: {}
  }
}

so you SLACK_COLOR could be set like this

SLACK_COLOR: "${{ contains(needs.*.result, 'failure') && 'danger' || 'good' }}"
like image 36
mkungla Avatar answered Nov 30 '25 05:11

mkungla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!