How to find executions of a particular job? - circleci

In our project, we have a release-workflow workflow executing every time our master branch is updated, which includes an optional manually-triggered job called Production Deployment.
Now, the problem I have is that in order to see when was the last deployment of a specific microservice, I need to expand all workflows in CircleCI and search where the Production Deployment job was triggered and succeeded (green icon).
Is there a way to see all executions of Production Deployment job in one page? So, on this page, I'll see when was the last time this job was triggered/succeeded.
I guess, in theory, it would be something like /{project}/jobs/production_deployment
The only thing I was able to find is Job Legacy View, but it shows you all jobs in a project, instead of specific one.

You can't do that type of filtering in the UI (not to my knowledge). But the API is your friend.
You can use the "Recent Builds For A Single Project" CircleCI API v1 endpoint, combined with some jq magic. For example:
curl https://circleci.com/api/v1.1/project/:vcs-type/:org-name/:project?filter=successful -H "Circle-Token: <circle-token>" | jq '.[]|select (.workflows.job_name == "Production Deployment")|.build_url'

Related

Jenkins job submission via curl and get its build number

How to submit the Jenkins job using curl api and get its buid number ?
Note - i tried with the following api, but its not printing any build number.
curl -X POST http://<hostname>:8080/job/<jobname>/build
In general, you can't do this: when you submit (or "trigger") a Jenkins job, this will not necessarily create and start a new build.
When triggering a job, only a request for starting a new build will enter the build queue. Depending on the availability of suitable executors, the actual build will be created (and started) immediately, later, or never at all. Also, multiple queued requests will normally be "squashed", so several submits will result in the same build number.
Bottom line: this can be done (with the constraints mentioned before), but it will require additional (Groovy) scripting for tracking the submit request through the build queue until the build will be started by Jenkins.

one version number to unite them all

I have multiple build jobs for a project. ie:
projectA is built with different parameters, for SIT, UAT, Staging and Prod DC1, Prod DC2
I use the build ID within the code for cache busting JS and CSS files.
However, there is a little problem here.
I have multiple build IDs for Prod DC1 and DC2.
for example:
DC1: apple.com/me.js?v=45
DC2: apple.com/me.js?v=78
I need one id to unite them all. so that my apple.js?v=blah wont be different in DC1 and DC2. I am also using CDN so this might become a bigger problem.
How can I do this on jenkins?
If all Jobs are connected as Upstream/Downstream way, create a version label parameter in the first Job and pass this label as parameter to the next downstream job till the last Job.
You can use this as the Unique label from starting Job to last Job.
Use Build Name Setter Plugin to set the build name with the unique label for all the Jobs. So that it will be easy to identify the which build belongs to which label.
To have a full visibility of the Jobs use Delivery Pipeline Plugin

Jenkins plugin code that should excute before any kind of job is run in Jenkins

I am new to Jenkins plugin development. M trying to write a plugin that should be executed before any Multi configuration type job runs in Jenkins.
In this plugin I want to write rules that will check what configuration parameters user has selected while submitting the job, based on selected parameters, I want to decide whether to allow the job to run or to restrict it.
User should be shown reason as to why that job cannot be run in the Console Output.
Does anyone have any ideas which class I need to extend or which interface I need to implement in order to get a hook into Jenkins job run?
You could look at the Matrix Execution Strategy which allows for a groovy script to select which matrix combinations to run. I would think if your script threw an exception it would stop the build.
For background, the multi configuration projects run a control job (or flyweight) which runs the SCM phase then starts all the actual combinations. This plugin runs after the flyweight SCM checkout.
If nothing else, this will give you a working plugin to start from
Disclaimer: I wrote this plugin
Blocked queue job plugin was what I needed
Out of the box that plugin supports two ways to block the jobs -
Based on the result of last run of another project.
Based on result of last run of the current project
In that plugin the BlockQueueItemTaskDispatcher.java extends Jenkin's QueueTaskDispatcher providing us a hook into Jenkins logic to allow or block the jobs present in the queue from running.
I used this plugin as a starting point for developing a new plugin that allows us to restrict project based on parameters selected and the current time. Ultimate goal is to restrict production migrations from running during the day.
Overriding the isBlocked() method of QueueTaskDispatcher gave access to hudson.model.Queue.Item instance as an argument to me. Then I used the Item instance's getParams method to get access to build parameters selected by the user at runtime. Parsed the lifecyle value from it. Checked the current time. If lifecycle was Production and current time was day time then restricted the job by returning non null CauseOfBlockage from isBlocked() method. If that condition was false, then returnedCauseOfBlockage as null allowing the queued job to run.

Jenkins - Running concurrent jobs with "circular" parameter

I'd like to run several builds concurrently in Jenkins for the same job. I run at maximum 3 builds concurrently. I want each build to run with a parameter that must be unique from a pool for parameters. For instance, pool=[1, 2, 3]: The 1st build picks "1", 2nd picks "2" and the 3rd picks "3".
I must ensure that different builds can't pick the same parameter.
After building, the parameter is available again.
How can I do it?
Alternative: How can I count the number of builds running in this project and pass it as parameter?
At first, select the checkbox button named build-concurrently-if-neccesary to ensure the same job could build concurrently. you'd better read the help-html seriously before
The isolated environments for building different jobs make that data could not be shared each other in a simple way.
Here is a solution that trigger the buildWithParameters link by jenkins rest api to control the pool in the program procedure of your own.
add a string-parameter in job's config.
post the string parameter to http://$JENKINS_SERVER_URL/job/$JOB_NAME/buildWithParameters
Maybe it's the most convenient way if no available plugin found.
I found a plugin in github and asked the author to publish it. It works well and solves my problem.
Jenkins Parameter Pool Plugin

Select branch via URL param for Jenkins job build

I make builds in Jenkins many times a day and I would like to automate it a little bit, but still access it through web interface.
I'm already selecting job with URL (and building this URL with very simple Alfred workflow) but I would like to also select a branch – we have many branches in repo and 90% of time I want to select master of develop
jenkins.skypicker.com:8080/job/beta/build works great for selecting job, but is there a way to select branch, something like jenkins.skypicker.com:8080/job/beta/build?branch=origin/master?
You can, theoretically, use the Parameterized Build feature to define a parameter later to be used with GIT plugin configuration. Then you should be able to use the format of url like this:
http://jenkins.skypicker.com:8080/job/beta/buildWithParameters?BRANCH=origin%2Fmaster
Be careful with the special character in the branch name though. If you can avoid it by specifying a branch name in the form of origin/$BRANCH it would be safer...
Remember that in order to start the build process one must use POST method, not GET - just a side note...
Anser by #Łukasz-rżanek is right, just adding a few notes on which I've stumbled
Set this in Job configuration:
and run it
#!/bin/bash
JOB_NAME="Beta"
JOB_BRANCH=""
JOB_TOKEN="TOKEN"
JENKINS_URL="http://jenkins..."
crumb=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`
curl -H $crumb -X POST $JENKINS_URL/job/$JOB_NAME/buildWithParameters?token=$JOB_TOKEN

Resources