Jenkins script console: how to start a build for a job? - jenkins

From jenkins script console, how can I initiate a build for a job?
Tried:
for(job in Hudson.instance.getView(view_name).items) {
job.startBuild()
}
Error:
groovy.lang.MissingMethodException: No signature of method: hudson.model.FreeStyleProject.startBuild() is applicable for argument types: () values: []

You can use run.scheduleBuild, as example
cause = new hudson.model.Cause.RemoteCause(startServer, startNote)
failedRuns.each{run -> run.scheduleBuild(cause)}

Related

How to add Jenkins Plugin to repo with tests written in Jenkins Pipeline Unit?

I'm trying to test simple Jenkinsfile Pipeline using Jenkins Pipeline Unit and I have an issue with plugin ansiColor.
I added plugin to build.gradle file:
dependencies {
[...]
testImplementation 'org.jenkins-ci.plugins:ansicolor:1.0.0'
}
I added option to Jenkinsfile
pipeline {
options {
ansiColor('xterm')
[...]
}
After running "gradle clean test" I received an error:
Test testExamplePipeline FAILED (2.2s)
groovy.lang.MissingMethodException: No signature of method: Jenkinsfile.ansiColor() is applicable for argument types: (String) values: [xterm]
at app//TestExampleJenkinsfile.testExamplePipeline(TestExampleJenkinsfile.groovy:16)
How to correctly add plugin to Jenkins Pipeline Unit test?
PS:
Code is available here:
https://github.com/shinhf/jenkins-jpu-test-example
PS2
I know I can always register method using registerAllowedMethod, but I think it's not resolution:
helper.registerAllowedMethod("ansiColor", [String], null)

How to fix triggerPhrase Error in Jenkins Pipeline

I just want to use triggerPhrase to trigger my jenkins pipeline if you give the comment such as "Rebuild"
triggerPhrase('Rebuild')
but I got this error
No signature of method: javaposse.jobdsl.dsl.helpers.triggers.TriggerContext.triggerPhrase() is applicable for argument types: (java.lang.String) values: [Rebuild]
any solutions?

No signature of method: description() is applicable for argument types: (java.lang.String) values: [abcdefg]

Need some help with a groovy script I'm running. I am trying to integrate this as a job in a Jenkins pipeline. The method in the script that it's failing on is:
private void blackduckTestJob(def blackduckTestJob){
blackduckTestJob.with {
description("This job runs dependency checks for the Java reference application using blackduck.")
logRotator this.logRotator.getLogRotator()
wrappers{
preBuildCleanup()
maskPasswords()
credentialsBindings{
usernamePassword('BLACKDUCK_USERNAME','BLACKDUCK_PASSWORD', this.service.getBlackduckCredentialsId())
}
}
steps {
copyArtifacts(this.getProjectFolder() + "/" + this.service.getComponentName() + "/${this.service.getComponentName()}_Application_Build") {
buildSelector {
buildNumber('${B}')
}
}
shell('''
#!/bin/bash
set +x
chmod +x ./mvnw ; ./mvnw com.blackducksoftware.integration:hub-maven-plugin:2.0.2:build-bom -Dhub.url=${BLACKDUCK_URL} -Dhub.username=${BLACKDUCK_USERNAME} -Dhub.password=${BLACKDUCK_PASSWORD}''')
}
}
}
The error I get is:
ERROR: (JavaMicroservicePipelineComponent.groovy, line 154) No signature of method: JavaMicroservicePipelineComponent.description() is applicable for argument types: (java.lang.String) values: [This job runs dependency checks for the Java reference application using blackduck.]
Finished: FAILURE
If anyone can provide any help or suggestions it would be very helpful!
The error is telling you that JavaMicroservicePipelineComponent does not contain a description(String) method. You are invoking that method which does not exist.

Getting FATAL: No signature of method: hudson.model.FreeStyleBuild.call() error

I am trying to execute below commands in Jenkins groovy script.
import hudson.model.*
def buildA = build("Master-Build")
println(buildA.getProject().getLastSuccessfulBuild())
and I am getting below errors
00:00:00.652 FATAL: No signature of method:
hudson.model.FreeStyleBuild.call() is applicable for argument types:
(java.lang.String) values: [Master-Build] 00:00:00.652 Possible
solutions: wait(), save(), any(), wait(long),
each(groovy.lang.Closure), any(groovy.lang.Closure) 00:00:00.652
groovy.lang.MissingMethodException: No signature of method:
hudson.model.FreeStyleBuild.call() is applicable for argument types:
(java.lang.String) values: [Master-Build]
How can I fix this?
Looking at the error it looks like you are calling Execute system Groovy script build step in a freestyle build. When you use groovy in that step, then the variable build is predefined to the current build and not a function for building other jobs (you might have confused it with the predefined functions/variables in Build Flow Job where the build variable is a function which starts a new build).
So in order to start a new build, you need to access the Jenkins instance directly and tell it to start a new job (inspired by this script):
// Import Jenkins
import jenkins.model.Jenkins;
// Get Jenkins instance
def j = Jenkins.getInstance();
// Get the job we wan't to trigger
def job = j.getItem("B");
// Finally we schedule a new build which starts directly (the zero in the argument)
job.scheduleBuild2(0)

Groovy script set number of executors

Please help me, I'm trying to change the number of executors on jenkins. When I'm running this code, it works:
import jenkins.model.Jenkins
Jenkins jenkins = Jenkins.getInstance()
jenkins.setNumExecutors(4)
jenkins.save()
When I use the next function:
void set_executors(int number) {
Jenkins jenkins = Jenkins.getInstance()
jenkins.setNumExecutors(number)
jenkins.save()
}
And running:
java -jar jenkins-cli.jar -s http://localhost:8080 groovy /var/lib/jenkins/executor.groovy set_executors 4
I'm getting:
groovy.lang.MissingMethodException: No signature of method:
Actions.set_executors() is applicable for argument types
(java.lang.String) values: [4]
Possible solutions: set_executors(int)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
Please help!
When run from the command line, groovy passes arguments as strings. Your set_executors function is being called with a String argument instead of an integer as the function expects. You need to modify your code to accept a String argument and convert it to an integer.

Resources