how get copy of environment variable - groovy job-dsl

274 views
Skip to first unread message

joshua...@pearson.com

unread,
May 4, 2018, 4:47:55 PM5/4/18
to Jenkins Users
hello,

I have the slack & job-dsl plugins installed & configured in my jenkins instance.

I run the seed job for the following my-slack.groovy file:
job("myfolder/myjob") {
  steps {
      shell("""#!/bin/bash
      echo "hello"
      """)
  }

  publishers {
    // See https://issues.jenkins-ci.org/browse/JENKINS-34124?focusedCommentId=253825 for an e.g. of how to call 'slackNotifier'. And the
    slackNotifier {
      room('#jenkins')

      notifySuccess(true)

      includeCustomMessage(true)
      String buildURL = System.getenv("BUILD_URL")
      String otherBuildURL = "\$BUILD_URL"
      customMessage("BUILD_URL=\$BUILD_URL buildURL={$buildURL} otherBuildURL=${otherBuildURL}")
    }
  } // publishers
} // job

(Note that BUILD_URL is listed on the jenkins Available Environment Variables page.)

When I run the job, the notification I receive in my Slack channel has the following text: 
BUILD_URL=http://jenkins-ui:8080/job/myfolder/job/myjob/12/ buildURL=null otherBuildURL=http://jenkins-ui:8080/job/myfolder/job/myjob/12/

But what I would like to do is (when myjob is run - not when the seed job is run) be able to get a copy of BUILD_URL and edit the copy and include that in customMessage. Is there any way to do that? (e.g. perhaps with the Scriptler Plugin?)

e.g. when I add the following code to my-slack.groovy (right after the line where otherBuildURL is initially declared):
otherBuildURL += "console"
then I get this in the Slack notification:
otherBuildURL=$BUILD_URLconsole

To sum this up: what I'm going for is whenever myjob is run, I would like the Slack notification text to include a link to the console output - e.g.: 
http://jenkins-ui:8080/job/myfolder/job/myjob/12/console

(Please let me know if I should instead direct this post somewhere else - e.g. the job-dsl-plugin group.)

Any suggestions would be greatly appreciated. thanks!

Victor Martinez

unread,
May 8, 2018, 7:39:23 AM5/8/18
to Jenkins Users
Just in case, this thread is already followed up in the job-dsl-plugin mailinglist:
Reply all
Reply to author
Forward
0 new messages