How to Receive Jenkins Build Notifications in Stride

 jekins alerts.png

Before you start

For Jenkins freestyle projects see  "How to Receive Jenkins Build Notifications in Stride - Freestyle Projects" 

Copy the access token and conversation URL

  1. In your Stride room or direct message, click apps.png(Apps) in the sidebar, then click plus-add.png.
  2. Click Add custom app > API tokens.
  3. Give the token a name. We recommend Jenkins.
  4. Copy the access token and paste it in a safe place.
  5. Copy the conversation url and paste it into the same place.

Add Token and conversation url to Jenkins Credentials

  1. Open your Jenkins Web Portal
  2. Click Credentials in the sidebar 
  3. Click on the Credential Domain "System"
  4. Click Add Credentials
  5. Use these settings:

    FieldValue
    KindUsername and Password

    Scope

    Global
    UsernameConversation Url you saved (see the previous section)
    PasswordToken you saved (see the previous section)
    IdstrideConversationToken
    DescriptionAnything you want to describe this for future Jenkins administrators

Add Global Scripts to Jenkins

Open your Jenkins Web Portal. 

  1. Click Manage Jenkins.
  2. Click Managed Files.
  3. Click Add a New Config.
  4. Choose the Managed script file type.
  5. Enter the file name as the in the Id field. This makes it easier to reference in a Jenkinsfile if any of your projects using them. 
  6. Click Submit.
  7. Put a human-readable name in the Name field
  8. Paste in the contents of the script
  9. Submit the form
  10. Repeat these steps for each notification type wanted

notify_stride.success.sh

set +x -u

curl -X POST \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$STRIDE_TOKEN \
-d "{\"content\":[{\"attrs\":{\"collapsible\":true,\"context\":{\"icon\":{\"label\":\"Jenkins Build Server\",\"url\":\"https://jenkins.io/sites/default/files/jenkins_favicon.ico\"},\"text\":\"Jenkins Build Server - ${JOB_NAME} ${BUILD_DISPLAY_NAME} - BUILD SUCCEEDED\"},\"description\":{\"text\":\"Tag: ${BUILD_TAG}    Commit:${GIT_COMMIT}\"},\"details\":[{\"lozenge\":{\"appearance\":\"success\",\"text\":\"BUILD SUCCEEDED\"}},{\"lozenge\":{\"appearance\":\"success\",\"text\":\"${BRANCH_NAME}\"},\"title\":\"Branch\"},{\"lozenge\":{\"appearance\":\"success\",\"text\":\"${BUILD_DISPLAY_NAME}\"},\"title\":\"Build Number\"}],\"link\":{\"url\":\"${RUN_DISPLAY_URL}\"},\"text\":\"${JOB_NAME} - BUILD SUCCEEDED\",\"title\":{\"text\":\"${JOB_NAME} - BUILD SUCCEEDED\",\"user\":{\"icon\":{\"url\":\"https://jenkins.io/sites/default/files/jenkins_favicon.ico\",\"label\":\"BUILD SUCCEEDED\"}}}},\"type\":\"applicationCard\"}],\"type\":\"doc\",\"version\":1}" \
--url $STRIDE_CONVERSATION_URL

notify_stride.failure.sh

set +x -u

curl -X POST \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$STRIDE_TOKEN \
-d "{\"content\":[{\"attrs\":{\"collapsible\":true,\"context\":{\"icon\":{\"label\":\"Jenkins Build Server\",\"url\":\"https://jenkins.io/sites/default/files/jenkins_favicon.ico\"},\"text\":\"Jenkins Build Server - ${JOB_NAME} ${BUILD_DISPLAY_NAME} - BUILD FAILED\"},\"description\":{\"text\":\"Tag: ${BUILD_TAG}    Commit:${GIT_COMMIT}\"},\"details\":[{\"lozenge\":{\"appearance\":\"removed\",\"text\":\"BUILD FAILED\"}},{\"lozenge\":{\"appearance\":\"removed\",\"text\":\"${BRANCH_NAME}\"},\"title\":\"Branch\"},{\"lozenge\":{\"appearance\":\"removed\",\"text\":\"${BUILD_DISPLAY_NAME}\"},\"title\":\"Build Number\"}],\"link\":{\"url\":\"${RUN_DISPLAY_URL}\"},\"text\":\"${JOB_NAME} - BUILD FAILED\",\"title\":{\"text\":\"${JOB_NAME} - BUILD FAILED\",\"user\":{\"icon\":{\"url\":\"https://jenkins.io/images/angry-jenkins_128.png\",\"label\":\"BUILD FAILED\"}}}},\"type\":\"applicationCard\"}],\"type\":\"doc\",\"version\":1}" \
--url $STRIDE_CONVERSATION_URL

notify_stride.stage.sh

set +x -u

curl -X POST \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$STRIDE_TOKEN \
-d "{\"content\":[{\"attrs\":{\"collapsible\":true,\"context\":{\"icon\":{\"label\":\"Jenkins Build Server\",\"url\":\"https://jenkins.io/sites/default/files/jenkins_favicon.ico\"},\"text\":\"Jenkins Build Server - ${JOB_NAME} ${BUILD_DISPLAY_NAME} - STAGE: ${STAGE_NAME}\"},\"description\":{\"text\":\"Tag: ${BUILD_TAG}    Commit:${GIT_COMMIT}\"},\"details\":[{\"lozenge\":{\"appearance\":\"inprogress\",\"text\":\"${STAGE_NAME}\"}},{\"lozenge\":{\"appearance\":\"inprogress\",\"text\":\"${BRANCH_NAME}\"},\"title\":\"Branch\"},{\"lozenge\":{\"appearance\":\"inprogress\",\"text\":\"${BUILD_DISPLAY_NAME}\"},\"title\":\"Build Number\"}],\"link\":{\"url\":\"${RUN_DISPLAY_URL}\"},\"text\":\"${JOB_NAME} - STAGE: ${STAGE_NAME}\",\"title\":{\"text\":\"${JOB_NAME} - STAGE: ${STAGE_NAME}\",\"user\":{\"icon\":{\"url\":\"https://jenkins.io/sites/default/files/jenkins_favicon.ico\",\"label\":\"STAGE: ${STAGE_NAME}\"}}}},\"type\":\"applicationCard\"}],\"type\":\"doc\",\"version\":1}" \
--url $STRIDE_CONVERSATION_URL

Add Notifications to your Jenkinsfile

https://bitbucket.org/snippets/atlassian/geXnM8#file-Jenkinsfile.groovy - Example of how to use the scripts

SUCCESS NOTIFICATION 
pipeline {


  post{
        success{
            configFileProvider([configFile(fileId: 'notify_stride.success.sh', variable: 'NOTIFY_SCRIPT')]) {
		//This injects the credentials for posting to Stride as environment variables inside this code block
                withCredentials([usernamePassword(credentialsId: 'strideConversationToken', passwordVariable: 'STRIDE_TOKEN', usernameVariable: 'STRIDE_CONVERSATION_URL')]) {
                	//This makes the script executable and then executes it. $NOTIFY_SCRIPT is a env var to the scripts path
			sh 'chmod +x $NOTIFY_SCRIPT && sh $NOTIFY_SCRIPT'
                }
            }
        }
    }
}
FAILURE NOTIFICATION 
pipeline {


  post{
        failure{
            configFileProvider([configFile(fileId: 'notify_stride.success.sh', variable: 'NOTIFY_SCRIPT')]) {
		//This injects the credentials for posting to Stride as environment variables inside this code block
                withCredentials([usernamePassword(credentialsId: 'strideConversationToken', passwordVariable: 'STRIDE_TOKEN', usernameVariable: 'STRIDE_CONVERSATION_URL')]) {
                	//This makes the script executable and then executes it. $NOTIFY_SCRIPT is a env var to the scripts path
			sh 'chmod +x $NOTIFY_SCRIPT && sh $NOTIFY_SCRIPT'
                }
            }
        }
    }
}
STAGE NOTIFICATION 
pipeline {

  stages{
        stage('Build'){
            configFileProvider([configFile(fileId: 'notify_stride.stage.sh', variable: 'NOTIFY_SCRIPT')]) {
		//This injects the credentials for posting to Stride as environment variables inside this code block
                withCredentials([usernamePassword(credentialsId: 'strideConversationToken', passwordVariable: 'STRIDE_TOKEN', usernameVariable: 'STRIDE_CONVERSATION_URL')]) {
                	//This makes the script executable and then executes it. $NOTIFY_SCRIPT is a env var to the scripts path
			sh 'chmod +x $NOTIFY_SCRIPT && sh $NOTIFY_SCRIPT'
                }
            }
        }
    }
}

You can put the stage notification in any stage in your Jenkinsfile, or just make a new stage called 'build started'if you only want to know when a build kicks off.

 

14 comments

Samuel Lacaille-Moisan June 11, 2018

Does not work

???ℜ???
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 12, 2018

@Samuel Lacaille-Moisan, I'd love to help you through whatever isn't working. Could you tell me what is happening or what errors you're seeing?

Thanks,

Jon Ross

Samuel Lacaille-Moisan June 12, 2018

It is unclear whether we need to copy paste the shell as well as the pipeline scripts as is or change them in any sort of way.

???ℜ???
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 12, 2018

@Samuel Lacaille-Moisan

You will need to copy the contents of each shell script you want to use into a global script with the name as the ID for this to work. If you don't manually set the ID then Jenkins generates a GUID for it and you can't change it for that global script. You'd either need to create a new global script with the expected ID or have to modify the Jenkinsfile sections like so, replacing the fileId parameter value with the GUID for the script ID:

pipeline {

  stages{
        stage('Build'){
            configFileProvider([configFile(fileId: '<BIG UGLY GUID>', variable: 'NOTIFY_SCRIPT')]) {
  //This injects the credentials for posting to Stride as environment variables inside this code block
                withCredentials([usernamePassword(credentialsId: 'strideConversationToken', passwordVariable: 'STRIDE_TOKEN', usernameVariable: 'STRIDE_CONVERSATION_URL')]) {
                 //This makes the script executable and then executes it. $NOTIFY_SCRIPT is a env var to the scripts path
   sh 'chmod +x $NOTIFY_SCRIPT && sh $NOTIFY_SCRIPT'
                }
            }
        }
    }
}

If you followed the steps in Add Token and conversation url to Jenkins Credentials and used the filenames as Id's in step 5 of Add Global Scripts to Jenkins then you should be able to copy and paste the pipeline script sections into your Jenkinsfile. Jenkins is highly customizable and there might be something different about your server that I didn't know to account for. 

Is there any error messages you're getting during a build? That would help me better understand what might be going wrong in your case. 

Roger Firpo July 5, 2018

This is quite inconvenient, I was expecting an improvement wrt HipChat, not this.

Are there any plans to publish a proper Jenkins plugin to make notifications simple to setup?

 

Thanks,

Roger

Dinesh July 16, 2018

Error:

executing script 'notify_stride.failure.sh'
$ /bin/sh /tmp/build_step_template2565551543215353038.sh
/tmp/build_step_template2565551543215353038.sh: line 3: BRANCH_NAME: unbound variable
Dinesh July 16, 2018

Error:

executing script 'notify_stride.failure.sh'

$ /bin/sh /tmp/build_step_template2565551543215353038.sh

/tmp/build_step_template2565551543215353038.sh: line 3: BRANCH_NAME: unbound variable

Dinesh July 16, 2018

Error:

executing script 'notify_stride.failure.sh'

$ /bin/sh /tmp/build_step_template2565551543215353038.sh

/tmp/build_step_template2565551543215353038.sh: line 3: BRANCH_NAME: unbound variable

???ℜ???
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2018

@Dinesh

If your repository or setup doesn't have branches you will probably need to modify the script and remove the use of BRANCE_NAME. 

Remove this from the script and it should work. 

,{\"lozenge\":{\"appearance\":\"inprogress\",\"text\":\"${BRANCH_NAME}\"}




Dinesh July 17, 2018

@???ℜ??? . Thank you for your reply, I am using freestyle type project and figured it out, that I need to follow an additional step from your freestyle tutorial link. 

And I haven't noticed that I had posted my question multiple times, sorry for that.  I am waiting the official Jenkins plugin, hope it will be out soon. 

Kirill Chernousov October 30, 2018

Hiya.

Is it possible to extract stage name somehow?

Especially for failure notification....

Najma December 14, 2018

error:

Caused: java.io.IOException: Cannot run program "nohup" (in directory "C:\Users\Najma\.jenkins\workspace\BasicProjectPipeline"): CreateProcess error=2, Le fichier spécifié est introuvable
 at java.lang.ProcessBuilder.start(Unknown Source)
 at hudson.Proc$LocalProc.<init>(Proc.java:249)
Najma December 18, 2018

error:

+ chmod +x 'C:\Users\Najma\.jenkins\workspace\BasicProjectPipeline@tmp\config7844713355693667854tmp'
+ sh 'C:\Users\Najma\.jenkins\workspace\BasicProjectPipeline@tmp\config7844713355693667854tmp'
C:\Users\Najma\.jenkins\workspace\BasicProjectPipeline@tmp\config7844713355693667854tmp: line 3: GIT_COMMIT: unbound variable
Najma December 18, 2018

error 

Error when executing failure post condition:
hudson.AbortException: script returned exit code 1
 at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.handleExit(DurableTaskStep.java:534)
 at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.check(DurableTaskStep.java:480)
 at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.run(DurableTaskStep.java:426)
 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
 at java.util.concurrent.FutureTask.run(Unknown Source)
 at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
 at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events