Skip to content

Possible to use conditional in the "env" section of a job? #25725

Answered by Simran-B
bagel-dawg asked this question in Actions
Discussion options

You must be logged in to vote

There is no support for a ternary operator, but there are workarounds. You can try the following:

- name: Send Build Notification
  uses: rtCamp/action-slack-notify@v2.1.0
  if: github.ref == 'refs/heads/master' &&  success()
  env:
    SLACK_COLOR: "${{ job.status == 'success' && 'good' || 'danger' }}"
    SLACK_USERNAME: "Github Actions"
    SLACK_ICON_EMOJI: ":ocotcat:"
    SLACK_TITLE: "This is a GitHub Actions build!"
    SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
    SLACK_CHANNEL: "#channel"
    SLACK_MESSAGE: "This job has ${{ job.status == 'success' && 'passed!' || 'failed. :(' }}"

A limitation here is that the value after && must not be falsey, because in that case the || (or)…

Replies: 4 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@devSajan
Comment options

Answer selected
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@ansiwen
Comment options

@m4heshd
Comment options

@m4heshd
Comment options

@ansiwen
Comment options

@m4heshd
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment