Deploying jenkins jobs for dynamically created gitlab repos - jenkins

Setup:
1 Jenkins server and 1 gitlab server that I own
Expected usage:
- We provision repositories for users on gitlab using some scripts when a user signs up with our portal.
- As soon as these users submit some code in the repository, we have to run some junit tests on them and email the users with a the test results in a clean way where they can see the passed and failed results with appropriate links which they can click on to get details of the tests.
Requirements for. jenkins:
1. Ability to send html junit test results with clickable test result links
2. Based on whether the junit test fails or passes, call two different python scripts.
Questions:
1. What is the best way to go about 1 and 2 requirements
2. Should I create a job template and create a job per dynamically created repo or can I use same job to run tests on multiple repos
References :
These were the links we referred.But as a newbie to jenkins couldn't come up with clear solutions.
1.https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin#Email-extplugin-Jellycontent
2.https://gist.github.com/nightspotlight/7cfa6af7c7989857f336f742cdcb443b
3.https://learn2automate.blog/2017/05/19/how-to-set-jenkins-job-status-based-on-test-case-pass/

Send some links in your mail using jenkins is simple
Install https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin
Install https://wiki.jenkins.io/display/JENKINS/Build+With+Parameters+Plugin in order to expose a job as link with params
Create some job in jenkins and in the post-build section add editable email notification
[![enter image description here][1]][1]
in In Default Content of editable email notification you must write the email body that will be sent to your users. If your requirement is send a link to allow user to open another jenkins page, you could write the following :
In order to execute python scripts click in this link
http://your-jenkins.com/job/my-job-python/parambuild/?param1=test1&param2=etc
Your mail provider will render this text as html.
Finally you need to create some job with name my-job-python with parameters param1, param2 or whatever. This job will execute your python scripts when user clicks on email links sent by jenkins.

Related

how to use emailext-template in jenkins pipeline to get the template?

There is a good plugin, emailext-template. Basically, I do not have the access to Jenkins-server. It is not convenient to choose the option mentioned in emailext to create the email template.
I dont know how to use this emailext-template plugin to get the template in jenkins-pipeline. There is no guideline in emailext-template. Any documents?
// expected usage in jenkins-pipeline
template_str = emailext-template('template-id')
This can be done in the following way, In case you can deploy jenkins again (if it is done on the cloud), you could create and deploy your email template as part of jenkins installation and configuration.
In case you did not have access to the jenkins server even while deployment, you can just form the html template with the corresponding variables and then use them in the job configuration itself.
The below image shows how the template files are used in jenkins, Instead of the script tag as in the image, you can replace with your own html content so that jenkins will be able to send email with that content.

Jenkins Email notification for multiple builds

I need suggestion for Jenkins project (multi-conf or pipeline) and plugins that will fit my work.
I have 10 "flavors" of the product, so I must build 10 times every time I commit to the repository (all in the same workspace, run in sequentially). Today I have 10 jobs (freestyle) and a "master" job that trigger the rest. I tried to add Email notification (using Email Extension Plugin) but I want only one Email report for all the builds, not 10 Emails.
I understand that I should change to one multi-configuration project or one pipeline project that will handle all the builds, so it will be easier to trigger only one Email, but what is the best practice to get only one Email report on multiple builds?
This is the exact scenario which we can achieve using Pipeline job(Jenkinsfile) from which you can trigger all those freeStyle build-jobs in parallel and collect the build-url and build-status of those and store in some file, then use the email plugin in the post-build task to send the status of your complete flow.
You can use the following link to find how to access build variable post calling that inside your pipeline.
How to I get the url of build triggered with build step on Jenkins?

Storing Jenkins pipeline job metadata?

Is there a way where to store some metadata from Jenkins pipeline job, e.g:
We have a Jenkinsfile which builds a gradle project, creates docker image and pushes it to google cloud
Then a "Subjob" is launched which runs integration tests (IT) on that docker image. Subjob receives a couple of parameters (one of them - the generated docker image name)
Now sometimes that IT job fails, and I would like to re-run it from the main job view, so idealy:
we have a plugin which renders a custom button in blue ocean UI on the main job
By clicking that button a subjob is invoked again with the same parameters (plugin queries the jenkins api, get params of this job, and resubmits the subjob).
The problem ? How to get/set those parameters. I could not seem to find a mechanism for that, expect artifact storage. I could get away with that by creating a simple json/text file and uploading it as artifact, and then retrieving it in my plugin, but maybe there is a better way?
Stage restart is not coming to Scripted Pipelines so that does not look like ant option.
Maybe you can use the Jenkins API to get the details of the build?
https://your_jenkins_url.com/job/job_name/lastBuild/api/json?pretty=true
Instead of lastBuild you can also use the build number or one of lastStableBuild, lastSuccessfulBuild, lastFailedBuild, lastUnstableBuild, lastUnsuccessfulBuild, lastCompletedBuild
There is a parameters key there with all parameter names and values used in the build.
More details on https://your_jenkins_url.com/job/job_name/api/
Also, any reason you can't use the replay button in the IT job?

Setting Jenkins to email a build notification to the BitBucket user who pushed a branch

A project repository has been successfully connected to a Jenkins server using the BitBucket plugin, and a project set up such that:
Each push to a branch in BitBucket will trigger a webhook sent to the Jenkins server
When the Jenkins server receives the webhook it will build the changed branch (by specifying branch name as ** in the config)
After the build is complete a notification is sent back to BitBucket of the build status using the BitBucket notifier
Each of these has been easy to set up with just the instructions in the plugin and a few quick Googles. However I've now run into a problem which is maybe more a matter of wanting to run in an unconventional manner than anything else.
Using the normal emailer plugin or the Email-ext plugin it's possible to set emails to send to people involved in the creation of a build. For example the Email-ext plugin allows choice of:
Requester
Developers (all people who have commits in the build based off its last version)
Recipient list (a pre-set list)
Various "blame" settings for broken builds
The development process being followed involves each project being worked on by one developer in a named branch, e.g. userA/projectB. Obviously other developers could check that out and push to make changes but that's frowned upon. Even in that instance, the user who pushes the change to BitBucket should be notified.
None of the current settings support this. Requester is the closest, but that only works for manual builds. It seems a very simple requirement that the push to SCM that triggered a build should notify the user who pushed, but this is not documented anywhere that is easy to find.
After a lot of searching it seems the only way to accomplish this is by using a Pre-send script. This is added to the Advanced setting of the Email-ext post-build step, and takes the form of code written in Groovy which is a Java extension.
The script can take advantage of Environment variables, but is hard to test as there's no way to run the script with these in place. You can test simple Groovy scripts from Home -> Manage Jenkins -> Script console.
One important "gotcha" with the environment variables is that they are "included" in the script, rather than variables or constants. E.g. before the script compiles and runs, the content of the variable is pasted in place of its $NAME. In the example below the multi-line string syntax is used to include the BitBicket payload, whereas it might be expected that def payload = $BITBUCKET_PAYLOAD would simply work.
import javax.mail.Message.RecipientType
import javax.mail.Address
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
def bitbucket = jsonSlurper.parseText('''
$BITBUCKET_PAYLOAD'''
)
switch (bitbucket.actor.username){
case "userA":
msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("user.a#domain.com"));
break;
case "userB":
msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("user.b#domain.com"));
break;
}
The setRecipients command overwrites any existing recipient. Thus the recipient list or other email configuration should be set as a fallback for if the user is not recognised. Additionally, if there is nobody selected to send the email to, the script won't run at all. As added debugging, including the username in the body might help.
If the script fails, stack traces should be printed to the console log output of the test, and the build pass/fail shouldn't be affected, but the normal email address setup will be used instead. In stack traces look for lines with Script() in them, as that's the container which evaluates the Groovy script.

Provide userName and shortDescription in Jenkins remote API job triggering

I know how to provide build parameters:
wget --post-data='json={"parameter": {"name": "testparam", "value": "HELLO"}}' http://jenkins/job/Job1/build?delay=0sec
But, is it possible to provide a shortDescription and userName in a Jenkins remote API build request via wget/curl?
How should it look like in json or xml? Is there any manual/guidance on the net?
I will use this in along with the problem described in Trigger dynamic set of jobs. I want to provide triggered job with the calling job name and build number.
You may consider using Jenkins CLI (http://[jenkins-host]/cli for help in the browser). You can specify a user to a build CLI command. I'm not sure what you mean by short description when starting a build, though.
Update: Please see Jenkins Wiki Authenticating Scripted Clients. I've created a user foobar ('full name' Foo Bar) and tried the following:
wget --auth-no-challenge --http-user=foobar --http-password=[apiToken] http://jenkins.yourcompany.com/job/your_job/build
Where the token is obtained from user configuration page: http://localhost:8081/user/foobar/configure. It worked. The user has to exist, though. Also, you must specify --auth-no-challenge option, otherwise it kicks off the build as anonymous. The status description says Started by user Foo Bar.
Another Update If everything else fails, you may consider the following workaround: start all builds via the Parameterized Trigger Plugin with an additional boolean parameter that tells the triggered job whether to run or not. In case the job is asked not to run it would fail immediately and call a 'clean-up' job passing to it the build info; the clean-up job then will delete the build from the system.

Resources