Define a class inside a groovy pipeline?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Define a class inside a groovy pipeline?

Peter Berghold
So I was playing around and tried something.   Here's a class I defined:

class MyClass implements Serializable { 

  MyClass() {
  }

  def bark(){
     sh 'echo WOOF!'
 }
}

and so I instantiate it 

def mc = new MyClass()

and I access my method

mc.bark()

and I get 

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: MyClass.sh() is applicable for argument types: (java.lang.String) values: [echo WOOF!]

So my question is: if I want to access what I perceive as normal pipeline functions from inside a class how do I do it?  What magic foo do I have to use? 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv0Dgkh-CnjYebNBzHji9pOpBZGDLhnioKD87w4-7BWn4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: Define a class inside a groovy pipeline?

Daniel Butler

This is my preferred as it’s nice and explicit:

class MyClass implements Serializable { 

 

  def ctx

 

  MyClass(ctx) {

     this.ctx=ctx

  }

 

  def bark(){

     ctx.sh 'echo WOOF!'

 }

}

 

 

You’d then construct it:
def mc = new MyClass(this)

 

If you remove the class definition so you end up with a groovy file of methods you end up with class with the same name as the groovy file. Inside the class you can call pipeline steps and access the global variables like it’s a pipeline script. However I don’t think you can have anything other than a no-arg constructor.

 

 

Regards,

Daniel.

 

From: [hidden email]
Sent: 01 December 2017 18:02
To: [hidden email]
Subject: Define a class inside a groovy pipeline?

 

So I was playing around and tried something.   Here's a class I defined:

 

class MyClass implements Serializable { 

 

  MyClass() {

  }

 

  def bark(){

     sh 'echo WOOF!'

 }

}

 

and so I instantiate it 

 

def mc = new MyClass()

 

and I access my method

 

mc.bark()

 

and I get 

 

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: MyClass.sh() is applicable for argument types: (java.lang.String) values: [echo WOOF!]
 
So my question is: if I want to access what I perceive as normal pipeline functions from inside a class how do I do it?  What magic foo do I have to use? 
 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv0Dgkh-CnjYebNBzHji9pOpBZGDLhnioKD87w4-7BWn4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5a21a5b5.06aadf0a.a3b73.5f40%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Define a class inside a groovy pipeline?

Peter Berghold
going to give it a shot Daniel.  Thank!

On Fri, Dec 1, 2017 at 1:56 PM Daniel Butler <[hidden email]> wrote:

This is my preferred as it’s nice and explicit:

class MyClass implements Serializable { 

 

  def ctx

 

  MyClass(ctx) {

     this.ctx=ctx

  }

 

  def bark(){

     ctx.sh 'echo WOOF!'

 }

}

 

 

You’d then construct it:
def mc = new MyClass(this)

 

If you remove the class definition so you end up with a groovy file of methods you end up with class with the same name as the groovy file. Inside the class you can call pipeline steps and access the global variables like it’s a pipeline script. However I don’t think you can have anything other than a no-arg constructor.

 

 

Regards,

Daniel.

 

From: [hidden email]
Sent: 01 December 2017 18:02
To: [hidden email]
Subject: Define a class inside a groovy pipeline?

 

So I was playing around and tried something.   Here's a class I defined:

 

class MyClass implements Serializable { 

 

  MyClass() {

  }

 

  def bark(){

     sh 'echo WOOF!'

 }

}

 

and so I instantiate it 

 

def mc = new MyClass()

 

and I access my method

 

mc.bark()

 

and I get 

 

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: MyClass.sh() is applicable for argument types: (java.lang.String) values: [echo WOOF!]
 
So my question is: if I want to access what I perceive as normal pipeline functions from inside a class how do I do it?  What magic foo do I have to use? 
 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv0Dgkh-CnjYebNBzHji9pOpBZGDLhnioKD87w4-7BWn4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5a21a5b5.06aadf0a.a3b73.5f40%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv3AhMEEL8iZgCgPavG6uyQyQ5pWRF_0S3%2BsQOSggVC35g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Define a class inside a groovy pipeline?

Peter Berghold
Yep!  That's the ticket.   Thannks again Daniel! 

On Fri, Dec 1, 2017 at 2:06 PM Peter Berghold <[hidden email]> wrote:
going to give it a shot Daniel.  Thank!

On Fri, Dec 1, 2017 at 1:56 PM Daniel Butler <[hidden email]> wrote:

This is my preferred as it’s nice and explicit:

class MyClass implements Serializable { 

 

  def ctx

 

  MyClass(ctx) {

     this.ctx=ctx

  }

 

  def bark(){

     ctx.sh 'echo WOOF!'

 }

}

 

 

You’d then construct it:
def mc = new MyClass(this)

 

If you remove the class definition so you end up with a groovy file of methods you end up with class with the same name as the groovy file. Inside the class you can call pipeline steps and access the global variables like it’s a pipeline script. However I don’t think you can have anything other than a no-arg constructor.

 

 

Regards,

Daniel.

 

From: [hidden email]
Sent: 01 December 2017 18:02
To: [hidden email]
Subject: Define a class inside a groovy pipeline?

 

So I was playing around and tried something.   Here's a class I defined:

 

class MyClass implements Serializable { 

 

  MyClass() {

  }

 

  def bark(){

     sh 'echo WOOF!'

 }

}

 

and so I instantiate it 

 

def mc = new MyClass()

 

and I access my method

 

mc.bark()

 

and I get 

 

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: MyClass.sh() is applicable for argument types: (java.lang.String) values: [echo WOOF!]
 
So my question is: if I want to access what I perceive as normal pipeline functions from inside a class how do I do it?  What magic foo do I have to use? 
 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv0Dgkh-CnjYebNBzHji9pOpBZGDLhnioKD87w4-7BWn4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5a21a5b5.06aadf0a.a3b73.5f40%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv0Jz4AsdLQdtVPj0jg-eu0tX_f%2Bucv%3Dxt8PgK8K9myunw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.