I'm using Bitbucket Push and Pull Request plugin to trigger pipeline via webhook when pull request is open or updated in
BitBucket repo.
Now I want to send a mail notification to pull requestor about the pipeline execution.
Additionally to that I want to send same kind of notification when the PR is merged to the the requester and merger.
post {
always {
emailext body: 'Test Message',
subject: 'Test Subject',
to: '${prRecipient}'
}
}
My question is how to get the e-mail address of the requestor and set it as value prRecipient in the example of the code above?
BitBucket is a server, not Cloud!
I
was able to achieve this whit emailext plugin and recipientProviders like that:
post {
always {
emailext to: "default#mail.com"
body: 'A Test EMail',
recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider'], [$class: 'DevelopersRecipientProvider']],
subject: 'Test'
}
}
Related
I need a plugin where a team lead can approve if a pipeline can move to the next stage in Jenkins. I am planning to use multistage pipeline(declarative) so after dev stage I need a person to approve(only he can approve) and the developer folks can just run the job. Is there any such plugin available so that only one person can approve such request?
tried role based access plugin but here there is no ways to control segregate people who can do stage approvals
#sidharth vijayakumar, I guess you can make use of the 'Pipeline: Input Step' plugin.
As per my understanding, this plugin pauses Pipeline execution and allows the user to interact and control the flow of the build.
The parameter entry screen can be accessed via a link at the bottom of the build console log or via link in the sidebar for a build.
message
This parameter gives a prompt which will be shown to a human:
Ready to go?
Proceed or Abort
If you click "Proceed" the build will proceed to the next step, if you click "Abort" the build will be aborted.
Your pipeline script should look like some what similar to below snippet
// Start Agent
node(your node) {
stage('Checkout') {
// scm checkout
}
stage('Build') {
//build steps
}
stage('Tests') {
//test steps
}
// Input Step
{
input message: 'Do you want to approve if flow can proceeded to next stage?', ok: 'Yes'
}
stage('Deploy') {
...
}
}
Reference link : https://www.jenkins.io/doc/pipeline/steps/pipeline-input-step/
To solve the problem of role based approval i used input block with submitter. This means that person who listed as the submitter will only be able to give stage approvals.
stage('Approval') {
agent none
steps {
script {
def deploymentDelay = input id: 'Deploy', message: 'Deploy to production?', submitter: 'rkivisto,admin', parameters: [choice(choices: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'], description: 'Hours to delay deployment?', name: 'deploymentDelay')]
sleep time: deploymentDelay.toInteger(), unit: 'HOURS'
}
}
}
Please note there submitter must be a valid user. Use role based access plugin and create a user with relevant access.
Reference link for the script :
https://support.cloudbees.com/hc/en-us/articles/360029265412-How-can-I-approve-a-deployment-and-add-an-optional-delay-for-the-deployment
I would like to know how to make a parameter mandatory in Jenkins, so the "Build" button is unavailable until someone fills it.
[$class: 'hudson.model.StringParameterDefinition', defaultValue: 'master', description: 'Description', name: 'repositoryTag'],
I have this, and I was wondering if there is something I could do such as adding "required: True" for example.
I use the organization plugin to build the pullRequest of my github project.
During this build, I want to send a custom comment with some metric of the project to the github pullRequest.
How can I do it?
NeverMind, I found : (you need to install the http Request plugin)
def SHA1 = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
def body="""{
"body": "Nice change",
"commit_id": "$SHA1",
"path": "/",
"position": 0
}"""
httpRequest authentication: '${yourCredential}', httpMode: 'POST', requestBody: body, url: 'https://api.github.com/repos/${yourOrga}/${yourRepo}/issues/${pullRequestNumber}/comments'
Not allowed to comment, but elaborating on previous answer:
httpRequest authentication: '${yourCredential}', httpMode: 'POST', requestBody: body, url: 'https://api.github.com/repos/${yourOrga}/${yourRepo}/issues/${pullRequestNumber}/comments'
$yourcredential is a name that should match a credential of type Username and password. In github you should create a token and use this.
in a pullrequest you will usually will get the url (Genericwebhook) to the issue where you can post a comment as part of the webhook payload.
In my Jenkins multi pipeline project i am having a input step like this:
input message: 'Merge', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: "Merge ${branchConfig.merge} to ${env.BRANCH_NAME}?"]]
I am starting this job by calling this url:
http://user:api-token#awesome.jenkins.de/job/myTest/job/dev/build
Now I want to add a GET parameter like this:
http://user:api-token#awesome.jenkins.de/job/myTest/job/dev/build?skipInput=true
My question now is, how can I get this parameter in groovy?
UPDATE: Following the first comment, I did the following:
// Add parameter to skip MergeInput.
properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', name: 'skipMergeInput', defaultValue: false]]]])
And adjusted the input like that:
input message: 'Merge', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: params.skipMergeInput, description: '', name: "Merge ${branchConfig.merge} to ${env.BRANCH_NAME}?"]]
When I am now starting my job, it shows me a popup that ask for the value that should be set. But no matter what i decide, the input is always false. I am trying to figure out what is going wrong and will update my post then.
UPDATE 2:
So I kept on debugging. I added the following to my groovy script:
// Add parameter to skip MergeInput.
def doMerge = properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', name: 'doMerge', defaultValue: true]]]])
println doMerge;
The output returns me NULL, and when I am doing something like
println params.doMerge
It tells me that params is not defined. Any idea what is going wrong?
UPDATE 3:
Call URL: /job/dg_test/job/master/buildWithParameters?test=true
Groovy Script:
properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', name: 'test', defaultValue: false]]]])
println params.test
Result:
No such property: params for class: groovy.lang.Binding
I finally solved it, this post really helped me: https://stackoverflow.com/a/41276956/1565249
And this is my implementation:
// Add fancy build parameter.
properties([
parameters([
booleanParam(
defaultValue: false,
description: 'Some description',
name: 'developmentMerge'
),
])
])
if (developmentMerge == "true" || developmentMerge == true) {
// Your code here
}
else {
// Your code here
}
When I now start my job manually from the GUI, it asks me which value should be set for "developmentMerge".
And I also can start my job by calling this URL:
"/job/dg_test/job/master/buildWithParameters?developmentMerge=true"
Where "dg_test" is the name of my Jenkins project and "master" is the job i wanted to start.
The if statement must be done like this:
if (developmentMerge == "true" || developmentMerge == true)
because when you start the job from GUI, it will send a boolean "true", but when you start the job by the URL call, you receive a string.
This is achievable in 3 simple steps:
Set a boolean parameter for your pipeline build:
Use the "params." prefix to access your parameter in your input message step:
input message: 'Merge', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: params.skipInput, description: '', name: "Merge ${branchConfig.merge} to ${env.BRANCH_NAME}?"]]
Use the "buildWithParameters" api command rather than "build":
http://user:api-token#awesome.jenkins.de/job/myTest/job/dev/buildWithParameters?skipInput=true
I need include email content from html file using email-ext plugin in jenkins pipeline (my jenkins is 2.24 version), i try this
emailext (
subject: "some subject",
body: "${FILE,path="enteryPath/template.html"}",
to: "email#example.com"
)
but dont work for me :( any suggestions or solution?? , thanks advance.
You can use attachmentsPattern parameter.
emailext (
subject: "some subject",
body: "${FILE,path="enteryPath/template.html"}",
to: "email#example.com"
attachmentsPattern: 'enteryPath/template.html'
)
Can't you store the body in the workspace? And then just run:
emailext (
subject: "some subject",
body: readFileFromWS("enteryPath/template.html"),
to: "email#example.com"
)
But this would mean the body is static from job-creation and forward, guess you want it to be read when the mail should be sent?
stage ('email'){
steps{
script{
echo "hello"
emailext (subject: "some subject", body: '${FILE,path="template.html"}',to: "xyz#gmail.com")
}
}
}
Install the email-ext plugin for Jenkins and you can have the above code to send emails as part of the stage and path here would be relative to Jenkins workspace.
You could make use of readFile()
emailext (
mimeType: 'text/html',
to: "sample#email.com",
attachLog: true,
subject: 'Subject Line',
body: readFile("path/to/html/file")
)
readFile will read it as stream of strings and hands it over to emailext plugin, hence if you might have used any special variables such as
${BUILD_NUMBER}
${BUILD_LOG_REGEX}
will get compiled correctly and replaced with interpreted values.