How to trigger a Jenkins job using the p4 plugin or p4? - jenkins

I've installed this p4 plugin in a Jenkins job. This scynces a workspace to the latest change and then builds. Now, under "Build Triggers", I've chosen "Build periodically" and set the job to run every 10 mins. Is there a way do trigger this Jenkins job when a new submit comes in to P4? Do I use the plugin or do I do something with "p4 trigger"? If so, how can I do this?
Thank you!

Under the same Build Triggers section, there is an option named Poll SCM. Enable that option. This option is present by default when you install Jenkins. It does exactly what you're looking for. It will trigger build as soon as it detects a new commit in P4. Although not necessary, it's good to enable Quiet period too. This option is under Advanced Project Options section (refer snapshot below). Also read Help (?) that's provided with every option to gain better understanding of what it does.

This is how to set up p4 url triggers.
Generate your API key, click your username in the top right > click configure > then generate a new API token!
In your build configuration check the "Trigger builds remotely (e.g., from scripts)", then write whatever you want as the authentication token I wrote "MadeUpToken"
The next step is to run this curl command:
curl -X POST -L --user your-jenkins-username:11170e251c58b2768d4d26bc1db3d6395f https://Your-Jenkins-URL.com/job/Local_UE4/build?token=MadeUpToken

Related

Script to build a set of jobs, wait until they finish and write a report about them

I'm planning to write a script that would choose a set of Jenkins jobs with a regexp, start their builds and wait until they finish. If most of them fail, I want to change some global env vars (I already know how to do this) and build them again. Then I want to collect the test results and format it into a nice report.
I can't put this into the individual jobs' post-build actions, I need info about all of them to write the report and to do the rebuilding.
My current idea is to use the Jenkins REST API, but before I do so: is this already implemented somewhere?
Give a look at Multi-Job plugin and see if it fits your requirement.
Else go with REST API.
Because unique requirement of our build process I had to use jenkins cli :
java -jar jenkins-cli.jar -s http://<jenkinsURL> build <job-name> -s <parameters if any>
The trailing -s makes it wait till the job is complete.

Let Jenkins build project from a Mercurial commit

Is there a way to specify a hook in the single repository?
Now we have specified the hook in the "/etc/mercurial/hgrc" file, but every time it builds twice, and it builds for each commit in each repository.
So we want to specify a build per repository.
This is how we implemented the hook:
[hooks]
changegroup = curl --silent http://jenkins:8080/job/ourProject/build
It's on a Ubuntu server.
Select the Poll SCM option under Build Triggers.
Make sure that schedule form is empty.
You should be creating in the .hg directory, /home/user/mercurial/.hg/hgrc and add hooks as:
[hooks]
commit.jenkins = wget -q http://localhost:8080/mercurial/notifyCommit?url=file:///home/user/mercurial > /dev/null
incoming.jenkins = wget -q http://localhost:8080/mercurial/notifyCommit?url=file:///home/user/mercurial > /dev/null
You should make sure that
Your Jenkins project doesn't poll
You use the proper notifyCommit URLs for your Mercurial hooks: https://wiki.jenkins-ci.org/display/JENKINS/Mercurial+Plugin
Ok, I found what I looked for (I'm the bounty; my case is Mercurial with a specific branch).
In the main/origin repository, place a hook with your desired build script. Pregroupchange is to maintain the incoming changes. I have a rhodecode installed on the main repository and itself has its own hooks.
In this way, I still trigger Jenkins and still have the changes afther the trigger for rhodecode push notifications and others.
[hooks]
pregroupchange = /path/to/script.extention
In the script, place your desired actions, also a trigger for Jenkins. Don't forget to enable in Jenkins:Job:Configure:Build Triggers:checkbox Trigger builds remotely + put here your desired_token (for my case: Mercurial).
Because you can't trigger only to a specific branch in Mercurial, I found the branch name in this way. Also, to trigger from a remote script, you need to give in Jenkins read permission for anonymous overall, or create a specific user with credentials and put them into the trigger URL.
Bash example:
#!/bin/bash
BRANCH_NAME=`hg tip --template "{branch}"`
if [ $BRANCH_NAME = "branch_name" ]; then
curl --silent http://jenkins_domain:port/path/to/job?token=desired_token
fi
For the original question:
In this way you only execute one build, for a desired branch. Hooks are meant only for the main repository in case you work with multiple clones and multiple developers. You may have your local hooks, but don't trigger Jenkins from you local, for every developer. Trigger Jenkins only from the main repository when a push came (commit, incoming, and groupchange). Your local hooks are for other things, like email, logs, configuration, etc.

Options(Plugins) in build section work for post build in jenkins

In jenkins, we have a requirement like having the options that appear under build step like (say)
Execute Shell
Execute commands over ssh
Invoke Ant
To appear in the post build section. And it should do the same work as it did under build section.
I made all the options in build section appear in UI under post build section by doing it.getbuilddescriptors in config.jelly of my plugin. And it appeared in my jenkins UI under post build section as a hetero list.
But the problem is I don't know how to make it work as a bost build step.
For shell we did
Shell s = new Shell(command);
s.perform(build, listener, launcher);
and it worked.
If this is possible, then it may even be possible for all build section items. Is there a direct way to do without doing as I did for 'Execute shell'?
You may consider looking at https://wiki.jenkins-ci.org/display/JENKINS/Any+Build+Step+Plugin
They provide ability to use Build Steps as Post-Build actions, and vice-versa

Jenkins - execute a script before building, then have the user confirm to build

I have a bash script that looks like this:
#!/bin/sh
previousRelease=`git describe --tags --match "release*" origin/release`
git diff --name-status $previousRelease..origin/release
Is there a way of having Jenkins execute it as part of a build process? The intention is to see a list of files that have changed since the last release, as a manual step to confirm that the release should go up. The user who has triggered the build needs to read the output and then confirm the release should go ahead.
Most things are possible to do in Jenkins but if it is the best way of doing it is another question.
To solve this I would use an approach with two jobs one for checking the diff (hock that one on to the git repository) The other job for doing the actual release.
The check diff job
1 Create a job of the type freestyle project with build type "execute shell" and run your script above. Add some prints at the end of the log to create a clickable link to manually start the release job with current git-id as argument.
Just printing an URL in console output will make it clickable so:
export GITID=`git log -n| grep and sed or awk something`
echo http://jenkins.example.com:8888/job/releaseme/buildWithParameters?label=$GITID&parameters=build
will create the accept changes user interface you requested.
The release job
2 Create another job(above I assumed you named it releaseme) let the job have one parameter as argument (tick "This build is parameterized") make let the argument be the git-id you would like to release. Create your release script in this job.

Make jenkins auto build one a day but build only when there are source code changed

I have problem in configure jenkins to auto build and deploy java project. I want to build and deploy once a day. However this build only there are changes during the day. IF there is no changes, I don't want jenkins to auto build and deploy.
Note: that I used gitlab as source code management.
Can you help me with this configuration.?
Firstly, you should configure the Git SCM section at the top of the job config page to point to your GitLab repository.
Then you can use the built-in "Poll SCM" build trigger — this will periodically check whether your source code repository has changed — and if it has, a build of this job will be started.
If the repository has not changed since the last build, then no build will be started.
You can configure this trigger — whether using a cron-like syntax, or a shortcut like #daily or #midnight — so that Jenkins only checks the source repository once a day.
Additionally, you should make sure that the "ignore post-commit hooks" option is enabled. If you're using webhooks from your Git repository to start Jenkins jobs whenever a commit occurs, this option prevents your once-per-day job from being triggered for every Git commit.
Here's the detail document: "Jenkins CI integration"
http://doc.gitlab.com/ee/integration/jenkins.html
Update to match your comment.
You don't want to trigger the Jenkins build via webhook. It's ok.
You want to check the code change 1 time a day.
Example on Linux, build at 6:00 AM if there's any code change.
Install
https://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin
Build Triggers
Build periodically: 0 6 * * *
Execute shell
Like this
SINCE=`curl http://192.168.0.1:8080/job/MyJava/lastStableBuild/buildTimestamp?format=dd-MMM-yyyy`
cd /opt/code/myjava/
git log --pretty="%h - %s" --author=gitster --since=$SINCE --before=$SINCE --no-merges -- t/
Post Build actions
Post build task
Log text: commit
Operation: AND
Script: Your script to build your Java
Jenkins text finder
Also search the console output
Regular expression: Could not match
Unstable if found

Resources