How can I inject environment variables into my groovy class?

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

How can I inject environment variables into my groovy class?

red 888
My pipeline is using the environment directive to set an environment var and then calls a groovy class:

def call(int blah) {

pipeline {
agent any

environment {
MyVar = credentials('djsjflsjfljsf')
}

stages {
stage('Stage ONE') {
steps {
echo test.methodA()
            ....


But from in my groovy class I don't see those environment variables:

// vars/test.groovy

class test implements Serializable {
static methodA (){
    def env = System.getenv()
return env.dump()
//return ['powershell', 'ls env:'].execute().text
}


env.dump() and listing envs from the shell from inside the class doesn't show those variables I added in the environment directive. Can I change the scope of the class to give it access to these environment vars?



















--
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/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I inject environment variables into my groovy class?

Victor Martinez
Just wondering whether using the previous thread: https://groups.google.com/forum/#!topic/jenkinsci-users/XbI9WjlLmFY/discussion might help to keep a history of the previous suggestions though

--
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/ae611924-be54-4018-8997-5fb3f08fb81a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I inject environment variables into my groovy class?

Michael Pailloncy
In reply to this post by red 888
Pipeline environment variables are not stored at the OS level. That's why you are not able to get them using System.getenv() (which only list OS level environment variables).
But you should be able to retrieve them like any other classical environment variables : with the "${MyVar}" syntax inside your shared lib class.

Hopefully it helps.

Michaël

2017-11-28 22:30 GMT+01:00 red 888 <[hidden email]>:
My pipeline is using the environment directive to set an environment var and then calls a groovy class:

def call(int blah) {

pipeline {
agent any

environment {
MyVar = credentials('djsjflsjfljsf')
}

stages {
stage('Stage ONE') {
steps {
echo test.methodA()
            ....


But from in my groovy class I don't see those environment variables:

// vars/test.groovy

class test implements Serializable {
static methodA (){
    def env = System.getenv()
return env.dump()
//return ['powershell', 'ls env:'].execute().text
}


env.dump() and listing envs from the shell from inside the class doesn't show those variables I added in the environment directive. Can I change the scope of the class to give it access to these environment vars?



















--
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/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.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/CAPO77c3iY5YycUtQiMGi2UgdczREa3brDiG5PBxvQKk%3DBHsWTw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I inject environment variables into my groovy class?

red 888
So this is a problem for me because the APIs I'm using in the groovy class are looking for OS level environment variables.

Is there any way to set these variables with jenkins?

On Tuesday, November 28, 2017 at 5:54:37 PM UTC-5, mpapo - Michael Pailloncy wrote:
Pipeline environment variables are not stored at the OS level. That's why you are not able to get them using System.getenv() (which only list OS level environment variables).
But you should be able to retrieve them like any other classical environment variables : with the "${MyVar}" syntax inside your shared lib class.

Hopefully it helps.

Michaël

2017-11-28 22:30 GMT+01:00 red 888 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="nrYGFkK0AwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fakemai...@...>:
My pipeline is using the environment directive to set an environment var and then calls a groovy class:

def call(int blah) {

pipeline {
agent any

environment {
MyVar = credentials('djsjflsjfljsf')
}

stages {
stage('Stage ONE') {
steps {
echo test.methodA()
            ....


But from in my groovy class I don't see those environment variables:

// vars/test.groovy

class test implements Serializable {
static methodA (){
    def env = System.getenv()
return env.dump()
//return ['powershell', 'ls env:'].execute().text
}


env.dump() and listing envs from the shell from inside the class doesn't show those variables I added in the environment directive. Can I change the scope of the class to give it access to these environment vars?



















--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="nrYGFkK0AwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: How can I inject environment variables into my groovy class?

Daniel Butler

If you’re writing a groovy class that’s run from a library in the pipeline script then you’re not going to be able to use System.getEnv().

 

There’s a few approaches you can take that do work:

  • You can pass in the values you need as parameters to the methods/constructors you’re using.
  • In the pipeline there’s the global variable `env` which is a map of all the currently declared environment variables (JOB_NAME etc) you could pass into your class.
  • Pass in `this` from the pipeline script. You can then use this object to access anything in pipeline, (Let’s assume you called the field/parameter ctx) i.e. ctx.env.JOB_NAME or ctx.powershell(“write-host ‘spooky’”)

 

I noticed also you’ve got a direct execution of a process commented out, don’t do that either. The same way System.getEnv() doesn’t work, doing that will not behave as expected either (Assuming the sandbox will let you)

 

 

Regards,

Daniel.

 

From: [hidden email]
Sent: 29 November 2017 20:28
To: [hidden email]
Subject: Re: How can I inject environment variables into my groovy class?

 

So this is a problem for me because the APIs I'm using in the groovy class are looking for OS level environment variables.

 

Is there any way to set these variables with jenkins?

On Tuesday, November 28, 2017 at 5:54:37 PM UTC-5, mpapo - Michael Pailloncy wrote:

Pipeline environment variables are not stored at the OS level. That's why you are not able to get them using System.getenv() (which only list OS level environment variables).

But you should be able to retrieve them like any other classical environment variables : with the "${MyVar}" syntax inside your shared lib class.

 

Hopefully it helps.

 

Michaël

 

2017-11-28 22:30 GMT+01:00 red 888 <<a href="javascript:" target="_blank">fakemai...@...>:

My pipeline is using the environment directive to set an environment var and then calls a groovy class:

 

def call(int blah) {

   
pipeline {
       
agent any

        environment
{
            MyVar =
credentials('djsjflsjfljsf')
        }

       
stages {
            stage(
'Stage ONE') {
               
steps {
                    echo test.methodA()
            ....

 

 

But from in my groovy class I don't see those environment variables:

 

// vars/test.groovy
 
class test implements Serializable {
   
static methodA (){
    def env = System.getenv()
    return env.dump()
    //return ['powershell', 'ls env:'].execute().text
}

 

env.dump() and listing envs from the shell from inside the class doesn't show those variables I added in the environment directive. Can I change the scope of the class to give it access to these environment vars?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--
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 <a href="javascript:" target="_blank">jenkinsci-use...@....
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.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/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.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/5a216231.13d4500a.b61be.a2e5%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I inject environment variables into my groovy class?

red 888
"In the pipeline there’s the global variable `env` which is a map of all the currently declared environment variables (JOB_NAME etc) you could pass into your class."

Is there a way to do that so all those envs are imported and accessible in the class like they are system variables? For example if I do a printenv from the pipeline "MyVar" shows up like its a system variable, I want it to show up like that from in my class too. Is there an elegant way of importing all the vars in "env" into my class and making them access like shell variables?


On Friday, December 1, 2017 at 9:08:12 AM UTC-5, Daniel Butler wrote:

If you’re writing a groovy class that’s run from a library in the pipeline script then you’re not going to be able to use System.getEnv().

 

There’s a few approaches you can take that do work:

  • You can pass in the values you need as parameters to the methods/constructors you’re using.
  • In the pipeline there’s the global variable `env` which is a map of all the currently declared environment variables (JOB_NAME etc) you could pass into your class.
  • Pass in `this` from the pipeline script. You can then use this object to access anything in pipeline, (Let’s assume you called the field/parameter ctx) i.e. ctx.env.JOB_NAME or ctx.powershell(“write-host ‘spooky’”)

 

I noticed also you’ve got a direct execution of a process commented out, don’t do that either. The same way System.getEnv() doesn’t work, doing that will not behave as expected either (Assuming the sandbox will let you)

 

 

Regards,

Daniel.

 

From: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="WGVhyRAhBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">red 888
Sent: 29 November 2017 20:28
To: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="WGVhyRAhBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">Jenkins Users
Subject: Re: How can I inject environment variables into my groovy class?

 

So this is a problem for me because the APIs I'm using in the groovy class are looking for OS level environment variables.

 

Is there any way to set these variables with jenkins?

On Tuesday, November 28, 2017 at 5:54:37 PM UTC-5, mpapo - Michael Pailloncy wrote:

Pipeline environment variables are not stored at the OS level. That's why you are not able to get them using System.getenv() (which only list OS level environment variables).

But you should be able to retrieve them like any other classical environment variables : with the "${MyVar}" syntax inside your shared lib class.

 

Hopefully it helps.

 

Michaël

 

2017-11-28 22:30 GMT+01:00 red 888 <[hidden email]>:

My pipeline is using the environment directive to set an environment var and then calls a groovy class:

 

def call(int blah) {

   
pipeline {
       
agent any

        environment
{
            MyVar =
credentials('djsjflsjfljsf')
        }

       
stages {
            stage(
'Stage ONE') {
               
steps {
                    echo test.methodA()
            ....

 

 

But from in my groovy class I don't see those environment variables:

 

// vars/test.groovy
 
class test implements Serializable {
   
static methodA (){
    def env = System.getenv()
    return env.dump()
    //return ['powershell', 'ls env:'].execute().text
}

 

env.dump() and listing envs from the shell from inside the class doesn't show those variables I added in the environment directive. Can I change the scope of the class to give it access to these environment vars?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--
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 jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="WGVhyRAhBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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/f9a07627-f0ce-4007-a4f5-54918f5d712c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: How can I inject environment variables into my groovy class?

Daniel Butler

If you’re dealing with a stubborn library that’s forcing you to use environment variables to pass in parameters you’re in for more pain than it’s worth trying to run it within the pipeline script environment.

 

Use it by running a groovy script externally, e.g.

 

sh “groovy MyScript.groovy”

 

and then you’ve got all the environment variables setup and you’re in a standard groovy environment without the pipeline restrictions and idiosyncrasies.

 

 

 

From: [hidden email]
Sent: 03 December 2017 00:28
To: [hidden email]
Subject: Re: How can I inject environment variables into my groovy class?

 

"In the pipeline there’s the global variable `env` which is a map of all the currently declared environment variables (JOB_NAME etc) you could pass into your class."

 

Is there a way to do that so all those envs are imported and accessible in the class like they are system variables? For example if I do a printenv from the pipeline "MyVar" shows up like its a system variable, I want it to show up like that from in my class too. Is there an elegant way of importing all the vars in "env" into my class and making them access like shell variables?



On Friday, December 1, 2017 at 9:08:12 AM UTC-5, Daniel Butler wrote:

If you’re writing a groovy class that’s run from a library in the pipeline script then you’re not going to be able to use System.getEnv().

 

There’s a few approaches you can take that do work:

  • You can pass in the values you need as parameters to the methods/constructors you’re using.
  • In the pipeline there’s the global variable `env` which is a map of all the currently declared environment variables (JOB_NAME etc) you could pass into your class.
  • Pass in `this` from the pipeline script. You can then use this object to access anything in pipeline, (Let’s assume you called the field/parameter ctx) i.e. ctx.env.JOB_NAME or ctx.powershell(“write-host ‘spooky’”)

 

I noticed also you’ve got a direct execution of a process commented out, don’t do that either. The same way System.getEnv() doesn’t work, doing that will not behave as expected either (Assuming the sandbox will let you)

 

 

Regards,

Daniel.

 

From: <a href="javascript:" target="_blank">red 888
Sent: 29 November 2017 20:28
To: <a href="javascript:" target="_blank">Jenkins Users
Subject: Re: How can I inject environment variables into my groovy class?

 

So this is a problem for me because the APIs I'm using in the groovy class are looking for OS level environment variables.

 

Is there any way to set these variables with jenkins?

On Tuesday, November 28, 2017 at 5:54:37 PM UTC-5, mpapo - Michael Pailloncy wrote:

Pipeline environment variables are not stored at the OS level. That's why you are not able to get them using System.getenv() (which only list OS level environment variables).

But you should be able to retrieve them like any other classical environment variables : with the "${MyVar}" syntax inside your shared lib class.

 

Hopefully it helps.

 

Michaël

 

2017-11-28 22:30 GMT+01:00 red 888 <[hidden email]>:

My pipeline is using the environment directive to set an environment var and then calls a groovy class:

 

def call(int blah) {

   
pipeline {
       
agent any

        environment
{
            MyVar =
credentials('djsjflsjfljsf')
        }

       
stages {
            stage(
'Stage ONE') {
               
steps {
                    echo test.methodA()
            ....

 

 

But from in my groovy class I don't see those environment variables:

 

// vars/test.groovy
 
class test implements Serializable {
   
static methodA (){
    def env = System.getenv()
    return env.dump()
    //return ['powershell', 'ls env:'].execute().text
}

 

env.dump() and listing envs from the shell from inside the class doesn't show those variables I added in the environment directive. Can I change the scope of the class to give it access to these environment vars?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--
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/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.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 <a href="javascript:" target="_blank">jenkinsci-use...@....
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.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/f9a07627-f0ce-4007-a4f5-54918f5d712c%40googlegroups.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/5a2462c7.08e31c0a.7be0f.5794%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I inject environment variables into my groovy class?

red 888
I guess it took longer than it should have to realize I was barking up the wrong tree, but let me explain why I wanted this.

I'm using the AWS java sdk. The AWS sdk looks for these env vars and pulls them in for creds automatically if they are set in the current session. Of course you can pass them in manually, but this feature is quite convenient! I'm calling the AWS SDK from a groovy class like this because its super portable and behaves almost like a lightweight jenkins plugin- I really like the idea of writing reusable api wrappers in groovy classes for the pipeline like this.

The GCP SDK has similar features where it looks for env vars for config.

It just would be nice to be able to define this once in the environment{} directive before calling these api wrapper classes.


On Sunday, December 3, 2017 at 3:47:19 PM UTC-5, Daniel Butler wrote:

If you’re dealing with a stubborn library that’s forcing you to use environment variables to pass in parameters you’re in for more pain than it’s worth trying to run it within the pipeline script environment.

 

Use it by running a groovy script externally, e.g.

 

sh “groovy MyScript.groovy”

 

and then you’ve got all the environment variables setup and you’re in a standard groovy environment without the pipeline restrictions and idiosyncrasies.

 

 

 

From: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="m7-opAHUBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">red 888
Sent: 03 December 2017 00:28
To: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="m7-opAHUBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">Jenkins Users
Subject: Re: How can I inject environment variables into my groovy class?

 

"In the pipeline there’s the global variable `env` which is a map of all the currently declared environment variables (JOB_NAME etc) you could pass into your class."

 

Is there a way to do that so all those envs are imported and accessible in the class like they are system variables? For example if I do a printenv from the pipeline "MyVar" shows up like its a system variable, I want it to show up like that from in my class too. Is there an elegant way of importing all the vars in "env" into my class and making them access like shell variables?



On Friday, December 1, 2017 at 9:08:12 AM UTC-5, Daniel Butler wrote:

If you’re writing a groovy class that’s run from a library in the pipeline script then you’re not going to be able to use System.getEnv().

 

There’s a few approaches you can take that do work:

  • You can pass in the values you need as parameters to the methods/constructors you’re using.
  • In the pipeline there’s the global variable `env` which is a map of all the currently declared environment variables (JOB_NAME etc) you could pass into your class.
  • Pass in `this` from the pipeline script. You can then use this object to access anything in pipeline, (Let’s assume you called the field/parameter ctx) i.e. ctx.env.JOB_NAME or ctx.powershell(“write-host ‘spooky’”)

 

I noticed also you’ve got a direct execution of a process commented out, don’t do that either. The same way System.getEnv() doesn’t work, doing that will not behave as expected either (Assuming the sandbox will let you)

 

 

Regards,

Daniel.

 

From: red 888
Sent: 29 November 2017 20:28
To: Jenkins Users
Subject: Re: How can I inject environment variables into my groovy class?

 

So this is a problem for me because the APIs I'm using in the groovy class are looking for OS level environment variables.

 

Is there any way to set these variables with jenkins?

On Tuesday, November 28, 2017 at 5:54:37 PM UTC-5, mpapo - Michael Pailloncy wrote:

Pipeline environment variables are not stored at the OS level. That's why you are not able to get them using System.getenv() (which only list OS level environment variables).

But you should be able to retrieve them like any other classical environment variables : with the "${MyVar}" syntax inside your shared lib class.

 

Hopefully it helps.

 

Michaël

 

2017-11-28 22:30 GMT+01:00 red 888 <[hidden email]>:

My pipeline is using the environment directive to set an environment var and then calls a groovy class:

 

def call(int blah) {

   
pipeline {
       
agent any

        environment
{
            MyVar =
credentials('djsjflsjfljsf')
        }

       
stages {
            stage(
'Stage ONE') {
               
steps {
                    echo test.methodA()
            ....

 

 

But from in my groovy class I don't see those environment variables:

 

// vars/test.groovy
 
class test implements Serializable {
   
static methodA (){
    def env = System.getenv()
    return env.dump()
    //return ['powershell', 'ls env:'].execute().text
}

 

env.dump() and listing envs from the shell from inside the class doesn't show those variables I added in the environment directive. Can I change the scope of the class to give it access to these environment vars?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--
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 jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/87b6e2b0-ed9a-4ede-b22c-3b06761345e1%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/6e7e1b97-7b9c-404f-9597-8577eb739b96%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="m7-opAHUBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/f9a07627-f0ce-4007-a4f5-54918f5d712c%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/f9a07627-f0ce-4007-a4f5-54918f5d712c%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/f9a07627-f0ce-4007-a4f5-54918f5d712c%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/f9a07627-f0ce-4007-a4f5-54918f5d712c%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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/12604f23-47ef-4049-873b-74977765b42c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.