how modify copy of environment variable - from groovy job-dsl

1,153 views
Skip to first unread message

joshua...@pearson.com

unread,
May 7, 2018, 9:42:03 AM5/7/18
to job-dsl-plugin
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, 5:22:40 AM5/8/18
to job-dsl-plugin
AFAIS, the publisher section doesn't require to use the 'System.getenv()...' which refers to the job dsl context rather than the myfolder/myjob  job itself.

In that sense, you could simplify the groovy script with something like the below snippet

job("myfolder/myjob") {
    ...

    slackNotifier {
     room('#jenkins')
     notifySuccess(true)
     includeCustomMessage(true)
     customMessage("BUILD_URL=\${BUILD_URL} consoleURL=\${BUILD_URL}/console")
   }
...

Cheers

joshua...@pearson.com

unread,
May 8, 2018, 8:55:26 AM5/8/18
to job-dsl-plugin
Right, thank you very much Victor.
I just realized I over-simplified my use case. I not only want to append text at the end, but also replace text in the middle.
For some other reasons, I need to have JENKINS_URL set to http://jenkins-ui:8080 - thus BUILD_URL ends up being e.g. http://jenkins-ui:8080/job/myfolder/job/myjob/12/.
But for the link (in the notification to Slack) to be valid/reachable, I need to change "jenkins-ui:8080" to something such as "myjenkinsinstace.com" - is there a way to do that from my job-dsl groovy code?
thanks again, 
Josh

Victor Martinez

unread,
May 8, 2018, 9:47:03 AM5/8/18
to job-dsl-plugin
JOB_URL is built using the <JENKINS_URL>/job/<JOB_NAME>/<BUILD_NUMBER> format , therefore you could use each env variable and build your own URL, with something like the below snippet:




Although it might be a bit trick when using folders/subfolders as you might require a further tweak most likely, but I guess this is a good starting point to explore.

Cheers

joshua...@pearson.com

unread,
May 8, 2018, 10:22:38 AM5/8/18
to job-dsl-plugin
Thanks Victor, you are spot on again. That is what I resorted to - to manually piece together the build url for each job. 
What i'm looking for (what would be ideal / even better) is a way to modify BUILD_URL - so I don't have to manually do it for each job (because I do have various folders and levels/depths of folders, so the url is unique for each job - totally doable, just a bit of tedious maintenance).

Victor Martinez

unread,
May 8, 2018, 11:07:42 AM5/8/18
to job-dsl-plugin
That's a bit tricky since the the Slack custom message works with the default env variables and you cannot use temporary variables on the fly. 

You might use some groovy post build to somehow inject the value, a bit hacky but it might be worthy to give a go:



So the below snippet might work in your case, although I have not tested it out entirely but just the parameter and post build groovy section.

job("myfolder/myjob") {
  parameters {
      stringParam('JOB_URL', '', 'Workaround of hudson.model.ParametersAction.keepUndefinedParameter')
  }

  ...

  publishers {

    publishers {
        groovyPostBuild('''
          import hudson.model.*

          def build = Thread.currentThread().executable

          def env = System.getenv()
          def currentURL = env['JOB_URL']
          // REPLACE HERE WHAT'S THE TRANSLATION
          def parsedURL = currentURL?.replaceAll('jenkins-ui:8080', 'myjenkinsinstace.com') 
          def newParams = null

          def pl = new ArrayList<StringParameterValue>()
          pl.add(new StringParameterValue('JOB_URL', parsedURL))

          def oldParams = build.getAction(ParametersAction.class)

          if(oldParams != null) {
            newParams = oldParams.createUpdated(pl)
            build.actions.remove(oldParams)
          } else {
            newParams = new ParametersAction(pl)
          }

          build.addAction(newParams)
        ''', Behavior.MarkFailed)
    }
    slackNotifier {
      room('#jenkins')
      notifySuccess(true)
      includeCustomMessage(true)
      customMessage("BUILD_URL=\${JOB_URL}/console")
    }
  } // publishers
} // job


Cheers

joshua...@pearson.com

unread,
May 10, 2018, 11:03:51 AM5/10/18
to job-dsl-plugin
Thanks again Victor - you're most helpful.
just fyi, i ended up deciding to go with the following suggestion of yours (balance of quick/simple - even if I have to specify the URL for each job):
kind regards,

Josh
Reply all
Reply to author
Forward
0 new messages