Why aren't these injected variables making it to my groovy class?

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

Why aren't these injected variables making it to my groovy class?

red 888

In my pipeline I'm injecting aws credentials:


withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: '89h9sfh8shf98shf98he9f8sf',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {

    sh 'printenv'

    echo myFunc.myMeth('blah','sdjlfkdf')

}


myFunc looks like this:


class myFunc {
    static Object myMeth (String param1, String param2){


        def env = System.getenv()
        //Print all the environment variables.
        env.each{
            println it
        }
    }
}


Looking at the output of sh 'printenv' I see AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are injected. But when I try to echo back what envs myFunc has access to I don't see those variables. Is this by design? Is myFunc isolated and I need change its scope or something?

--
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/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why aren't these injected variables making it to my groovy class?

Daniel Butler
By doing System.getenv() you're getting the environment of the Jenkins master process.

The environment that will be used by any process run in the pipeline is available through the built-in variable env. 

Regards, 
Daniel 

On 20 Nov 2017 2:14 am, "red 888" <[hidden email]> wrote:

In my pipeline I'm injecting aws credentials:


withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: '89h9sfh8shf98shf98he9f8sf',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {

    sh 'printenv'

    echo myFunc.myMeth('blah','sdjlfkdf')

}


myFunc looks like this:


class myFunc {
    static Object myMeth (String param1, String param2){


        def env = System.getenv()
        //Print all the environment variables.
        env.each{
            println it
        }
    }
}


Looking at the output of sh 'printenv' I see AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are injected. But when I try to echo back what envs myFunc has access to I don't see those variables. Is this by design? Is myFunc isolated and I need change its scope or something?

--
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/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%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/CAO37hbjxXNv7NtZb%3DvLo-sZ-uh3%2Bv_1%2B6Ch4S9oD4j1dCtq9dA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why aren't these injected variables making it to my groovy class?

red 888
Can you explain this i don't understand.

sh 'printenv' returns my injected variables. System.getenv() inside my function does not. Even if I shell out from the groovy function and run printenv I still don't see the variables. Why aren't they there in the context of my function?

The amazon api looks for these variables so when I run this in my function its looking for those two environment vars that should be injected but are not:
        def ebClient = new AmazonDynamoDBClientBuilder()
            .withCredentials(new EnvironmentVariableCredentialsProvider())
            .build()


On Monday, November 20, 2017 at 6:57:07 AM UTC-5, Daniel Butler wrote:
By doing System.getenv() you're getting the environment of the Jenkins master process.

The environment that will be used by any process run in the pipeline is available through the built-in variable env. 

Regards, 
Daniel 

On 20 Nov 2017 2:14 am, "red 888" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="g2ghfFYVAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fakemai...@...> wrote:

In my pipeline I'm injecting aws credentials:


withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: '89h9sfh8shf98shf98he9f8sf',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {

    sh 'printenv'

    echo myFunc.myMeth('blah','sdjlfkdf')

}


myFunc looks like this:


class myFunc {
    static Object myMeth (String param1, String param2){


        def env = System.getenv()
        //Print all the environment variables.
        env.each{
            println it
        }
    }
}


Looking at the output of sh 'printenv' I see AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are injected. But when I try to echo back what envs myFunc has access to I don't see those variables. Is this by design? Is myFunc isolated and I need change its scope or something?

--
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="g2ghfFYVAgAJ" 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/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%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/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%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/89ce7f8b-99ae-4e47-abbb-a57e0f306fcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: Why aren't these injected variables making it to my groovy class?

Daniel Butler

As I’d said the System.getEnv() call returns the environment the Jenkins process is currently running in.

 

When Jenkins runs an external process (ie sh) it adds the values in the env map to the environment of the external process. The environment of the Jenkins process is left untouched.

Thus “sh ‘printenv’” shows the vars (It’s an external process)

System.getEnv()  does not as you’re just getting the Jenkins process environment.

 

If you’re connecting up to Dynamo you’d generally don’t want to do that from within pipeline script; Things are a lot more robust if you do those sorts of things from an external script.

 

The recommendation is to use pipeline script to orchestrate the job but leave the actual heavy lifting to external processes and plugins.

 

 

From: [hidden email]
Sent: 20 November 2017 18:36
To: [hidden email]
Subject: Re: Why aren't these injected variables making it to my groovy class?

 

Can you explain this i don't understand.

 

sh 'printenv' returns my injected variables. System.getenv() inside my function does not. Even if I shell out from the groovy function and run printenv I still don't see the variables. Why aren't they there in the context of my function?

 

The amazon api looks for these variables so when I run this in my function its looking for those two environment vars that should be injected but are not:

        def ebClient = new AmazonDynamoDBClientBuilder()
            .withCredentials(new EnvironmentVariableCredentialsProvider())
            .build()

 


On Monday, November 20, 2017 at 6:57:07 AM UTC-5, Daniel Butler wrote:

By doing System.getenv() you're getting the environment of the Jenkins master process.

 

The environment that will be used by any process run in the pipeline is available through the built-in variable env. 

 

Regards, 

Daniel 

 

On 20 Nov 2017 2:14 am, "red 888" <<a href="javascript:" target="_blank">fakemai...@...> wrote:

In my pipeline I'm injecting aws credentials:

 

withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: '89h9sfh8shf98shf98he9f8sf',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
 
    sh 'printenv'
 
    echo myFunc.myMeth('blah','sdjlfkdf')
 
}

 

myFunc looks like this:

 

class myFunc {
    static Object myMeth (String param1, String param2){
 
 
        def env = System.getenv()
        //Print all the environment variables.
        env.each{
            println it
        }
    }
}

 

Looking at the output of sh 'printenv' I see AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are injected. But when I try to echo back what envs myFunc has access to I don't see those variables. Is this by design? Is myFunc isolated and I need change its scope or something?

--
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/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%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/89ce7f8b-99ae-4e47-abbb-a57e0f306fcc%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/5a1400f0.cebe500a.3a6dc.5945%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why aren't these injected variables making it to my groovy class?

red 888
It is external in the sense that its a self contained groovy script/class that jenkins executes.

So how can I have withCredentials inject these vars into the environment of the main process?

On Tuesday, November 21, 2017 at 5:33:36 AM UTC-5, Daniel Butler wrote:

As I’d said the System.getEnv() call returns the environment the Jenkins process is currently running in.

 

When Jenkins runs an external process (ie sh) it adds the values in the env map to the environment of the external process. The environment of the Jenkins process is left untouched.

Thus “sh ‘printenv’” shows the vars (It’s an external process)

System.getEnv()  does not as you’re just getting the Jenkins process environment.

 

If you’re connecting up to Dynamo you’d generally don’t want to do that from within pipeline script; Things are a lot more robust if you do those sorts of things from an external script.

 

The recommendation is to use pipeline script to orchestrate the job but leave the actual heavy lifting to external processes and plugins.

 

 

From: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="dkOEaFxfAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">red 888
Sent: 20 November 2017 18:36
To: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="dkOEaFxfAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">Jenkins Users
Subject: Re: Why aren't these injected variables making it to my groovy class?

 

Can you explain this i don't understand.

 

sh 'printenv' returns my injected variables. System.getenv() inside my function does not. Even if I shell out from the groovy function and run printenv I still don't see the variables. Why aren't they there in the context of my function?

 

The amazon api looks for these variables so when I run this in my function its looking for those two environment vars that should be injected but are not:

        def ebClient = new AmazonDynamoDBClientBuilder()
            .withCredentials(new EnvironmentVariableCredentialsProvider())
            .build()

 


On Monday, November 20, 2017 at 6:57:07 AM UTC-5, Daniel Butler wrote:

By doing System.getenv() you're getting the environment of the Jenkins master process.

 

The environment that will be used by any process run in the pipeline is available through the built-in variable env. 

 

Regards, 

Daniel 

 

On 20 Nov 2017 2:14 am, "red 888" <[hidden email]> wrote:

In my pipeline I'm injecting aws credentials:

 

withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: '89h9sfh8shf98shf98he9f8sf',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
 
    sh 'printenv'
 
    echo myFunc.myMeth('blah','sdjlfkdf')
 
}

 

myFunc looks like this:

 

class myFunc {
    static Object myMeth (String param1, String param2){
 
 
        def env = System.getenv()
        //Print all the environment variables.
        env.each{
            println it
        }
    }
}

 

Looking at the output of sh 'printenv' I see AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are injected. But when I try to echo back what envs myFunc has access to I don't see those variables. Is this by design? Is myFunc isolated and I need change its scope or something?

--
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/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%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/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/6dfaad11-29f5-4287-a0a0-a16a2373ecd4%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="dkOEaFxfAgAJ" 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/89ce7f8b-99ae-4e47-abbb-a57e0f306fcc%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/89ce7f8b-99ae-4e47-abbb-a57e0f306fcc%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/89ce7f8b-99ae-4e47-abbb-a57e0f306fcc%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/89ce7f8b-99ae-4e47-abbb-a57e0f306fcc%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/11993c26-a1e9-4442-8dcf-6e5b4d385ff8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.