Want to disable & enable jenkins pipeline to achieve automation - jenkins

I am working on a idea where I need to send a mail if certain conditions are met to inform the Team. The condition may continue for 30 minutes or 1 hour. But I do not want to flood the mailbox with continuous mails. As we will be scheduling the job every minute, the mails will be sent every minute. So we are trying to disable the pipeline for specific time once the mail is sent and enable it again automatically.
Thanks in advance.
I tried to put the job on sleep, but as it was scheduled to build every minute, the new jobs were initiated every minute and mails were sent.
//This is scheduled to build every minute * * * * *
`pipeline{
agent {
stages {
stage (check condition){
when
// check conditions
// if everything working fine - ok
else
// send email - this will be sent every minute. I want to avoid mailbox malfunction and just report the issue.
}
}
}
}`
Please help me with the solutions.

I used alternative: Sending below URL in the mail.
To disable the pipeline - $BUILDURL/disable to disable the pipeline.
The recipient will have to click on the link and disable the pipeline himself. This also means that the mail is acknowledged and has not gone unnoticed.
To enable the pipeline - $BUILDURL/enable. Once the issue is fixed, the recipient can enable the pipeline..
Edit:
Adding pipeline sample for reference.
pipeline{
agent any
stages {
stage ('Checkout') {
// checkout git repo
}
stage ('run job') {
// script to run the job
}
stage('email') {
when {
expression(condition to be satisfied)
}
steps {
send email
body - $Build_Url/disable to disable the job.
$Build_Url/enable to enable the job.
}
}
}

Related

Cant get job description using Jenkins slack notifcation plugin with pipeline script

I am using the slack notification plugin with Jenkins and I already set the notification settings in Jenkinsfile. I want to preview the job description on the Slack channel, and I am setting it on Jenkinsfile like this: Info: ${currentBuild.description} but I am getting on the slack channel Info: NUll although I already putting a description on the job.
Probably you are not setting the description properly. Log the description before the slack call and check whether you are getting the description correctly. Something like the one below works for me.
currentBuild.description = "This is a Description"
pipeline {
agent any
stages {
stage('Test') {
steps {
echo "${currentBuild.description}"
summary = "The Job ${currentBuild.description} worked"
slackSend(channel: channel, color: colorCode, message: summary)
}
}
}
}

Deleted Scheduled Messages still sending

I am building a slack application that will schedule a message when someone posts a specific type of workflow in a channel.
It will schedule a message, and if someone from a specific group of users replies before it has sent, it will delete the scheduled message.
Unfortuantely these messages are still sending, even though the list of scheduled messages is empty and the response when deleting the message is a successful one. I am also deleting the message within the 60 second limit that is noted on the API.
Scheduling the message gives me a success response, and if I use the list scheduled messages I get:
[
{
id: 'MESSAGE_ID',
channel_id: 'CHANNEL_ID',
post_at: 1620428096, // 2 minutes in the future for testing
date_created: 1620428026,
text: 'thread_ts: 1620428024.001300'
}
]
Canceling the message:
async function cancelScheduledMessage(scheduled_message_id) {
const response = await slackApi.post("/chat.deleteScheduledMessage", {
channel: SLACK_CHANNEL,
scheduled_message_id
})
return response.data
}
response.data returns { "ok": true }
If I use the list scheduled message API to retrieve what is scheduled I get an empty array []
However, the message will still send to the thread.
Is there something I am missing? I have the proper scopes set up and the API calls appear to be working.
If it helps, I am using AWS Lambda, and DynamoDB to store/retrieve the thread_ts and message IDs.
Thanks all.
For messages due in 5 minutes or less, chat.deleteScheduleMessage has a bug (as of November 2021) [1]. Although this API call may return OK, the actual message will still be delivered due to the bug.
Note that for messages within 60 seconds, this API does return an proper error code, as described in the documentation [2]. For the range (60 seconds, ~5 minutes), the API call returns OK but fails behind the scenes.
Before this bug is fixed, the only thing one can do is to only delete messages scheduled 5 minutes (the exact threshold may vary, according to Slack) or more (yes not very ideal and may not be feasible for some applications).
[1] Private communication with Slack support.
[2] https://api.slack.com/methods/chat.deleteScheduledMessage

How to send email with condition in editable email notification

I have job in jenkins with editable email notification post step, but email is sending every time when job is triggered. So question is how to send mail only once per day (lets say 08:00AM) and left othere job runs without email notification. Should condition be in 'pre-steps'?
thanks
I figured it out by adding such groovy code in 'pre-send' script
def mydate = new Date(System.currentTimeMillis())
Calendar cal = Calendar.getInstance();
cal.setTime(mydate);
int hour = cal.get(Calendar.HOUR_OF_DAY);
if( hour>7 && hour<9 ){
cancel=cancel}
else{
cancel=true}
So job will send email report only if it triggered in time from 7AM-9AM
cancel boolian stands for letting jenkins know if report should be send or not

Acting on post-failure only if the job has not regressed

I want to implement the following notifications in my declarative pipeline:
(regression) → Job has started to fail!
(failure) → Job is still failing.
(fixed) → Job has resumed.
It doesn't seem like post's conditions can do this. Instead, when a build fails, both failure and regression trigger. There's a changed condition, but I think what this task wants is unchanged { failure }, which doesn't exist.
post {
regression {
slackSend message: 'Job has started to fail!'
}
failure {
slackSend message: 'Job is still failing.'
}
fixed {
slackSend message: 'Job has resumed.'
} // no need for 'success' as we don't want a notification while it's working
}
The above config will send the following notifications, if the job previously succeeded:
(job fails)
Job has started to fail!
Job is still failing.
(job fails)
Job is still failing.
(job succeeds)
Job has resumed.
There's that extra "Job is still failing" message when the job first starts to fail. Is there a way around this using post conditions?
Alternatively, how can I implement this behaviour another way? I've made a pass at post { always { // decide which message is appropriate } }, but haven't figured out how to identify the previous build's status.

Twilio - Connecting an Enqueued call to an Agent

I'm trying to create a phone system where a caller gets enqueued, and ideally, the system will then call out to an agent, who would then pickup and then modify the call to bridge the top of the queue.
What I've accomplished thus far is the dialing loop, where a user calls in, and it dials agents in sequence, until someone picks up, or gives the user the option to leave a message or stay on the line while hearing it ring. And a simple enqueue with hold music.
I just can't seem to figure out how to combine these two systems.
The closest I've found is this post, and it's helpful, but it glosses over how to call out once the caller is enqueued.
Unfortunately, the only Twilio documentation I've found thus far tells me how to dial into the queue, which isn't what I want out of this system. I want this system to place a caller in a queue with hold music, while the system then dials agent numbers until an agent picks up.
Any and all help is much appreciated.
Thanks.
Edit:
Solution
index.php
This is the general IVR tree that the caller initially hits.
<Say>This hits your general IVR tree</Say>
<Say>As the last action, since the caller hasn't pressed anything and should be enqueued, redirect the caller to EnqueueCaller.php</Say>
<Redirect>./EnqueueCaller.php</Redirect>
Since PHP is a preprocessor, there's no real way to sleep or timeout the dialing of the call. The redirect in the IVR tree is necessary so the Agents aren't being dialed when the user is still in the IVR tree.
EnqueueCaller.php
This is where the Caller gets redirected once the IVR tree has finished and the user has chosen to wait for an agent. The call actually happens before the Enqueue, since PHP loads first before the TwiML xml is read (I think?). But since there's an inherent delay when calling, the caller will always be enqueued before an agent can pick up (I hope).
<Enqueue waitUrl="wait_file.xml">name_of_queue</Enqueue>
$call = $client->account->calls->create($from, $to, "http://example.com/DialQueueHandler.php", array( "StatusCallback" => "DialQueueEventHandler.php" );
DialQueueHandler.php
This simply bridges the agent and whoevers at the top of the queue.
<Say>Connecting to caller now.</Say>
<Dial><Queue>name_of_queue</Queue></Dial>
DialQueueEventHandler.php
This script houses the logic for what happens when the dialed agent state changes (answered, complete, initiated, ringing) from $_REQUEST['CallStatus']. In my case, I dialed a single agent from the enqueue script, and in this script, either continue dialing the next agents via setting of a flag.
switch($_REQUEST['CallStatus'] {
case 'answered':
case 'completed':
$next = false;
break;
default:
$next = true;
break;
}
if($next) { $call = $client->account->calls->create($from, $nextAgentNumber, "http://example.com/DialQueueHandler.php", array( "StatusCallback" => "DialQueueEventHandler.php?agentOffset=$num" ); } //same line from EnqueueCaller.php, and track where we are in agent array.
If the call is not answered or completed, then dial the next agent. Otherwise when the call is picked up by an agent, the DialQueueHandler.php file gets hit and the call becomes bridged.
Jeff, I'm Megan from Twilio.
You can utilize the workflowSid attribute of <Enqueue> to configure a Task which initiates the call flow to an available agent using TaskRouter. There is a TaskRouter quickstart in PHP and I think given where you've gotten so far, you could pick up on the third part.
Let me know if you find this to be helpful.

Resources