4.0.2 • Published 10 months ago

semantic-release-slack-bot v4.0.2

Weekly downloads
12,786
License
MIT
Repository
github
Last release
10 months ago

semantic-release-slack-bot

semantic-release plugin to get release notifications on slack from a slack bot

npm npm CircleCI branch license add to slack

StepDescription
successSend a slack message to notify of a new release.
failSend a slack message to notify of a failed release.

Install

Add the plugin to your npm-project:

$ npm install semantic-release-slack-bot -D

Requirements

As per the new release of semantic-release library version 20, the library is requiring the use of Node v18, which we need to comply as well to use keep the repo updated with it's latest feature. More information can be found here.

Slack App/Webhook Usage

The corresponding slack app has to be installed in your slack workspace as well. Follow the instructions under configuration for more information.

The plugin can be configured in the semantic-release configuration file:

{
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    [
      "semantic-release-slack-bot",
      {
        "notifyOnSuccess": false,
        "notifyOnFail": false,
        "slackWebhook": "https://my-webhook.com",
        "branchesConfig": [
          {
            "pattern": "lts/*",
            "notifyOnFail": true
          },
          {
            "pattern": "master1",
            "notifyOnSuccess": true,
            "notifyOnFail": true
          }
        ]
      }
    ]
  ]
}

With this example:

  • Slack notification will always be sent using the "https://my-webhook.com" webhook url
  • Slack notifications are sent on a failure release from branches matching "lts/*"
  • Slack notifications are sent on a failure or successful release from branch "master"
  • Slack notifications are skipped on all other branches

Note

The official documentation says that a developer cannot override the default name, channel or icon that are associated with the webhook, but some users of this library reported that they were able to.

  • use slackName (or SLACK_NAME env var) for overriding slack app name
  • use slackIcon (or SLACK_ICON env var) for overriding slack app icon
  • use slackChannel (or SLACK_CHANNEL env var) for overriding slack channel

WARNING: This is not mentioned in the official documentation, so use at your own risk.

Slack Access Token/Channel Usage

This configuration can be used with a bot Slack Access token with minimum permissions of chat:write.

The plugin can be configured in the semantic-release configuration file:

{
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    [
      "semantic-release-slack-bot",
      {
        "notifyOnSuccess": false,
        "notifyOnFail": false,
        "slackToken": "token",
        "slackChannel": "#my-channel-name",
        "branchesConfig": [
          {
            "pattern": "lts/*",
            "notifyOnFail": true
          },
          {
            "pattern": "master1",
            "notifyOnSuccess": true,
            "notifyOnFail": true
          }
        ]
      }
    ]
  ]
}

With this example:

  • Slack notification will always be sent to the channel "#my-channel-name"
  • Slack notifications are sent on a failure release from branches matching "lts/*"
  • Slack notifications are sent on a failure or successful release from branch "master"
  • Slack notifications are skipped on all other branches

Screenshots

Screenshot of success Screenshot of fail

Configuration

Slack app installation

The plugin uses a slack webhook which you get by adding the slack app to your slack workspace. Register the app using the button below or this link.

For the security concerned, feel free to create your own slack app and create a webhook or inspect the server code that does this creation for you at create-webhook.js. The only required permission for the webhook is to publish to a single channel.

Slack Webhook

Installing the app will yield you with a webhook that the app uses to publish updates to your selected channel. The Slack webhook authentication link is required and needs to be kept a secret. It should be defined in the environment variables.

Slack Access tokens

If you are creating your own slack app you can choose to use a bot access token and channel instead of the webhook with at least one of the following permission scopes.

  1. chat:write with this permission scope the app/bot must be added to any channels before it can post to them
  2. chat:write.public with this permission scope we can automatically post to any public channel for more information see here

Environment variables

Options can be defined in the environment where you will run semantic release. This can be done by exporting it in bash or in the user interface of your CI provider.

Alternatively, you can pass the webhook as a configuration option or use an Access Token.

VariableDescription
SLACK_WEBHOOKSlack webhook created when adding app to workspace.
SLACK_TOKENSlack bot Access token.
SLACK_CHANNELSlack channel name or id to send notifications to.
SLACK_ICONSlack bot app icon.
SLACK_NAMESlack bot app name.
SEMANTIC_RELEASE_PACKAGEOverride or add package name instead of npm package name

Options

OptionDescriptionDefault
notifyOnSuccessDetermines if a successful release should trigger a slack message to be sent. If false this plugin does nothing on success.false
notifyOnFailDetermines if a failed release should trigger a slack message to be sent. If false this plugin does nothing on fail.false
onSuccessFunctionProvides a function for the slack message object on success when notifyOnSuccess is true. See function.undefined
onFailFunctionProvides a function for the slack message object on fail when notifyOnFail is true. See function.undefined
onSuccessTemplateProvides a template for the slack message object on success when notifyOnSuccess is true. See templating.undefined
onFailTemplateProvides a template for the slack message object on fail when notifyOnFail is true. See templating.undefined
markdownReleaseNotesPass release notes through markdown to slack formatter before rendering.false
slackWebhookEnVarThis decides what the environment variable for exporting the slack webhook value.SLACK_WEBHOOK
slackWebhookSlack webhook created when adding app to workspace.value of the environment variable matching slackWebhookEnVar
slackTokenEnVarThis decides what the environment variable for exporting the slack token value.SLACK_TOKEN
slackTokenSlack bot token.value of the environment variable matching slackTokenEnVar
slackChannelEnVarThis decides what the environment variable for exporting the slack channel value.SLACK_CHANNEL
slackChannelSlack channel name or id to send notifications to.value of the environment variable matching slackChannelEnVar
slackIconEnVarThis decides what the environment variable for specifying the slack app icon with slack emoji. ex. :smile: or smile (without semicolons)SLACK_ICON
slackIconSlack app iconvalue of the environment variable matching slackIconEnVar
slackNameEnVarThis decides what the environment variable for specifying the slack app nameSLACK_NAME
slackNameSlack app namevalue of the environment variable matching slackNameEnVar
packageNameOverride or add package name instead of npm package nameSEMANTIC_RELEASE_PACKAGE or npm package name
unsafeMaxLengthMaximum character length for the release notes before truncation. If unsafeMaxLength is too high, messages can be dropped. Read here for more information. Set to '0' to turn off truncation entirely.2900
branchesConfigAllow to specify a custom configuration for branches which match a given pattern. For every branches matching a branch config, the config will be merged with the one put at the root. A key "pattern" used to filter the branch using glob expression must be contained in every branchesConfig.[]

Function

If a function is provided with either the onSuccessFunction or onFailFunction options, it will be used for the respective slack message. The function should return an object that follows the Slack API message structure. The function is passed two objects, pluginConfig and context, the same objects that are passed to plugins.

Note: This only works with a configuration file that exports an object (see below for an example).

const slackifyMarkdown = require('slackify-markdown')
const { chunkifyString } = require('semantic-release-slack-bot/lib/chunkifier')

const onSuccessFunction = (pluginConfig, context) => {
  const releaseNotes = slackifyMarkdown(context.nextRelease.notes)
  const text = `Updates to ${pluginConfig.packageName} has been released to *Stage!*`
  const headerBlock = {
    type: 'section',
    text: {
      type: 'mrkdwn',
      text
    }
  }

  return {
    text,
    blocks: [
      headerBlock,
      ...chunkifyString(releaseNotes, 2900).map(chunk => {
        return {
          type: 'section',
          text: {
            type: 'mrkdwn',
            text: chunk
          }
        }
      })
    ]
  }
}

module.exports = {
  branches: ['master'],
  preset: 'eslint',
  plugins: [
    [
      'semantic-release-slack-bot',
      {
        notifyOnSuccess: true,
        onSuccessFunction,
        packageName: 'Testing Semantic Release'
      }
    ]
  ]
}

Templating

If a template is provided via either the onSuccessTemplate or onFailTemplate options, it will be used for the respective slack message. The template should be an object that follows the Slack API message structure. Strings within the template will have keywords replaced:

KeywordDescriptionExampleTemplate
$package_nameThe name of the package.semantic-release-testBoth
$npm_package_versionThe version of the release.1.0.93onSuccessTemplate
$repo_pathThe repository path.juliuscc/semantic-release-testBoth
$repo_urlThe repository URL.https://github.com/juliuscc/semantic-release-testBoth
$release_notesThe notes of the release.onSuccessTemplate

A sample configuration with template can look like this

"onSuccessTemplate": {
  "text": "A new version of $package_name with version $npm_package_version has been released at $repo_url!"
}

Helper functions

There are two helper functions exported by the chunkifier module.

chunkifyArray takes an array of strings and returns a new array based on maxLength and delimiter. delimiter is optional and defaults to newline.

chunkifyString takes a string and returns an array based on maxLength and delimiter. delimiter is optional and defaults to newline.

See respective implementation for more details.

4.0.2

10 months ago

4.0.1

12 months ago

4.0.0

1 year ago

3.5.3

2 years ago

3.4.0

2 years ago

3.3.0

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.1

2 years ago

3.0.0

2 years ago

2.1.2

2 years ago

2.1.1

3 years ago

2.1.0

3 years ago

1.7.0

3 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.1-beta.1

4 years ago

1.6.1-beta.0

4 years ago

1.6.0-beta.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

5 years ago

1.2.0

5 years ago

2.0.0

5 years ago

1.1.3-alpha.0

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago