I am new to Jenkins and to groovy scripting, I want to read a .txt file which is located in the workspace of one of the jobs. I am trying to do this way:
myfile = Jenkins.instance.getJob('JobName').workspace.readFileFromWorkspace('file.txt');
But leads to the following error:
groovy.lang.MissingMethodException: No signature of method:
hudson.FilePath.readFileFromWorkspace() is applicable for argument
types: (java.lang.String) values: [file.txt]
Try this:
file = new File("${Jenkins.instance.getJob('JobName').workspace}/file.txt").text
I was struggling to make it work for the pom modules for a file in the workspace, in the
Extended Choice Parameter. Here is my solution with the printlns:
import groovy.util.XmlSlurper
import java.util.Map
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
try{
//get Jenkins instance
def jenkins = Jenkins.instance
//get job Item
def item = jenkins.getItemByFullName("The_JOB_NAME")
println item
// get workspacePath for the job Item
def workspacePath = jenkins.getWorkspaceFor (item)
println workspacePath
def file = new File(workspacePath.toString()+"\\pom.xml")
def pomFile = new XmlSlurper().parse(file)
def pomModules = pomFile.modules.children().join(",")
return pomModules
} catch (Exception ex){
println ex.message
}
Related
I'm creating my Jenkins instance using groovy scripts because I'm automating the Jenkins creation process. I create this script:
/* Adds a multibranch pipeline job to Jenkins */
import hudson.model.*
import hudson.util.PersistedList
import jenkins.*
import jenkins.branch.*
import jenkins.model.*
import jenkins.model.Jenkins
import jenkins.plugins.git.*
import com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger
import org.jenkinsci.plugins.workflow.multibranch.*
// Create job
def env = System.getenv()
Jenkins jenkins = Jenkins.instance
String jobName = "Job"
String jobScript = "Jenkinsfile"
def job = jenkins.getItem(jobName)
// Create the folder if it doesn't exist
if (job == null) {
job = jenkins.createProject(WorkflowMultiBranchProject.class, jobName)
}
job.getProjectFactory().setScriptPath(jobScript)
// Add git repo
String id = null
String remote = env.CODE_COMMIT_URL
String includes = "*"
String excludes = ""
boolean ignoreOnPushNotifications = false
GitSCMSource gitSCMSource = new GitSCMSource(id, remote, null, includes, excludes, ignoreOnPushNotifications)
BranchSource branchSource = new BranchSource(gitSCMSource)
// Remove and replace?
PersistedList sources = job.getSourcesList()
sources.clear()
sources.add(branchSource)
job.addTrigger(new PeriodicFolderTrigger("1m"))
and paste it at $JENKINS_HOME/ref/init.groovy.d/. When I start Jenkins, by job was already created. Besides that, I need to add some Git Behaviors to my job and I'd like to know if is there a way to add Git Behaviors using groovy script?
My Git after created:
Git behaviors I'd like to add at initialization (Discover tags, Check out to matching local branch, Custom user name/e-mail address)
Thank you!
I think what you want is managed through traits (I haven't actually tried this):
import jenkins.plugins.git.traits.*
def traits = []
// Add your traits...
traits.add(new TagDiscoveryTrait())
traits.add(new LocalBranchTrait())
gitSCMSource.setTraits(traits)
In a Jenkins job, I want to trigger another Jenkins job from a Groovy script :
other_job.scheduleBuild();
But other_job is not launched on the same node than the parent job. How can I modify my script to launch other_job on the same node than the parent job ?
I used to do that with the "Trigger/call builds on other project" and "NodeLabel Parameter" plugins but I would like now to do that inside a script.
Firstly, check Restrict where this project can be run option in 'other_job' configuration - you must specify the same node name there.
Then, this should work:
import hudson.model.*
def job = Hudson.instance.getJob('other_job')
job.scheduleBuild();
If you don't want to use this option in your 'other_job', then you can use NodeLabel Parameter Plugin (which you already used) and pass the NodeLabel parameter to downstream job.
In this case, see example from Groovy plugin page how to start another job with parameters (you need to use NodeParameterValue instead of StringParameterValue):
def job = Hudson.instance.getJob('MyJobName')
def anotherBuild
try {
def params = [
new StringParameterValue('FOO', foo),
]
def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
println "Waiting for the completion of " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
anotherBuild = future.get()
} catch (CancellationException x) {
throw new AbortException("${job.fullDisplayName} aborted.")
}
println HyperlinkNote.encodeTo('/' + anotherBuild.url, anotherBuild.fullDisplayName) + " completed. Result was " + anotherBuild.result
If it's not working, probably the issue is with node restrictions (e.g., there is only one executor for the node).
NOTE: I prefer to use Jenkins pipelines for job configurations. It allows you to store your build configs in Jenkinsfiles which can be loaded from repository (e.g., from GitLab). See example of triggering job with NodeParameterValue.
Based on the answer of biruk1230, here is a full solution :
import hudson.model.*;
import jenkins.model.Jenkins
import java.util.concurrent.*
import hudson.AbortException
import org.jvnet.jenkins.plugins.nodelabelparameter.*
def currentBuild = Thread.currentThread().executable
current_node = currentBuild.getBuiltOn().getNodeName()
def j = Hudson.instance.getJob('MyJobName')
try {
def params = [
new NodeParameterValue('node', current_node, current_node),
]
def future = j.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
println "Waiting for the completion of " + j.getName()
anotherBuild = future.get()
} catch (CancellationException x) {
throw new AbortException("aborted.")
}
Building on this suggestion:
Using FilePath to access workspace on slave in Jenkins pipeline I am trying to get the files that are dropped into the workspace by git, and iterate through those files using eachFileRecurse, by passing in the adjusted folder. However, when calling into FilePath, I get errors.
import groovy.io.FileType
import java.io.File
import java.lang.Object
import hudson.FilePath
import jenkins.model.Jenkins
def createFilePath(path) {
return new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path);
}
#NonCPS // has to be NonCPS or the build breaks on the call to .each
def getFiles(dirLoc) {
def dir = new File (dirLoc)
def list = []
dir.eachFileRecurse (FileType.FILES)
{file -> if (file.name.endsWith('.txt')) {list << file}}
return list
}
I get this error
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.File(hudson.FilePath)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1737)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1537)
I cannot figure out what I am missing here to make this work. Thanks in advance!
There is no Constructor for java.io.file with hudson.FilePath as a parameter.
Refer to java doc at following link: https://docs.oracle.com/javase/7/docs/api/java/io/File.html
For the already available multi-job in jenkins, need to add new phase jobs using Groovy Scripting. I have written the following groovy code which adds up an already existing job p25_deploy-1.
This code is working to create the multi-job but the phase job is not showing as mapped in the Jenkins UI. Where as if I see it config.xml, its created properly as expected except a tag <killPhaseOnJobResultCondition>. Not sure why the phase job is not mapped properly?
import jenkins.model.*
import hudson.model.*
import com.tikal.jenkins.plugins.multijob.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.KillPhaseOnJobResultCondition.*
import java.lang.String.*
import hudson.model.Descriptor;
import hudson.tasks.Builder;
def jenkinsInstance = jenkins.model.Jenkins.instance
def templateJobName = 'profile_p25'
def templateJob = jenkinsInstance.getJob(templateJobName)
// get MultiJob BuildPhases and clone each PhaseJob
builders = templateJob.getBuilders();
builders.each { b ->
if (b instanceof MultiJobBuilder){
def pj = b.getPhaseJobs()
hudson.model.Describable p1 = new PhaseJobsConfig("p25_deploy-1",null,
true,PhaseJobsConfig.KillPhaseOnJobResultCondition NEVER,null,false,false,null,0,false,true,null,false,false)
pj.add(p1)
}
}
templateJob.save()
// update dependencies
jenkinsInstance.rebuildDependencyGraph()
Any help will be really appreciated. Have tried many ways but was not able to figure out the problem with the script.
We can use DSL to create but I wanted it to be done in Groovy Scripting and moreover modify the existing job.
Blockquote
Yay! I am back with the answer for my question. Have tried this since very long time. Finally am able to make it though. I was aware that solution would be really simple but not able to figure out the hack of it.
import jenkins.model.*
import hudson.model.*
import com.tikal.jenkins.plugins.multijob.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.KillPhaseOnJobResultCondition.*
import java.lang.String.*
import hudson.model.Descriptor
import hudson.tasks.Builder
def jenkinsInstance = jenkins.model.Jenkins.instance
def templateJobName = 'profile_p25'
def templateJob = jenkinsInstance.getJob(templateJobName)
// get MultiJob BuildPhases and clone each PhaseJob
builders = templateJob.getBuilders();
builders.each { b -> if (b instanceof MultiJobBuilder)
{ def pj =
b.getPhaseJobs()
hudson.model.Describable newphase = new
PhaseJobsConfig(deploys[i],null,
true,null,null,false,false,null,0,false,false,"",false,false)
newphase.killPhaseOnJobResultCondition = 'NEVER'
pj.add(newphase)
}
}
templateJob.save()
We have a evaluated Groovy script in Jenkins below:-
;
But the build is failing with error [EnvInject] - [ERROR] - [EnvInject] - [ERROR] - Problems occurs on injecting env vars as a build wrap: null
17:04:06 Finished: FAILURE.
Also how can I call the variable from Jenkins shell script to get last successful build date. -Thanks
def env = System.getenv()
def item = Jenkins.instance.getItem("")
def f=item.getLastFailedBuild()
println f.getTime()
def ff=env['item.getLastSuccessfulBuild()]
println ff.getTime().format("YYYY-MMM-dd HH:MM:SS")
println ff.getTime().format("dd-MM-yyyy")
def pa = new ParametersAction([new StringParameterValue('PARAMETER_NAME', ff)]);
Thread.currentThread().executable.addAction(pa)
println 'Script finished! \nenv variable
The easy answer is that on line 7 you have no closing quote here:
def ff=env['item.getLastSuccessfulBuild()]
However, that is not the last of your issues:
I don't think you want to use ff = env['item.getLastSuccessfulBuild()]'] but rather just a simple ff = item.getLastSuccessfulBuild()
You need to include the following import lines to be able to use the associated classes:
import jenkins.model.Jenkins
import hudson.model.ParametersAction
import hudson.model.StringParameterValue
The line item = Jenkins.instance.getItem("Fastlane_Test") doesn't work in my environment, even replacing "Fastlane_Test" with a job that exists.I've replaced it with item = Jenkins.instance.getItemByFullName("Fastlane_Test").
Also, for safety, you should test to ensure item isn't null
Finally, have you missed the Thread.currentThread().executable.addAction(pa) line out for a reason? You need to use it to add the new parameter to the running environment.
The following code should hopefully be a reasonable starting point, however please note that I've removed the line def env = System.getenv() since env isn't used anywhere else in the code later:
import jenkins.model.Jenkins
import hudson.model.ParametersAction
import hudson.model.StringParameterValue
def item = Jenkins.instance.getItemByFullName("Fastlane_Test")
if (item) {
def f=item.getLastFailedBuild()
println f.getTime()
def ff=item.getLastSuccessfulBuild()
println ff.getTime().format("YYYY-MMM-dd HH:MM:SS")
println ff.getTime().format("dd-MM-yyyy")
def pa = new ParametersAction([new StringParameterValue("LAST_GOOD", ff.getTime().toString())])
Thread.currentThread().executable.addAction(pa)
}
Hope you find this of assistance, although I see it's been a while since you posted the question.
Kind Regards
Thanks Nick!!
I added the below "execute system groovyscript" as part of Jenkins job and it worked:
import jenkins.model.Jenkins
`. def item = Jenkins.instance.getItem("Job")
def ff=item.getLastSuccessfulBuild()
println ff.getTime().format("yyyy-MM-dd")
def temp = ff.getTime().format("yyyy-MM-dd")
import hudson.model.*
def build = Thread.currentThread().executable
def pa = new ParametersAction([
new StringParameterValue("LAST_BUILD_DATE",temp)
])
build.addAction(pa)`