Programmatically set Timestamper plugin "Enabled for all Pipeline builds" option - jenkins

Is it possible to set Timestamper plugin "Enabled for all Pipeline builds" option programmatically (upon Jenkins installation)?

You could use Groovy init scripts with something like:
import hudson.plugins.*
instance = Jenkins.getInstance()
tsconfig = instance.getDescriptorByType(timestamper.TimestamperConfig)
tsconfig.setAllPipelines(true)
tsconfig.save()
See: https://www.jenkins.io/doc/book/managing/groovy-hook-scripts/
And: https://javadoc.jenkins.io/plugin/timestamper/index.html?hudson/plugins/timestamper/api/TimestamperAPI.html

Related

How to extract Jenkinsfile by a custom script?

I've created a Jenkins job of type "Pipeline", and used an inline pipeline script. Now I'd like to put the script under version control and use the "Pipeline script from SCM" option (I think, I don't have to describe the merits of this).
However, our version control system (CA SCM) is not well supported in Jenkins: I couldn't make the plugin to check out anything.
We do have, however, some scripts for working with CA SCM that allow to check out things reliably.
So, my question is: Is it possible (and how) to have the Jenkinsfile under version control, do the check out for it by a custom script (e.g. using a .bat command) and then have the pipeline executed as if the Jenkinsfile had been extracted by the "Pipeline script from SCM" option?
I.e., as I understand it, I need a command in the pipeline plugin to execute a given Jenkinsfile.
You could try to use the feature "Prepare an environment for the build" from the "Environment Injector Plugin" (https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin) and provide a script file (or inlined content) to execute.

How to use "Parameterized Remote Trigger Plugin" in Jenkins Pipeline script?

I tried search but didn't find any example. I tried https://jenkins.io/doc/pipeline/examples/#trigger-job-on-all-nodes and got it is for the different nodes on the same Jenkins.
I would like to trigger a build on another Jenkins. I configured the Remote Hosts and Authentication in system configuration of my Jenkins.
How to call "Parameterized Remote Trigger Plugin" in Jenkins Pipeline script?
Seems to be an open bug: https://issues.jenkins-ci.org/browse/JENKINS-38657
As a workaround you could create another job locally of an old type and use the plugin in the old school non pipeline script way. Then in your pipeline script you would just trigger this job. I know it's an ugly adapter but then you have parametrize this adapter and have it up and running for almost anything ;)
EDIT:
The bug 38657 is already closed, the plugin is available as pipeline step since 16th of May 2018. Usage should be as easy as:
//Trigger remote job
def handle = triggerRemoteJob(remoteJenkinsName: 'remoteJenkins', job: 'RemoteJob')
More information on the triggerRemoteJob step
For anyone wondering how to do this using the Declarative Jenkinsfile Syntax:
steps {
triggerRemoteJob remoteJenkinsName: 'configured-remote-jenkins-name', job: 'trigger-job-folder/trigger-job-name', blockBuildUntilComplete: true
}

How to get the Jenkins plugin Build User Vars to work with Jenkins Pipeline

I'm using this Jenkins plugin: https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin
With this plugin installed, if you check the box "Set jenkins user build variables", you can use the environment variable ${BUILD_USER} which gives the name of the person who built the Jenkins job.
But, I can't get the plugin to work with the Pipeline plugin (https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin).
I noticed that checking that "Set jenkins user build variables" box adds the following line to the config.xml of your Jenkins job: <org.jenkinsci.plugins.builduser.BuildUser plugin="build-user-vars-plugin#1.5"/>
So I tried the following:
import org.jenkinsci.plugins.builduser.BuildUser
echo "${env.BUILD_USER}"
But it prints out null.
try use this variable:
def USER = wrap([$class: 'BuildUser']) {
return env.BUILD_USER
}
I used here and It worked !!

Initializing Jenkins 2.0 with pipeline in init.groovy.d script

For automation, I would like to initialize a Jenkins 2.0 instance with a pipeline job. I want to create a Groovy script that is copied to the /usr/share/jenkins/ref/init.groovy.d/ folder on startup. The script should create a Jenkins 2.0 Pipeline job for processing a Jenkinsfile from SCM.
I cannot find the relevant Javadoc for the 2.0 pipeline classes or examples of how to do this.
Previously, using Job DSL to create a pipeline, I used a Groovy script to create a FreeStyleProject with an ExecuteDslScripts builder. That job would then be the Job DSL seed job.
One option is to use an init script to create a Job DSL seed job to create a Jenkins 2.0 pipeline. It just seems unnecessarily complex.
I am experimenting in this repo: https://github.com/martinmosegaard/vigilant-sniffle
With Job DSL 1.47 (to be released soon) you can use the Job DSL API directly from the init script without the need to create a seed job.
import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.plugin.JenkinsJobManagement
def jobDslScript = new File('jobs.groovy')
def workspace = new File('.')
def jobManagement = new JenkinsJobManagement(System.out, [:], workspace)
new DslScriptLoader(jobManagement).runScript(jobDslScript.text)
See PR #837 for details.
If you only need to create one simple pipeline job, you can use the Jenkins API. But that really only works well when creating one simple job, for a complex setup you need some abstraction like Job DSL.
Start here: http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html#createProject(java.lang.Class,%20java.lang.String).
Example:
import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.job.WorkflowJob
WorkflowJob job = Jenkins.instance.createProject(WorkflowJob, 'my-pipeline')
Then you need to populate the job, e.g. setting a flow definition.
Or you can wait for the System Config DSL Plugin to be ready. But it has not been released yet and I'm not sure if it can create jobs.

Can I use tokens in Jenkins job config?

I'd like to use the Jenkins job name in the config:
Maven root POM should be: {JOB_NAME}\pom.xml
SCM sub path should be: {JOB_NAME}
Are there tokens I could use here?
For SCM the answer is 'yes' - use ${JOB_NAME} (e.g. svn://myserver/myrepo/trunk/${JOB_NAME}).
In Maven build step it does not work. However, you may try using a custom workspace (push 'Advanced' button under 'Build' -> check 'use custom workspace') that contains ${JOB_NAME} (e.g. C:\workspaces\${JOB_NAME}) as a workaround.

Resources