I had set-up notifications via Microsoft Teams for my jenkins job - success, failure, abort, etc.
pipeline {
options {
office365ConnectorWebhooks([[
startNotification: true,
notifySuccess: true,
notifyFailure: true,
notifyAborted: true,
notifyBackToNormal: true,
url: 'webhook_url'
]]
)
} }
With the help of above script i am receiving notifications for all except the failure notifications.
Even i aborted the job i am receiving the notification.
Can anyone help on this issue ?
You can define a notification step regardless of the pipeline completion status using the post section and the always condition like the following:
pipeline {
agent any
stages {
stage('Test notification') {
steps {
echo "Let's simulate a failure"
error('Failing the build.')
}
}
}
post {
always {
echo 'I will always run!'
office365ConnectorSend status: currentBuild.currentResult, webhookUrl: 'webhook_url'
}
}
}
Note that the syntax has changed.
To learn more about the plugin usage:
Office 365 Connector plugin
Office 365 Connector steps
And about the Jenkins post usage:
Jenkins Pipeline Syntax
Related
We have 2 jenkins job AA and BB.
Is there a way to allow BB only to be trigger from AA after it completes?
Basically, you can use build triggers to do this. More about build triggers can be found here. There are two ways to add build triggers. Following is how you can add triggers using the UI. By going to Configure Job you can add triggers. Please refer to the following image.
In a declarative pipeline, you can add triggers as shown below.
pipeline {
agent any
triggers { upstream(upstreamProjects: 'AA', threshold: hudson.model.Result.SUCCESS) }
stages {
stage('Hello') {
steps {
echo 'Hello World BB'
}
}
}
}
Here you can specify the threshold on when to trigger the build based on the status of the upstream build. There are 4 different thresholds.
hudson.model.Result.ABORTED: The upstream build was manually aborted.
hudson.model.Result.FAILURE: The upstream build had a fatal error.
hudson.model.Result.SUCCESS: The upstream build had no errors.
hudson.model.Result.UNSTABLE: The upstream build had an unstable result.
Update 02
If you want to restrict all other jobs/users from triggering this job you will have to restructure your Job. You can wrap your stages with a parent Stage and conditionally check who triggered the Job. But note that the Job will anyway trigger but the stages will be skipped. Please refer the following pipeline.
pipeline {
agent any
triggers { upstream(upstreamProjects: 'AA', threshold: hudson.model.Result.SUCCESS) }
stages{
stage('Parent') {
// We will restrict triggering this Job for everyone other than Job AA
when { expression {
print("Checking if the Trigger is allowed to execute this job")
print('AA' in currentBuild.buildCauses.upstreamProject)
}
}
stages {
stage('Hello') {
steps {
echo 'Hello World BB'
}
}
}
}
}
}
I have a multibranch job in my jenkins, what I have a webhook setup from my github to my jenkins that send every pull request changes and issue comments.
What I'm trying to do is let github send the pull request changes for indexing purposes, but don't run the job, unless the developer add comment 'test' in the comment on the github pull request.
This is my Jenkinsfile,
pipeline {
agent { label 'mac' }
stages {
stage ('Check Build Cause') {
steps {
script {
def cause = currentBuild.buildCauses.shortDescription
echo "${cause}"
}
}
}
stage ('Test') {
when {
expression {
currentBuild.buildCauses.shortDescription == "[GitHub pull request comment]"
}
}
steps {
sh 'bundle exec fastlane test'
}
}
}
}
So I want if the trigger isn't GitHub pull request comment, don't run anything. I've tried this but it doesn't work. I've tried print currentBuild.buildCauses.shortDescription variable and it prints [GitHub pull request comment], but the job still won't run with my when expression
How can I do this? Thanks
So actually the problem is because currentBuild.buildCauses.shortDescription return ArrayList instead of plain String.
I didn't really think this was meant an array [GitHub pull request comment], so I manage to fix the issue with just array index.
currentBuild.buildCauses.shortDescription[0]
This return the correct build trigger GitHub pull request comment. So for anyone who also stumbles to this issue, this is how I fixed it
pipeline {
agent { label 'mac' }
stages {
stage ('Test') {
when {
expression {
currentBuild.buildCauses.shortDescription[0] == "GitHub pull request comment"
}
}
steps {
sh 'bundle exec fastlane test'
}
}
}
}
im trying to post Jenkins job script output to slack notification: but i cant able to access the output in slack notification setting.
other than /env-vars.html/ the variables here i can't able to access any other variable.
At the moment there is no support to get the variables other than env vars in Jenkins. We can use "Environment Injector" plugin but I am not sure about that plugin.
For your case you can create a "Pipeline" with the below scripted pipeline
node('JENKINS_NODE') {
git([url: 'GITHUB_REPO_URL', branch: 'BRANCH'])
def getResult
stage ('Execute Script') {
getResult = sh(
script: "python test.py",
returnStdout: true,
)
}
stage ('Send Slack Notification') {
slackSend channel: '#YOUR_SLACK_CHANNEL', color: 'good', message: getResult'
}
}
I have a Jenkins pipeline using Groovy to send the status of the builds via emails when it is failure or unstable. But when i cancel the build manually in Jenkins, the email still send because it take that as a failure. How can i make Jenkins not to send emails in this situation?
You can play with the FlowInterruptedException, for example, my not graceful, but working solution:
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
node(){
stage("doing things"){
sendEmailflag = true
try{
echo "into try block"
sleep 10
}
catch(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
sendEmailflag = false
echo "!!!caused error $e"
throw e
}
finally{
if(sendEmailflag == false)
{echo "do not send e-mail"}
else
{echo "send e-mail"}
}
}
}
this is based on:
Abort current build from pipeline in Jenkins
How to catch manual UI cancel of job in Jenkinsfile
Catching mulitple errors in Jenkins workflow
Can I use slackSend command in jenkins flow dsl if i have Jenkins ver. 1.656.
I have enabled Slack Notification Plugin and it works fine in most cases, but i wish to display message when build starts.
You can set up script in the pipeline, should be something like this:
def notify(status) {
slackSend channel: "#jenkins",
color: '#d71f85',
message: "${status}",
tokenCredentialId: 'yourtoken'
}
pipeline{
....
stages{
stage('Buildstart) {
steps {
notify("Build Started")
}
}
....
}
}