-
Notifications
You must be signed in to change notification settings - Fork 205
Reuse Custom Templates
While you can provide a custom template via the custom
parameter, these custom templates are not reusable and add many lines to your config.
If you have created a custom template that you wish to use in multiple projects, it is possible to share the template and reduce the amount of lines added to your config using Contexts.
A Context on CircleCI is a "bucket" of environment variables that can be injected in your jobs across multiple projects.
Begin by designing your desired template.
Utilize the Block Kit Builder from slack to quickly and easily, visually design the desired notification message. When complete, copy the generated code to your clipboard.
Note: You can reference environment variables (example: ${CIRCLE_JOB}
) in the template directly and they will be populated at run-time.
Follow the CircleCI docs to create a Context, and give your Context a name such as slack-templates
.
Now you can create environment variables containing your Slack template JSON that was copied earlier. The name you give the environment variable will be the name of the template.
Contexts are added to jobs via your workflow, and you can add as many as you need. Any job with the context we have created here will be able to access these templates via the template
parameter using the name of the environment variable for that template.
Here is a full example.
version: '2.1'
orbs:
slack: circleci/slack@4.3
jobs:
my-job:
docker:
- image: 'cimg/base:stable'
steps:
- slack/notify:
event: always
template: MY_SLACK_TEMPLATE
channel: ABCXYZ
workflows:
my-workflow:
jobs:
- my-job:
context: slack-templates
The templates within slack-templates
can now be imported this way in all projects within the organization.