slack-orb icon indicating copy to clipboard operation
slack-orb copied to clipboard

Using dynamic template with env variables does not work

Open valentindotxyz opened this issue 4 years ago • 1 comments

Orb version:

4.4.2

What happened:

I'm trying to send a list of commits in a Slack message but so far, without success, encountering this error parse error: Invalid numeric literal at line 1. From the documentation, it is a bit unclear to me how to use dynamic templates that are using env variables so any hint would be more than welcomed!

This is what I have done so far:

  • config.yml:
  steps:
      - checkout
      - jq/install
      - run:
          name: Export commits list
          command: |
            echo 'export COMMITS_LIST=$(git log --oneline origin/master...origin/develop | jq -aRs .)' >> $BASH_ENV
            echo 'export SLACK_AUTO_DEPLOY_TEMPLATE=$(cat .circleci/slack/auto-deploy.json)' >> $BASH_ENV
            source $BASH_ENV
      - slack/notify:
          event: always
          channel: 'XXX'
          template: SLACK_AUTO_DEPLOY_TEMPLATE
  • .circleci/slack/auto-deploy.json:
{
  "blocks":[
    {
      "type":"section",
      "text":{
        "type":"mrkdwn",
        "text":"Latest changes: \`\`\`${COMMITS_LIST}\`\`\`"
      }
    }
  ]
}

Expected behavior:

Using dynamic template with an environment variable should work without raising a "parse error".

Additional Information:

  • I've tried piping to jq -aRs . to help but no luck so far 😔
  • Might be related to #264

Thanks a lot for your help!

valentindotxyz avatar Jun 10 '21 17:06 valentindotxyz

I integrated my CI with Slack Orb version 4.1.1 and the following worked for me -

{
   "type": "section",
   "text": {
    "type": "mrkdwn",
    "text": "```${commits_message}```"
   }
}

The above variable had multiple lines of strings, so had to use ` 3 times. You can either use that or see the following, where I listed all of my blocks -

"blocks": [
 {
  "type": "header",
  "text": {
   "type": "plain_text",
   "text": "Build ${CIRCLE_TAG} Successful! :tada:",
   "emoji": true
  }
 },
 {
  "type": "section",
  "fields": [
   {
    "type": "mrkdwn",
    "text": "*Tag*:\n`$CIRCLE_TAG`"
   },
   {
    "type": "mrkdwn",
    "text": "*Commiter*:\n${CIRCLE_USERNAME}"
   }
  ],
  "accessory": {
   "type": "image",
   "image_url": "https://assets.brandfolder.com/otz5mn-bw4j2w-6jzqo8/original/circle-logo-badge-black.png",
   "alt_text": "CircleCI Logo"
  }
 },
 {
  "type": "actions",
  "elements": [
   {
    "type": "button",
    "text": {
     "type": "plain_text",
     "text": "View Job"
    },
                   "url": "${CIRCLE_BUILD_URL}"
   }
  ]
 },
 {
  "type": "divider"
 },
       {
          "type": "section",
          "text": {
           "type": "mrkdwn",
           "text": "List of issues: <${jiras}|Jira Filter>"
          }
       },
 {
  "type": "section",
  "text": {
   "type": "mrkdwn",
   "text": "Change Log:"
  }
 },
       {
          "type": "section",
          "text": {
           "type": "mrkdwn",
           "text": "```${commits_message}```"
          }
         }
]

Also, one more thing to note here is that I did not install jq explicitly, and, all the bash commands that I had have been put in a bash_env.sh file, as shown below. In this file, I calculated the needed variables and in the end, I just exported these variables so that they can be loaded into the job.


export commits_message
export jiras

My job looks like this -

ci_build:
  docker: # use the Docker executor
    - image: circleci/python:3.7.9-stretch
      environment:
        TZ: "Asia/Kolkata"
        BASH_ENV: ".circleci/bash_env.sh"
  steps: # steps that comprise the `ci_build` job
    - checkout # check out source code to working directory
    - run:
          name: "Latest Tag Print"
          command: |
            echo 'Generating'
    - slack/notify:
          channel: 'C61C3FWJ39K'
          custom: |

One more heads up I wanna give you is that if needed, please parse the bash variables that you export in the bash_env.sh script to be jq compliant, otherwise, the job will fail with jq parse errors.

It took multiple trial and error tries for me to figure this out. Hope this helps you out.

Also, double quotes if present in your bash variable must be escaped like the following, where I escaped the double quotes present in the commits_file variable and stored that in commits_message variable which I exported and used in the job above -

export commits_message=$(awk '{printf "%s\\n", $0}' commits_file | sed 's/"/\\"/g')

nikhilkumarbol avatar Jun 24 '21 06:06 nikhilkumarbol