How can I add more context to my Slack Message?

version: 2.1

orbs:
  katalon-studio: katalon/katalon-studio@23.0.11
  slack: circleci/slack@4.12.0  # Include Slack Orb

workflows:
  build:
    jobs:
      - katalon-studio/run:
          version: "latest"
          command_arguments: '-retry=0 -testSuitePath="Test Suites/I am on the website" -browserType="greg_lambdatest"'
          post-steps:
            - run:
                name: Save Job ID
                command: echo ${CIRCLE_WORKFLOW_JOB_ID} > /tmp/job_id.txt
            - persist_to_workspace:
                root: /tmp
                paths:
                  - job_id.txt
      - post_to_slack:
          requires:
            - katalon-studio/run
          filters:
            branches:
              only: Dev_New  # âś… Keep filters for branch control

jobs:
  post_to_slack:
    docker:
      - image: cimg/base:stable
    steps:
      - attach_workspace:
          at: /tmp
      - run:
          name: "Determine Job Status & Send Slack Notification"
          command: |
            # Read the job ID from the file and strip any unwanted characters
            KATALON_JOB_ID=$(cat /tmp/job_id.txt | tr -d '[:space:]')

            # Determine if the pipeline passed or failed
            if [[ "$CIRCLE_JOB_STATUS" == "failed" ]]; then
              STATUS="❌ *FAILED*"
            else
              STATUS="âś… *PASSED*"
            fi

            # Get the correct artifact URL dynamically
            ARTIFACT_URL="https://output.circle-artifacts.com/output/job/${KATALON_JOB_ID}/artifacts/0/report/report.html"
            
            # Fetch execution duration (start and end time)
            EXECUTION_TIME=$(circleci-agent step timing | awk '/Total/{print $3}')

            # Send the message to Slack
            curl -X POST -H 'Content-type: application/json' \
            --data "{\"text\": \"${STATUS} - Test Report: <${ARTIFACT_URL}|Click here to view>\"}" \
            $SLACK_WEBHOOK_URL

I use the above circle-yml to run 2 jobs. Right now the 2nd job post_to_slack is working and send a message with a link to the report/report.html generated from the katalon-studio/run job.

I’ve been trying to add more context to this message though and am having issues with various things.

  1. The post_to_slack is only working when katalon-studio/run passes, if it fails then post_to_slack does not execute. I want my post_to_slack message to send regardless of a Pass or Fail for the test job.

  2. I want to add more details to the Slack message such as Execution Time, Git branch, and even username

Can anyone advise me on how I can improve my YML to support these?

@gregory-olshansky_sa The slack orb has an example of always sending a slack message even if the job passes or fails, CircleCI Developer Hub - circleci/slack. That that specific example, event is set to fail which will only send when the job fails. You can change this to always, to have it always send. You will need to move the post_to_slack job to an step inside your other job. Or I believe you should be able to use it in your post-steps you already have.

There are some other ways you could handle this as well, with the new ability to requires which enables you to set conditions like:

      - post_to_slack:
          requires:
            - katalon-studio/run: failed

As for getting more data into the message, look at CircleCI’s built in environment variables and using those in your messages!

2 Likes