Jenkins run without build - jenkins

I have made a simple program that needs to be executed once in every hour. I've set in Jenkins to run every hour and run shell commands at post step of the build.
The thing is that it needs to build only when the code is changed on GIT. If nothing has changed, I'd just like to run the post steps without the new build.
Is there any way to take care of this? I just can't find any plugins to take care of this.
git change -> build -> run post step.
git not change -> build X -> run post step every hour
Thanks in advance.
-edit-
This simple program is currently running on my Jenkins every hour as below.
trigger(every hour) -> get code from Github -> build -> run post step(executing three shell commands)
But! I want to change to
trigger(every hour) -> get code from Github -> build -> run post step(executing three shell commands)
+
trigger(when Github code changes) -> get code from Github -> build -> run post step(executing three shell commands)

If I understand your requirements correctly, you can set your job to build on a Git push to master (or whatever branch) using a Git hook while also keeping the hourly build and add a check in your code for the build cause. Something like (this is inside an execute shell step):
if [ $BUILD_CAUSE != 'TIMERTRIGGER' ]; then
build your code
fi
execute 3 commands
That way builds triggered by the timer will skip the code build but manual or hook triggers will not.
If you are using pipeline you can use currentBuild.rawBuild.getCauses()[0].getShortDescription().contains('text identifying git trigger') for similar results.
Edit: an alternative solution using the Conditional BuildStep plugin.
Add a Conditional Step (single) with the trigger as build cause SCM Trigger as the first step on the build and add a second build step to run the shell commands. See attached screenshot (in the image the Post build Actions tab is highlighted but this is done in the Build section).

Related

How to stop a build by buildnumber in a Jenkins pipeline

We uses notes (comments) from Gitlab to issue a couple of user-commands, for example retry a build that seems to have failed from a failed integration etc. We would now like to ask jenkins to stop a build. We have got the buildnumber from the comment (through a callback to to gitlab searching the comments) so we know what build to stop but now we stumbled on the problem. We don't seem to find an api-call to stop the build by buildnumber alone.
We could of course make a request to https://server/project/buildnumber/stop (same url as used by the ui) but then we have to enable the crumbIssuer and according to Ops open for a CSRF-attack.
Is there a way to do this operation from inside a pipeline?
Navigate to Manage Jenkins > Script Console.
Run the following script setting the job name and number of the hung build accordingly:
def build = Jenkins.instance.getItemByFullName("jobName").getBuildByNumber(jobNumber)
build.doStop()
build.doKill()
Otherwise you can create a separate pipeline job and configure the above script
Abort the job build from a cid node (if the previous option did not help):
Log in to any cid node.
Run:
cd /srv/volumes/jenkins/jobs/<job-name>/builds/
rm -rf <hung-build-number>

Previous Build Number in Jenkins

I have a batch Job (not the pipeline Job) in Jenkins where I am using a plugin called "Naginator" which will check if the current build is failed/unstable - If the current build is failed/unstable it will run immediately the next build to confirm the error is genuine. If the error is not genuine then the run is successful and it will periodically set the next build to run. Along with this, I use some CMD commands to move my data into another folder with the Build number as my folder name.
Now here is the issue, if the previous build was unstable and Naginator runs the next build and that build is stable then what I need to do is delete the previous unstable build data from the folder manually. Is it possible to fetch the previous build number in Jenkins so that I can delete the file in an automated way - lets say in CMD Commands .BAT file.
Jenkins has it's own global variables. You can check whem in your pipeline job -> Pipeline Syntax -> Global Variables Reference.
Additionally check http://jenkins_url:port/env-vars.html/
For your purpose BUILD_NUMBER exist.
Just create new env var like this:
PREV_BUILD_NUMBER = $($BUILD_NUMBER -1)
Excuse me if this piece of code will not work, I'm not good about scripting) Just for example.
UPDATE:
also you can find in mentioned reference a list of variables:
previousBuild
previousBuildInProgress
previousBuiltBuild
previousCompletedBuild
previousFailedBuild
previousNotFailedBuild
previousSuccessfulBuild

Run Nant script as a Jenkins post build action

Is it possible to run a Nant script as a Jenkins post build action?
I get the option to run a script as a Build Step but not as a build action. Is there any particular plugin which enables this option.
The reason I am looking for this functionality is that I need to run a script which depends on the ArtifactDeployer post build action. If i specify the code in the build step it gets executed before the ArtifactDeployer and the build fails
You can use the Post Build Task Plugin
edit
One way of getting the build number if it's not working with this plugin is using the Groovy Post Build Plugin
With it you can execute groovy code as a post build action, get the build number and execute NAnt
the build number is accessible from the following property
manager.build.number
Post-build Actions -> Execute a set of script run after a build when it succeeds or fails. My experience shows that it only sometimes runs when a build is aborted.
As advised above, the Post-build Actions -> Post build task (via the named post task plug in) is always evaluated for run (regardless of the build exit status). Additional setting via phrase in the log ("Build was aborted") works reliable for me.
My problem was to run something even on an aborted build and post build task sorted out this problem.

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