Jenkins Manual email approval before deployment - jenkins

I am trying to build a logic for our UAT team to deploy code only upon approval from manager . I would like to know how can be achieved ? Email approval ?. Please help

You can try something like this
//this will grab user - who is running the job
def user
node {
wrap([$class: 'BuildUser']) {
user = env.BUILD_USER_ID
}
emailext mimeType: 'text/html',
subject: "[Jenkins]${currentBuild.fullDisplayName}",
to: "user#xxx.com",
body: '''click to approve'''
}
pipeline {
agent any
stages {
stage('deploy') {
input {
message "Should we continue?"
ok "Yes"
}
when {
expression { user == 'hardCodeApproverJenkinsId'}
}
steps {
sh "echo 'describe your deployment' "
}
}
}
}
you can also get some more help from this StackOverflow question

Related

java.lang.UnsupportedOperationException: no known implementation of class jenkins.tasks.SimpleBuildWrapper is named BuildUser in jenkins

I preparing script in Jenkins as below where I getting error while build job. This job is send email to user for input for next step.
[Pipeline] End of Pipeline
java.lang.UnsupportedOperationException: no known implementation of class jenkins.tasks.SimpleBuildWrapper is named BuildUser
at org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:549)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:473)
...
...
Version :
$ java --version
openjdk 11.0.11 2021-04-20
jenkins : 2.277.3
Pipeline code:
def user
node {
wrap([$class: 'BuildUser']) {
user = env.BUILD_USER_ID
}
emailext mimeType: 'text/html',
subject: "[Jenkins]${currentBuild.fullDisplayName}",
to: "user#xxx.com",
body: '''click to approve'''
}
pipeline {
agent any
stages {
stage('deploy') {
input {
message "Should we continue?"
ok "Yes"
}
when {
expression { user == 'hardCodeApproverJenkinsId'}
}
steps {
sh "echo 'describe your deployment' "
}
}
}
}
Can anyone please review this, please ?
You need to install the build user vars plugin

Conditional post section in Jenkins pipeline

Say I have a simple Jenkins pipeline file as below:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh ...
}
}
stage('Build') {
steps {
sh ...
}
}
stage('Publish') {
when {
buildingTag()
}
steps {
sh ...
send_slack_message("Built tag")
}
}
}
post {
failure {
send_slack_message("Error building tag")
}
}
}
Since there's a lot non-tag builds everyday, I don't want to send any slack message about non-tag builds. But for the tag builds, I want to send either a success message or a failure message, despite of which stage it failed.
So for the above example, I want:
When it's a tag build, and stage 'Test' failed, I shall see a "Error building tag" message. (This is a yes in the example)
When it's a tag build, and all stages succeeded, I shall see a "Built tag" message. (This is also a yes in the example)
When it's not a tag build, no slack message will ever been sent. (This is not the case in the example, for example, when the 'Test' stage fails, there's will be a "Error building tag" message)
As far as I know, there's no such thing as "conditional post section" in Jenkins pipeline syntax, which could really help me out here. So my question is, is there any other way I can do this?
post {
failure {
script {
if (isTagBuild) {
send_slack_message("Error building tag")
}
}
}
}
where isTagBuild is whatever way you have to differentiate between a tag or no tag build.
You could also apply the same logic, and move send_slack_message("Built tag") down to a success post stage.
In the postbuild step you can also use script step inside and use if. And inside this if step you can add emailext plugin.
Well, for those who just want some copy-pastable code, here's what I ended-up with based on #eez0's answer.
pipeline {
agent any
environment {
BUILDING_TAG = 'no'
}
stages {
stage('Setup') {
when {
buildingTag()
}
steps {
script {
BUILDING_TAG = 'yes'
}
}
}
stage('Test') {
steps {
sh ...
}
}
stage('Build') {
steps {
sh ...
}
}
stage('Publish') {
when {
buildingTag()
}
steps {
sh ...
}
}
}
post {
failure {
script {
if (BUILDING_TAG == 'yes') {
slackSend(color: '#dc3545', message: "Error publishing")
}
}
}
success {
script {
if (BUILDING_TAG == 'yes') {
slackSend(color: '#28a745', message: "Published")
}
}
}
}
}
As you can see, I'm really relying on Jenkins built-in buidingTag() function to help me sort things out, by using an env-var as a "bridge". I'm really not good at Jenkins pipeline, so please leave comments if you have any suggestions.

taking user input from user jenkins

i need to take some variables from user as input in my jenkinsfile. For this purpose in have written following code.
pipeline {
agent any
stages {
stage ('Take Input') {
steps {
script {
def INPUT_PARAMS = input message: 'Please Provide Parameters', ok: 'Next',
parameters: [
input(name: 'ENVIRONMENT', description: 'Please select the Environment')]
}
}
}
stage('clone repository') {
steps {
checkout scm
}
}
}
}
In case of choices this code is working is fine but i need user to type his answer in textbox. But i upon running job i am getting following error.
please can someone help me achieving this.

Get error reason in Jenkinsfile failure

I have the following post failure section:
post {
failure {
mail subject: "\u2639 ${env.JOB_NAME} (${env.BUILD_NUMBER}) has failed",
body: """Build ${env.BUILD_URL} is failing!
|Somebody should do something about that""",
to: "devel#example.com",
replyTo: "devel#example.com",
from: 'jenkins#example.com'
}
}
I would like to include the reasons why the build failed in the body of the error message.
How can I do that?
If not, is there a way to attach the build log file to the email?
I don't know of a way to retrieve the failure reason automatically out of thin air.
However, you can use "post{ failure {" blocks in each phase to capture at least the phase in which it failed into a environment variable (e.g. env.FAILURE_REASON), and access that env var in the final (global scope) notification block.
For more granularity, you can reuse the same mechanism of the global env variable, but use try { } catch { } blocks to capture which specific step failed.
A generic example would be:
pipeline {
stages {
stage('Build') {
steps {
...
}
post {
failure {
script { env.FAILURE_STAGE = 'Build' }
}
}
}
stage('Deploy') {
steps {
...
}
post {
failure {
script { env.FAILURE_STAGE = 'Deploy' }
}
}
}
...
}
post {
failure {
mail subject: "\u2639 ${env.JOB_NAME} (${env.BUILD_NUMBER}) has failed",
body: """Build ${env.BUILD_URL} is failing in ${env.FAILURE_STAGE} stage!
|Somebody should do something about that""",
to: "devel#example.com",
replyTo: "devel#example.com",
from: 'jenkins#example.com'
}
}
}
Technically, you can even do some automated triage based on the failing stage and send a more targeted notification, or even create specific (e.g. Jira) tickets.
For attaching the console log to the email notification, you'd want to look at
emailext and the 'attachLog: true' attribute

Not receiving email notifications from pipeline runs

I am having problems receiving email notifications for a pipeline I have setup like so:
#!groovy
pipeline {
agent any
stages {
stage('Build Prep') {
steps {
sh '...'
}
}
stage('Build') {
steps {
sh '...'
}
}
stage('Tests') {
steps {
parallel (
"Jasmine": {
sh '...'
},
"Mocha": {
sh '...'
}
)
}
}
stage('Deploy') {
steps {
sh "..."
}
}
}
post {
success {
emailext to:"me#me.com", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay, we passed."
}
failure {
emailext to:"me#me.com", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed."
}
unstable {
emailext to:"me#me.com", subject:"UNSTABLE: ${currentBuild.fullDisplayName}", body: "Huh, we're unstable."
}
changed {
emailext to:"me#me.com", subject:"CHANGED: ${currentBuild.fullDisplayName}", body: "Wow, our status changed!"
}
}
}
The build logs confirm an email is sent, but I dont get anything in my inbox, nothing in spam either.
I have come across this https://jenkins.io/blog/2016/07/18/pipline-notifications/
But I am unsure if I can use using the syntax I have, I don't have any nodes defined, should I?
Your script works fine. I did receive the email using the above code. Please check your other settings or if you had created a rule for Jenkins emails. You can make your code look easier by adding your notifications to the Jenkins shared library. Please take a look at the below link.
https://jenkins.io/doc/book/pipeline/shared-libraries/

Resources