I'm trying to trigger a job manually from a controller in my Grails4 application.
I'm seeing this error:
groovy.lang.MissingMethodException: No signature of method: static com.myapp.MyJob.triggerNow() is applicable for argument types: (LinkedHashMap) values: [[foo:It works]]
I've seen older posts with the same issue, but they were using older versions of the plugin and I have tried all of the suggestions (package of controller and job the same, inject the quartzScheduler in the controller).
Here's my controller:
package com.myapp
class MyController {
def quartzScheduler
def myAction() {
MyJob.triggerNow([foo:"It works"])
}
}
Here's my job:
package com.myapp
class MyJob {
static triggers = {}
def execute(context) {
println context.mergedJobDataMap.get('foo')
}
}
My build.gradle file has this:
compile 'org.grails.plugins:quartz:2.0.13'
compile 'org.quartz-scheduler:quartz:2.2.1'
Related
I would like to integrate a Global library into my build flow. I have written a basic function
srv/core/jenkins/Checks.groovy:
package core.jenkins
class Checks implements Serializable {
def script
Checks(script) {
this.script = script
}
def fileExists(){
script.echo "File exists in the repo."
}
}
And it is exposed as a global var
vars/fileExisits.groovy:
def call() {
new core.jenkins.Checks(this).fileExists()
}
While configuring the Global Shared Library settings in Jenkins, I have the following settings:
Now in my jenkinsfile, Im doing something like this:
pipeline {
agent { label 'master' }
stages {
stage('Check for md files'){
steps {
sh 'echo hello'
script {
checks.fileExists()
}
}
}
}
}
This always gives the error
groovy.lang.MissingPropertyException: No such property: checks for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at
For it to work, I have to add the lines to the top of my Jenkinsfile
import core.jenkins.Checks
def checks = new Checks(this)
Is there a way for me to invoke the function fileExists from a library without having to add the above 2 lines always ?
Just replace:
checks.fileExists()
with:
fileExists()
All Groovy scripts that implements def call() methods and are stored in the vars/ folder can be triggered by their script file name. Alternatively, if you would like to keep checks.fileExists() syntax, then you need to create vars/checks.groovy script file and implement def fileExists() method inside of it.
I've read a lot of tutorials regarding the Jenkins job DSL plugin but I cannot figure out how to create a job factory class that in turn uses another class that generates the jobs.
I already have a lot of jobs Groovy scripts that directly call the job factory and that works and my jobs are generated correctly. What I need is another class that in turn generates a lot of jobs using the previous job factory.
I've spent hours trying to debug this issue. I'm pretty sure it has something to do with closures and instantiating the factory objects.
package company.factory
import javaposse.jobdsl.dsl.DslFactory
import javaposse.jobdsl.dsl.Job
/**
* Base DSL templates for all Jenkins jobs.
*
*/
class JobFactory {
private DslFactory dslFactory
JobFactory(DslFactory dslFactory) {
this.dslFactory = dslFactory
}
Job generateBaseJob() {
dslFactory.job('TEST-1') {
logRotator(365, -1, 1, -1)
}
}
def generate() {
generateBaseJob()
}
}
Calling it a Groovy script works:
import company.factory.JobFactory
JobFactory jobfactory = new JobFactory(this)
jobfactory.generate()
Now, when I try to extend that class with another class, it fails.
package company.flow
import company.factory.JobFactory
class DeploymentFlow {
JobFactory jobfactory = new JobFactory(this)
def generateAllJobs() {
jobfactory.generate()
}
}
Calling it a Groovy script fails:
import company.flow.DeploymentFlow
new DeploymentFlow().generateAllJobs()
Error:
javaposse.jobdsl.dsl.DslScriptException: (JobFactory.groovy, line 36) No signature of method: company.flow.DeploymentFlow.job() is applicable for argument types: (java.lang.String, company.factory.JobFactory$_generateBaseJob_closure1) values: [TEST-1, company.factory.JobFactory$_generateBaseJob_closure1#60ed3159]
Possible solutions: any(), wait(), getA(), find(), grep(), dump()
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:114)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts_closure1(AbstractDslScriptLoader.groovy:61)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:87)
at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:57)
Caused by:
groovy.lang.MissingMethodException: No signature of method: company.flow.DeploymentFlow.job() is applicable for argument types: (java.lang.String, company.factory.JobFactory$_generateBaseJob_closure1) values: [TEST-1, company.factory.JobFactory$_generateBaseJob_closure1#60ed3159]
Possible solutions: any(), wait(), getA(), find(), grep(), dump()
at company.factory.JobFactory.generateBaseJob(JobFactory.groovy:36)
at company.flow.DeploymentFlow.generateAllJobs(DeploymentFlow.groovy:31)
at script.run(script:24)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:138)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:108)
... 6 more
I need to extend my job factory class into another class as I want to generate a lot of jobs and I want to avoid duplicated code. Any help would be greatly appreciated.
I managed to figure it out with the help from the nice people on Google Jenkins DSL plugin group.
The working code is below.
Factory class:
package company.factory
import javaposse.jobdsl.dsl.DslFactory
import javaposse.jobdsl.dsl.Job
class JobFactory {
private DslFactory dslFactory
JobFactory(DslFactory dslFactory) {
this.dslFactory = dslFactory
}
Job generateBaseJob() {
dslFactory.job('TEST-1')
}
}
Deployment class:
package company.flow
import company.factory.JobFactory
import javaposse.jobdsl.dsl.DslFactory
class DeploymentFlow {
JobFactory JobFactory
DeploymentFlow(DslFactory dslFactory) {
JobFactory = new JobFactory(dslFactory)
}
void generateAllJobs() {
JobFactory.generateBaseJob()
}
}
Calling my deployment class in my seed job:
import company.flow.DeploymentFlow
DeploymentFlow deploymentFlow = new DeploymentFlow(this)
deploymentFlow.generateAllJobs()
I want to refactor my Jenkins pipeline script into classes for readability and reuse.
The problem is i get exceptions when doing so.
Let's look at a simple example:
When i run
echo currentBuild.toString()
everything is fine
But when i extract it into a class as so:
class MyClass implements Serializable {
def runBuild() {
echo currentBuild.toString()
}
}
new MyClass().runBuild()
i get an exception:
Started by user admin
Replayed #196
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: currentBuild for class: MyClass
What is the proper way of extracting pipeline code in to classes?
You are on the right way, but the problem is that you didn't pass the script object to the instance of your class and was trying to call method which is not defined in the class that you have created.
Here is one way to solve this:
// Jenkins file or pipeline scripts editor in your job
new MyClass(this).runBuild()
// Class declaration
class MyClass implements Serializable {
def script
MyClass(def script) {
this.script=script
}
def runBuild() {
script.echo script.currentBuild.toString()
}
}
your code missing declare class field script
class MyClass implements Serializable {
def script
MyClass(def script) {
this.script=script
}
def runBuild() {
script.echo script.currentBuild.toString()
}
}
this code should be ok #bram
I have a test where the gsp uses the Asset Pipeline tag assetPath, in grails 2 I had no issues but in grails 3 I'm getting an org.grails.taglib.GrailsTagException: [SOME_GSP.gsp:184] Error executing tag <g:form>: Error evaluating expression [assetPath(src: 'SOME_PDF.pdf')] on line [60]: No signature of method: SOME_GSP_gsp.assetPath() is applicable for argument types: (java.util.LinkedHashMap) values: [[src:SOME_PDF.pdf]]
Grails 3.1.6
Example test file:
#TestMixin(GroovyPageUnitTestMixin)
class SomeTests extends Specification {
void "ssn is not returned to the SOME_GSP page"() {
given:
def cmd = new commandObject(SOME_FIELD: 'data')
when:
def renderedView = render(view: 'SOME_GSP', model: [cmd: cmd])
then:
assert !renderedView.contains('data')
}
}
Turns out I needed to mock certain classes:
#Mock([AssetsTagLib, AssetMethodTagLib, AssetProcessorService])
And to get the classes to resolve I needed to add a testCompile dependency to my build.gradle
testCompile "com.bertramlabs.plugins:asset-pipeline-grails:2.9.4"
I would like to use a slightly more complex pipeline build via jenkinsfiles, with some reusable steps as I have a lot or similar projects. I'm using jenkins 2.0 with the pipeline plugins. I know that you can load groovy scripts which contain can contain some generic pieces of code but I was wondering if these scripts can use some of the Object oriented features of groovy like traits. For example say I had a trait called Step:
package com.foo.something.ci
trait Step {
void execute(){ echo 'Null execution'}
}
And a class that then implemented the trait in another file:
class Lint implements Step {
def execute() {
stage('lint')
node {
echo 'Do Stuff'
}
}
}
And then another class that contained the 'main' function:
class foo {
def f = new Lint()
f.execute()
}
How would I load and use all these classes in a Jenkinsfile, especially since I may have multiple classes each defining a step? Is this even possible?
Have a look at Shared Libaries. These enable the use of native groovy code in Jenkins.
Your Jenkinsfile would include your shared libary, and the use the classes you defined. Be aware, that you have to pass the steps variable of Jenkins, if you want to use stage or the other variables defined in the Jenkins Pipeline plugin.
Excerpt from the documentation:
This is the class, which would define your stages
package org.foo
class Utilities implements Serializable {
def steps
Utilities(steps) {this.steps = steps}
def mvn(args) {
steps.sh "${steps.tool 'Maven'}/bin/mvn -o ${args}"
}
}
You would use it like this:
#Library('utils') import org.foo.Utilities
def utils = new Utilities(steps)
node {
utils.mvn 'clean package'
}