Add Jenkins Groovy Postbuild step to all jobs - groovy

How can the same Jenkins Groovy Postbuild Plugin step be added to all jobs? We have 50+ jobs, so it is too hard to copy-paste the link to desired groovy code to every job.

I usually do similar mass changes by updating the config.xml of the effected jobs. Every good editoru should have a search and replace function that works on files. use the following workflow.
shut down Jenkins
update job config.xml files
start up Jenkins
There are other possible workflows like the following
1. update job config.xml files
2. reload config
However, with the second option I don't know how it effects if a job is running while you reload a config.

The following script requires the Jenkins Groovy and Groovy postbuild plugins to work. It will add a Jenkins Groovy postbuild publisher to all jobs:
https://gist.github.com/genericpenguin/9ac1b84ed7a145b3b6dd
Change the view from 'All' to some other view for testing. We use it for mass changes and it works fine. Only caveat is that postbuild groovy script must reside on Jenkins master. This can be easily changed in the script if it is short (like a script loader).

Related

How to automate a Jenkins task using groovy script which runs on script console

I have groovy script which runs on jenkins script console. My objective here is to automate the process which should run everyday.
Could you guys help me in achieving this task?
There are different ways you can automate it, but you will need to install one of the below plugins:
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
Scriptler plugin is another plugin.
Then,
- Create a Jenkins Job
- Add (system groovy build action or groovy post build action)
- Run and Test
I hope it helps.
Cheers

Running Groovy command in jenkins

I want to run a groovy command in Jenkins.For which I have configured groovy through Install automatically.Then I created a free style project and selected Execute groovy script under the build section and then under it I have selected groovy command and have written a simple hello world line.But when I tried executing the job the job history shows pending-Waiting for next available executor and I am unable to build the job.Please help me in identifying the problem and do suggest solution.I am pretty new to Groovy.
You need have at least one executor to run your job.
Do you have any slaves? if yes, check if your slave is okej, otherwise, check if your jenkins have free executor.

Using jenkins to modify files

I've just started to get to grips with Jenkins. It currently performs the following tasks:
Pulls the latest codebase from git
Uploads the codebase via sftp to my environment
Sends a notification email to the testers and the PM to inform them of a completed deployment.
However for it to be truly useful I need it to perform two more tasks:
Delete the robots.txt and .htaccess file which exists in the git repo and replace it with a predefined version which is specific for the server
Go through all the code and remove specific code-blocks (perhaps something in between comments: eg. /** Dev only **/ Code to be removed goes here /** Dev only **/ or something like that).
Are there any plugins which can accomplish these things or would I have to read up on writing groovy scripts for this sort of thing (I don't know anything about those yet).
On a related note: I'd also love it if it could combine kit and SASS files, however I can't see a plugin for these things, however I assume I can just install compass on my build server and then run it via command line in the build process. Is that correct?
Instead of putting your build tasks directly into the Jenkins job, I recommend writing a build script to accomplish your publishing/deployment tasks.
Jenkins is great for having a single point of automation that is easy to run, can publish build results, and can track successes and failures. In my experience though, you're better off not putting your individual tasks and configuration steps into the Jenkins job configuration. At some point, you'll want to be able to run this job without Jenkins, either because you want to test local changes, or you want to handle multiple jobs and trying to keep job configurations in sync is not fun, or because you're moving to another build/deployment system. Also, putting the build script into a file allows you to put it into your source control system and track changes.
My advice: choose a scripting language (Python, Ruby, Perl, whatever you're comfortable with) or build system (SCons and Rake are options) and write a build script. In Python Ruby, and Perl, it's easy to manipulate files (#1) and all have a wide choice of templating systems that will accomplish #2. Then the Jenkins job becomes running your build script on the command line (or executing through a language-specific builder). And the build script can include running any of the tasks that you decide to put in your build (compass, etc).

How to place Email-Ext groovy script on the jenkins file system

I need to dynamically modify the notification e-mail recipients based on the build, so I'm using a groovy script. I want this script to be available to all jobs, so I want it to reside on the Jenkins file system and not within each project. It can be either in the recipients fields (using ${SCRIPT,...}) or in the pre-send script. A short (fixed) script that evaluates the master script is also good, as long it is the same for all projects.
You should try Config File Provider plugin. It works together with the Credentials configuration of Jenkins.
Go to Manage Jenkins > Managed files
Click Add a new Config
Select Groovy file
Enter the contents of your script
Your script will now be saved centrally on Jenkins, available to master/slave nodes.
In your Job Configuration:
Under Build Environment
Check Provide Configuration file
Select your configured groovy File from dropdown
Set Variable with a name, for example: myscript
Now, anywhere in your job, you can say ${myscript} and it will refer to absolute location of the file on filesystem (it will be somewhere in Jenkins dir).
My impression it that you would probably want to completely switch to Jenkins pipelines where the entire job is groovy file (Jenkinsfile) in the root of the repository.
Email-Ext already supports it even if it may be lacking some documentation.
https://jenkins.io/doc/pipeline/steps/email-ext/

How to Inject a Build Environment Variable Computed from a Jenkins Run Parameter?

I understand how to use the EnvInject plugin to compute variables from string parameters (the parameter name simply is an unbound variable in the groovy script performing the injection).
I want to use a Run Parameter - the parameter that contains the latest successful build of a project (or whichever build the user selects), but it appears that any of those are not available at the time the EnvInject plugin runs.
I guess I need to write groovy code to inspect the desired project myself and get the right build name directly from the model - except I can't do that, since the model lives on the master and the envInject plugin runs on the slave....
EnvInject can run at different times...
At node/master level (on startup, and available always).
At job startup, before SCM step (configured at the top of job configuration).
After SCM step (configured under "build environment" section).
As a build step.
You should either last or second-last options, if you want build parameters to be available
Lastly, there is another option to try. Get pre-scm-buildstep plugin. This allows to run any build step (including EnvInject), just before the SCM checkout. I've done a quick test, and the job parameters and their values are available at this stage.

Resources