Send slack notification at end of workflow

For scenarios where you are using the Slack orb and want to send a notification if a workflow itself has been successful or failed -- you would need to implement a notification per job.

For workflows that run sequentially, you can anchor the Slack notification and apply the anchor to each of the jobs in the workflow via post-steps -- except the last job that is run. For that job, you would set it to the standard slack/notify to catch both successes and failures. Below is an example of how this may look:

version: 2.1

slack-fail-post-step: &slack-fail-post-step
  post-steps:
    - slack/status:
        fail_only: true

orbs:
  slack: circleci/slack@3.4.2

jobs:
  build:
    docker:
      - image: cimg/base:stable
      
    steps:
      - run:
          name: Success
          command: |
            exit 0
  last-job:
    docker:
      - image: cimg/base:stable
      
    steps:
      - run:
          name: Success
          command: |
            exit 0
  
workflows:
  version: 2
  workflow:
    jobs:
      - build: *slack-fail-post-step
      - last-job:
          requires:
            - build
          post-steps:
            - slack/status

However if your workflow jobs don't run sequentially, you will need to create a new job that contains the slack notification and require that the job has run all the previous jobs. 

Was this article helpful?
2 out of 4 found this helpful

Comments

0 comments

Article is closed for comments.