CircleCI-Public / slack-orb

Create custom Slack notifications for CircleCI job statuses
https://circleci.com/developer/orbs/orb/circleci/slack
MIT License
213 stars 205 forks source link

Orb4 beta: Script not running, nothing posted on Slack #165

Closed beders closed 4 years ago

beders commented 4 years ago

Orb version

version: 2.1

orbs: slack: circleci/slack@dev:beta

What happened

No slack message was posted even though step returned with exit code 0

Here's the relevant config:

- slack/notify:
          event: pass
          webhook: DEV_WEBHOOK
          custom: >
            {"blocks":[{"type":"header","text":{"type":"plain_text","text":"Xanadu deployed :yay:","emoji":true}},{"type":"section","text":{"type":"mrkdwn","text":"Access your build <${PREVIEW_LINK}|here> *Test: Ignore*"}}]}

and this is the output for "Slack - Sending Notification":

#!/bin/bash -eo pipefail

SetEnvVars() {
    INTRNL_SLACK_WEBHOOK=$(eval echo "$SLACK_PARAM_WEBHOOK")
}

BuildMessageBody() {
    # Send message
    #   If sending message, default to custom template,
    #   if none is supplied, check for a pre-selected template value.
    #   If none, error.
    if [ -n "$SLACK_PARAM_CUSTOM" ]; then
        ModifyCustomTemplate
        CUSTOM_BODY_MODIFIED=$(echo $CUSTOM_BODY_MODIFIED | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g' | sed 's/</\\</g' | sed 's/>/\\>/g')
        T2=$(eval echo $CUSTOM_BODY_MODIFIED)
        echo $T2
    elif [ -n "$SLACK_PARAM_TEMPLATE" ]; then
        TEMPLATE="$(echo \$$SLACK_PARAM_TEMPLATE)"
        T1=$(eval echo $TEMPLATE | sed 's/"/\\"/g' | sed 's/\\n/\\\\n/g')
        T2=$(eval echo $T1)
    else
        echo "Error: No message template selected."
        echo "Select either a custom template or one of the pre-included ones via the 'custom' or 'template' parameters."
        exit 1
    fi
    SLACK_MSG_BODY=$T2
}

PostToSlack() {
    curl -f -X POST -H 'Content-type: application/json' \
        --data \
        "$SLACK_MSG_BODY" "$INTRNL_SLACK_WEBHOOK"
}

Notify() {
    if [[ "$CCI_STATUS" == "$SLACK_PARAM_EVENT" || "$SLACK_PARAM_EVENT" == "always" ]]; then
    PostToSlack
    echo "Sending Notification"
    else
        # dont send message.
        echo "NO SLACK ALERT"
        echo
        echo "This command is set to send an alert on: $SLACK_PARAM_EVENT"
        echo "Current status: $CCI_STATUS"
        exit 0
    fi
}

ModifyCustomTemplate() {
    # Inserts the required "text" field to the custom json template from block kit builder.
    # Block Kit Builder will not work with webhooks without this.
    if [ "$(echo "$SLACK_PARAM_CUSTOM" | jq '.text')" == "null" ]; then
        CUSTOM_BODY_MODIFIED=$(echo "$SLACK_PARAM_CUSTOM" | jq '. + {"text": ""}')
    else
        # In case the text field was set manually.
        CUSTOM_BODY_MODIFIED=$(echo $SLACK_PARAM_CUSTOM | jq '.')
    fi
    echo "$CUSTOM_BODY_MODIFIED"
}

InstallJq() {
    if echo $OSTYPE | grep darwin > /dev/null 2>&1; then
        brew install jq
        return $?
    fi

    if cat /etc/issue | grep Alpine > /dev/null 2>&1; then
        apk add jq
        return $?
    fi

    if cat /etc/issue | grep Debian > /dev/null 2>&1 || cat /etc/issue | grep Ubuntu > /dev/null 2>&1; then
        if [[ $EUID == 0 ]]; then export SUDO=""; else # Check if we're root
            export SUDO="sudo";
        fi
        $SUDO apt update
        $SUDO apt install -y jq
        return $?
    fi

}

# Will not run if sourced from another script.
# This is done so this script may be tested.
if [[ "$_" == "$0" ]]; then
    InstallJq
    SetEnvVars
    BuildMessageBody
    Notify
fi

CircleCI received exit code 0

Expected behavior

Message should be posted.

gmemstr commented 4 years ago

@beders Interesting - what Docker image are you running in your job?

beders commented 4 years ago

We are running on - image: circleci/clojure:lein-2.9.1-node-browsers

beders commented 4 years ago

aand we just switched to - image: circleci/clojure:lein-2.9.3-node-browsers although I have not tested it with that image yet.

KyleTryon commented 4 years ago

Thank you @beders, allow me to look into this.

KyleTryon commented 4 years ago

@beders The issue should be resolved 👍 thank you for the feedback! Information in #166 if interested.

I would love to hear if this worked for you, or if you run into any other issues. Thank you again.

beders commented 4 years ago

Thanks guys. Will try again

beders commented 4 years ago

Also: Avoid bash. I’ve been manually posting to slack from bash and it is a string escape nightmare. Might I suggest babashka?

gmemstr commented 4 years ago

@beders As much as I love Lisps/Clojure, I don't think it's feasible to implement into all of our images or within a specific orb - I understand bash is not ideal, but given the fact it's available in most/all the jobs it's the best option.

beders commented 4 years ago

For an orb like that, which seems to be written purely in bash, literally any other scripting language will give you a better experience. (ok, maybe not perl ;). Babashka is a 42MB executable (vs. bash around 600k), so it is a bit heavy-weight, but even python or tcl are vastly better than trying to wrangle the idiosyncrasies of bash - and there are many. There's also Ferret that is able to produce binaries that run on 2KB of RAM. It is a sad day when bash is considered the best option.

Anyways, thanks again for being on top of this bug and fixing it quickly!