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

I have several use cases where the environment variables for a job change depending on if I am on the master branch or not, or for example if the build up to the point has failed.

My code for sending a slack message based on build status looks something like this:

- name: Success Send Build Notification
  uses: rtCamp/action-slack-notify@v2.1.0
  if: github.ref == 'refs/heads/master' &&  success()
  env:
    SLACK_COLOR: "good"
    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 build has passed!."

- name: Failed Send Build Notification
  uses: rtCamp/action-slack-notify@v2.1.0
  if: github.ref == 'refs/heads/master' &&  failure()
  env:
    SLACK_COLOR: "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 build has failed. :("

It would be much more convenient if I could instead combine this into a single code block which conditional env vars, something like this?

- 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. :('"

Is there a way to do this, or perhaps another creative, compact way of accomplishing the same goal?

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) branch would be taken incorrectly. Not a problem here however, because it’s not an empty string or the like.

Another pseudo ternary expression that I haven’t tested yet, but which should work in principle and doesn’t suffer from above limitation:

fromJSON('["danger", "good"]')[job.status == 'success']

This relies on type coercion, an implicit cast from boolean true to 1 or false to 0, which is then used as array index. As array literals aren’t supported (yet), fromJSON() is required to construct the array.

2 Likes

@bagel-dawg,

Possible to use conditional in the “env” section of a job?

This is not supported currently.

As a workaround, you can try to configure your workflow like as below:

jobs:
  build:
    . . .

  send_slack_msg:
    needs: [build]
    if: ${{ github.ref == 'refs/heads/master' && always() }}
    runs-on: ubuntu-latest
    steps:
      - name: Set environment variables
        run: |
          echo "SLACK_USERNAME=Github Actions" >> $GITHUB_ENV
          echo "SLACK_ICON_EMOJI=:ocotcat:" >> $GITHUB_ENV
          echo "SLACK_TITLE=This is a GitHub Actions build!" >> $GITHUB_ENV
          echo "SLACK_WEBHOOK=${{ secrets.SLACK_WEBHOOK }}" >> $GITHUB_ENV
          echo "SLACK_CHANNEL=#channel" >> $GITHUB_ENV

          if [[ ${{ needs.build.result }} == success ]]; then
            echo "SLACK_COLOR=good" >> $GITHUB_ENV
            echo "SLACK_MESSAGE=This build has passed!" >> $GITHUB_ENV
          else
            echo "SLACK_COLOR=danger" >> $GITHUB_ENV
            echo "SLACK_MESSAGE=This build has failed." >> $GITHUB_ENV
          fi

      - name: Send Build Notification
        uses: rtCamp/action-slack-notify@v2.1.0

Thanks! I’ll try out both solutions and select one.