Answer Jenkins Input from slack - jenkins

I am working currently on integration between Jenkins to Slack,
I want to fully control Jenkins from slack, basically, I want to trigger jobs, and I want to answer input if it exists.
for e.g
pipeline{
agent any
stages{
stage('Test Notification success stage'){
steps{
script{
env.createofflinepkg = input message: 'User input required',
ok: 'Submit',
parameters: [choice(name: 'Create Offline Package', choices: "Create\nSkip", description: 'Create Offline Package or Skip')]
}
slackSend (channel: 'input-response',color: '#ffff00', message: "Yellow at general : Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
}
I want this to be sent to slack and then I could answer this from slack, there is a way to do this?
Thanks in Advance.

I don't know if this is exactly what you are after. If you are sending ${env.BUILD_URL}/input URL to slack, that should redirect you directly to the approval page from Slack to Jenkins approval page.

Related

email on jenkins user input

I have a jenkins pipeline where I wait for user input to proceed or abort. Is there any way I can send the same in email to let the concerned person aware and he can click on the email to procceed Or abort the pipeline.?
It is possible to execute such action by using Jenkins API, this can be achieved using Jenkins Rest Api you can check this SO question.
I haven't tried this solution yet but I guess this pipeline could help you get a better idea of how I would think about implementing such behavior in a pipeline like this:
pipeline {
agent any
stages {
stage('Mail Notification') {
steps {
echo 'Sending Mail'
mail bcc: '',
body: 'Stop job through this link: ${env.BUILD_URL}/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/stop',
cc: '',
from: '',
replyTo: '',
subject: 'Jenkins Job',
to: 'example#domain.com'
}
}
}
}
Jenkins pipeline is aware of such variables like ${env.BUILD_URL} ${env.JOB_NAME} ${env.BUILD_NUMBER}

Office365ConnectorSend pipeline step does not work

I'm trying to configure Jenkinks notifications to MS Teams. I followed the instructions by setting up and configuring Jenkins app on the relevant channel and Office365 plugin in Jenkins. I get standard job status notifications if I request them.
Now I need to be able to send custom notifications from the pipeline. I was expecting that using office365ConnectorSend pipeline step would do just that:
office365ConnectorSend message:'Test message', webhoolUrl:'office365ConnectorSend message: 'Manual test', webhookUrl: 'https://outlook.office.com/webhook/.../JenkinsCI/...'
When the pipeline runs, everything is reported as working correctly and the job completes successfully, yet the message never appears in teams.
How can post a message?
office365ConnectorSend message:'Test message', webhoolUrl:'office365ConnectorSend message: 'Manual test', webhookUrl: 'https://outlook.office.com/webhook/.../JenkinsCI/...'
Did you check the spelling? it should be webhookUrl not webhoolUrl and only once.
I use something like this in the post pipeline action step, where MSTEAMS_HOOK is defined as an environment variable within the environment {} pipeline directive to the Teams URL.
success {
office365ConnectorSend (
status: "Pipeline Status",
webhookUrl: "${MSTEAMS_HOOK}",
color: '00ff00',
message: "Test Successful: ${JOB_NAME} - ${BUILD_DISPLAY_NAME}<br>Pipeline duration: ${currentBuild.durationString}"
)
}
Try to replace the single quote to double in the webhookUrl.
webhookUrl:"$msteams_url"
Except for the spell error, the script you have been trying works fine. The problem might be with your network restriction. The HTTP request triggered by jenkins might have been blocked. Try pasting the webhook URL in the browser of the system you are using and check the response. If the response says something other than 'Invalid webhook request - GET not supported'. There is a possibility the request has been a failure.
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
office365ConnectorSend message: 'Manual test', webhookUrl: 'https://outlook.office.com/webhook/*'
}
}
}
}

Implementation of Manual Approval in Continuous Delivery using Kubernetes and Jenkins

Currently I am trying to implement examples for both continuous delivery and deployment using Kubernetes and Jenkins. I have successfully implemented continuous deployment. Automatically, my REST API is deploying to my Kubernetes cluster via Jenkins. Both test and prod namespaces are deploying.
Now I am trying to implement continuous delivery by making a manual user approval before releasing to prod namespace. Means implement a manual approval by implementing one switch in between test and prod environments.
For more clarity I am adding here screenshots I got while I am exploring,
Continuous Delivery & Continuous Deployment Difference in Manual Approval
Here my confusion is that, when I am implementing the delivery, how I can add the user interaction? Do I need to change any parameter in my deployment.yaml or service.yaml? Or do I need to change anything when I am creating my Jenkins pipeline job in Jenkins UI?
I am new to the continuous delivery side. Can anyone suggest any documentation or tutorials or any method to explore please?
You can use the Jenkins Input Step to do something like this. Input step coupled with a try/catch will enable you fairly good control over success/failure of the job also.
The example below is from CloudBees support portal and uses the input box, captures the input and uses that input value to set the success/failure of the current build
def userInput
try {
userInput = input(
id: 'Proceed1', message: 'Was this successful?', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']
])
} catch(err) { // input false
def user = err.getCauses()[0].getUser()
userInput = false
echo "Aborted by: [${user}]"
}
node {
if (userInput == true) {
// do something
echo "this was successful"
} else {
// do something else
echo "this was not successful"
currentBuild.result = 'FAILURE'
}
}
We are using a shared library in Jenkins. We will pause the pipeline for 1 day and send mail to Approval DL mention in AD with the link. If not approval timely we will terminate deployment.
Approval has to login jenkins using the link to the approval of the deployment. (we thought about direct approval link but it is a security risk.)
timeout(time: 1, unit: "DAY")
{
log("Send an email to approvers#example.net with link)
sendEmail()
def inputResult = input (
id: "123",
message: "I approve this deployment",
)
}

Trigger Input Step in Jenkins via HipChat

I can find tons of information in the internet about integrating HipChat in a scripted Jenkins pipeline. So that works fine for me. But how can I post a status message to HipChat that contains an element to trigger an action back in Jenkins?
Currently I have input steps in Jenkins to approve a deployment to the next stage, e.g. PROD.
I also send a HipChat message if approval is needed which contains a link to JENKINS. That looks like this:
hipchatSend color: "${color}", notify: true, room: "Jenkins Team FooBar", message: "${env.JOB_NAME}#${env.BUILD_NUMBER}: ${message}", textFormat: true
/************** PROD **************/
stage ('Approval for PROD') {
try {
timeout(time: approvalTime, unit: approvalTimeUnit) {
input 'Do you approve the deployment to PROD?'
}
} catch (Exception ex) {
finishBuild = true
}
}
// Stop build if flag was set
if (finishBuild)
return
How can I define actions in that hipChat message? Is there a way that I can approve the next build step within HipChat?
I have not found a tutorial or documentation on how to define other kinds of hipchat messages with this plugin.
I could send a POST request to JENKINS if the message would contain standard HTML. Any ideas on how to do that?
How would it work with cards?
Thanks in advance.

How to invoke email-ext plugin from Jenkins declarative script?

I am writing a simple Jenkins declarative script to run 'make' and send an email with the result (success/failure).
I can send a simple email using:
post {
success {
mail to:"myname#me.com", subject:"${currentBuild.fullDisplayName} - Failed!", body: "Success!"
}
failure {
mail to:"myname#me.com", subject:"${currentBuild.fullDisplayName} - Failed!", body: "Failure!"
}
}
The resulting email is rather simplistic.
How can I call the email-ext plugin from the script to send an old-style post-build email? (I guess this should use email-ext's groovy-text.template).
I would like to be able to access lists like CulpritsRecipientProvider and to include the tail of the console log.
You can use it in this way:
emailext (
subject: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
For more information you can check:
Sending Notifications in Pipeline
Email Extension Plugin

Resources