npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deploy-notify-slack

v0.5.9

Published

Send Slack notification about deploy with version comments

Downloads

650

Readme

Dummy script to send notification about new version deployment to a Slack channel

  • no npm dependencies, plain nodejs 8.x or higher
  • use Slack incoming webhooks API to send a message
  • can attach version description Markdown files

Use default message template

You can use default message template with the following env variables:

Required env variables

  • SLACK_WEBHOOK_URL - you should generate webhook url for your target channel, see: https://api.slack.com/messaging/webhooks
  • STAGE - name of an application stage you're deploying, usually: dev, staging, prod..
  • VERSION - deployed version

Optional env variables

  • TITLE - ('Deployment' by default) notification title
  • CHANGELOG_PATH - path of your deployed version details file (changelog by default as well as we assume that the package installed locally, so this option is required if the package installed globally)
  • COLOR - ('#7f8583' by default) left bar notification color. Should be hex color code without # symbol
  • EMOJI - (':rocket:' by default) emoji to be displayed in the notification title
  • MAX_BLOCKS - (5 by default) maximum amount of large blocks(2500 symbols) available in slack message. If your changelog is bigger than this value it will be truncated.

version details file is a Markdown file having the name like ${STAGE}-v${VERSION}.md.

you can also create cross-environment file with name pattern v${VERSION}.md and if script cannot find stage specific description it will get this one.

If no description file found details block will be omitted in Slack message.

  • FAILS_IF_NOT_SENT - (false by default) Should exit with not 0 error code if message was not sent successfully.

How it works

  • Generate Slack webhook URL https://api.slack.com/messaging/webhooks

  • In Bitbucket pipeline or another place you wish to notify about just deployed version of your application you can add dev dependency

npm i --no-save deploy-notify-slack@^0.5

or major version

npm i --location=global deploy-notify-slack@^0.5
  • run the scrypt with your env variables:
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXXXXX STAGE=dev VERSION=1.0.0 node ./node_modules/deploy-notify-slack/notify

Bitbucket pipeline example:

- step:
    name: Notify deploy
    image: node:16-alpine
    script:
      - npm i --location=global deploy-notify-slack
      - VERSION=$(npm run version --silent)
      - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node ./node_modules/deploy-notify-slack/notify

or install package globally

- step:
    name: Notify Slack
    image: node:16-alpine
    script:
      - npm i --location=global deploy-notify-slack
      - VERSION=$(npm run version --silent)
      - PWD=$(pwd)
      - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION CHANGELOG_PATH=$PWD/changelog node /usr/local/lib/node_modules/deploy-notify-slack/notify.js

version script above is just a echo $npm_package_version command

Full bitbucket CI/CD pipeline example for deploy NestJs application and send deploy message:

image: atlassian/default-image:2
clone:
  depth: full
pipelines:
  default:
    - step:
        name: Test and Build
        image: node:16-alpine
        caches:
          - node
        script:
          - npm ci
          - npm run test:ci
          - npm run build
        services:
          - database
        artifacts:
          - node_modules/**
          - dist/**
    - step:
        name: Pack and deploy to bundle
        script:
          - VERSION=$(npm run version --silent)
          - cp .env.static .env
          - zip -r application.zip . -x "src/*" -x "docker/*" -x "test/*" -x "cloudformation/*"
          - pipe: atlassian/aws-elasticbeanstalk-deploy:1.0.2
            variables:
              AWS_ACCESS_KEY_ID: $AWS_DEV_ACCESS_KEY_ID
              AWS_SECRET_ACCESS_KEY: $AWS_DEV_SECRET_ACCESS_KEY
              AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
              S3_BUCKET: $AWS_DEV_DEPLOY_BUCKET
              VERSION_LABEL: "DEV-${VERSION}-${BITBUCKET_BUILD_NUMBER}-${BITBUCKET_COMMIT:0:8}"
              APPLICATION_NAME: $AWS_DEV_APP_NAME
              ENVIRONMENT_NAME: $AWS_DEV_EB_ENV_NAME
              ZIP_FILE: "application.zip"
    - step:
        name: Notify Slack
        image: node:16-alpine
        script:
          - npm i --location=global deploy-notify-slack
          - VERSION=$(npm run version --silent)
          - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node /usr/local/lib/node_modules/deploy-notify-slack/notify.js
  
definitions:
  services:
    database:
      image: postgres
      user: postgres
      variables:
        POSTGRES_DB: test
        POSTGRES_USER: api
        POSTGRES_PASSWORD: example 

Use custom message template

You can specify your own message template instead of default one. It's useful if you want to add some additional information to the message.

Try to use Slack message builder to create your own message template.

Then you should load your template from file and pass it to the script as env variable CUSTOM_MESSAGE:

For example you saved your message template to message.json file:

{
  "blocks": [
    {
      "type": "header",
      "text": {
        "type": "plain_text",
        "text": ":flying_saucer: New API deploy of stage *DEV*",
        "emoji": true
      }
    }
  ]
}

Then you can run the script with the following command:

npm i --location=global deploy-notify-slack@latest
CUSTOM_MESSAGE=$(cat message.json)
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} CUSTOM_MESSAGE=$CUSTOM_MESSAGE node /usr/local/lib/node_modules/deploy-notify-slack/notify.js