CircleCI Slack Orb Custom Template
Tags: devops, circleci, cicd
Published: 2021-03-24 09:59:23

Hey there, how's CircleCI treating you? If you are using it as your CI/CD tool, I'm sure there is need for Slack notifications reporting the status to a Slack channel. There are some default templates like `basic_fail_1` and `success_tagged_deploy_1 ` by [CircleCI/Slack Orb](https://circleci.com/developer/orbs/orb/circleci/slack) for instant usage, but you may also provide custom template if u dislike the default template format.

The approach for template customization here is to include more informational tags in the notification message. In addition, we want it to be a reusable template and used by multiple jobs in our pipeline. We can do that with `commands` in the CircleCI's `config.yml`. Notice the << parameters.env >> will be a dynamic input string.

```yml
commands:
  notify-success:
    parameters:
      env:
        type: string
        default: "env"
    steps:
      - slack/notify:
          event: pass
          custom: |
            {
              "text": "CircleCI job succeeded!",              
              "blocks": [
                {
                  "type": "header",
                  "text": {
                    "type": "plain_text",
                    "text": "Job Successful. :tada:",
                    "emoji": true
                  }
                },
                {
                  "type": "section",
                  "fields": [
                    {
                      "type": "mrkdwn",
                      "text": "*Env*: << parameters.env >>"
                    },
                    {
                      "type": "mrkdwn",
                      "text": "*Job*: ${CIRCLE_JOB}"
                    }
                  ]
                },
                {
                  "type": "section",
                  "fields": [
                    {
                      "type": "mrkdwn",
                      "text": "*Project*: $CIRCLE_PROJECT_REPONAME"
                    },
                    {
                      "type": "mrkdwn",
                      "text": "*Branch*: $CIRCLE_BRANCH"
                    }
                  ]
                },
                {
                  "type": "actions",
                  "elements": [
                    {
                      "type": "button",
                      "text": {
                        "type": "plain_text",
                        "text": "View Job"
                      },
                      "url": "${CIRCLE_BUILD_URL}"
                    }
                  ]
                }
              ]
            }
```
To use commands in your jobs, for example:

```yml
jobs:
  deploy-mobile:
    docker:
      - image: 'cimg/node:15.14.0'
    resource_class: large
    steps:
      - checkout
      - node/install-packages:
          pkg-manager: yarn
          app-dir: ./mobile
          override-ci-command: yarn install --frozen-lockfile
          with-cache: true
      - run: yarn deploy:mobile
      - notify-success:
          env: "Mobile Staging"
  deploy-web:
    docker:
      - image: 'cimg/python:3.6.11-node'
    resource_class: large
    steps:
      - checkout
      - node/install-packages:
          pkg-manager: yarn
          app-dir: ./webapp
          override-ci-command: yarn install --frozen-lockfile
          with-cache: true
      - run: yarn deploy:web
      - notify-success:
          env: "Web Production"
```

Slack notification results in:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxt0una440wpmw25ra5e.png)