zseikyocho
zseikyocho

Reputation: 671

How to add custom message to slack with variable in Jenkins?

I want to send slack message after Jenkins build a task like this:

My_Job - #10 Success after 18 sec (Open)
My Report: http://my_domain/report_20181017195500.html

So I tried to set this varialbe in the Execute shell area of the job in Jenkins:

...
DATE=`date '+%Y%m%d%H%M%S'`
MY_REPORT="report-$DATE.html"

Add this to the Include Custom Message of the Slack Notifications block:

My Report: http://my_domain/${MY_REPORT}

But when I check the Slack channel, it send me:

My_Job - #10 Success after 18 sec (Open)
My Report: http://my_domain/${MY_REPORT}

How to set $MY_REPORT the true value in this case?


Edit

I have tried EnvInject plugin. Set this code to the Execute shell block:

...
echo MY_REPORT=$(echo "http://my_domain/report-$DATE.html")  > /var/lib/jenkins/var.properties

It can read value from the /var/lib/jenkins/var.properties which set in the Inject environment variables to the build process of Build Environment block. But can't write into that file from Execute shell.

Upvotes: 6

Views: 8702

Answers (3)

auphill
auphill

Reputation: 11

After some trial and error I found that I needed to:

  1. Write the environment vars to a var.properties file from the main "Execute shell" build step.
  2. Add an "Inject environment variables" build step reading from the same file. The step must be after the main shell step.
  3. Use it in Slack Notifications after both those steps are completed

Upvotes: 0

Thomas C. G. de Vilhena
Thomas C. G. de Vilhena

Reputation: 14575

Environment variables are acessible for building custom slack notifications. In my case I'm using variables from GitLab webhooks configured to trigger builds to customize slack messages:

So this message definition:

enter image description here

Results in this slack notification:

enter image description here

Upvotes: 1

Wanmi Siangshai
Wanmi Siangshai

Reputation: 142

You can do this:

slackSend channel: 'channel_name', message: 'My_Job - ' + BUILD_NUMBER + '\nMy_Report:' + JENKINS_URL

Upvotes: 1

Related Questions