Environment Variables in Jenkins Plugin

113 views
Skip to first unread message

Sourabh Jain

unread,
Aug 31, 2020, 8:44:05 AM8/31/20
to Jenkins Developers
Hi Team,

How Can I get the Environment Variables that are created in a Jenkins Pipeline.

    environment {
        ENV_V_1 = "SASbv"
    }

For now when I try to use 

EnvVars environment = run.getEnvironment(listener);

It fetches only the Environment variables attached with the Jenkins Job.

Pointer to any code or link will be helpful.

Thanks,
Sourabh Jain
Maintainer - sumologic-publisher plugin

Jesse Glick

unread,
Aug 31, 2020, 10:19:40 AM8/31/20
to Jenkins Dev
On Mon, Aug 31, 2020 at 8:44 AM Sourabh Jain <sou...@sumologic.com> wrote:
> How Can I get the Environment Variables that are created in a Jenkins Pipeline.

If you mean from within a Pipeline step, you should use

stepContext.get(EnvVars.class)

which will look up variables defined in a particular context.

Sourabh Jain

unread,
Aug 31, 2020, 11:05:45 AM8/31/20
to jenkin...@googlegroups.com
Also, is there a class which we can use, extend or implement which can be run by default for each stage in a Jenkins pipeline(without modifying the pipeline)?

On Mon, Aug 31, 2020 at 8:29 PM Sourabh Jain <sou...@sumologic.com> wrote:
Thanks for a quick response.

So the context is lost after the job is completed..rgt.

Thus, running "run.getEnvironment(listener);" does not give all the environment  variables when the job is completed and we try to fetch the code.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/CDKvVuClyJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr0Z%3DaLD%3DKiLD9WKbUZJ%2BVFHmugy8JEG8DL8Wc-nZrMjvA%40mail.gmail.com.


--
Best Regards,
Sourabh Jain
Integration Engineer, APP DEV team,



--
Best Regards,
Sourabh Jain
Integration Engineer, APP DEV team,

Sourabh Jain

unread,
Aug 31, 2020, 11:06:03 AM8/31/20
to jenkin...@googlegroups.com
Thanks for a quick response.

So the context is lost after the job is completed..rgt.

Thus, running "run.getEnvironment(listener);" does not give all the environment  variables when the job is completed and we try to fetch the code.

On Mon, Aug 31, 2020 at 7:49 PM Jesse Glick <jgl...@cloudbees.com> wrote:
--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/CDKvVuClyJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr0Z%3DaLD%3DKiLD9WKbUZJ%2BVFHmugy8JEG8DL8Wc-nZrMjvA%40mail.gmail.com.

Jesse Glick

unread,
Aug 31, 2020, 1:24:46 PM8/31/20
to Jenkins Dev
On Mon, Aug 31, 2020 at 11:05 AM Sourabh Jain <sou...@sumologic.com> wrote:
> is there a class which we can use, extend or implement which can be run by default for each stage in a Jenkins pipeline(without modifying the pipeline)?

https://javadoc.jenkins.io/plugin/workflow-api/org/jenkinsci/plugins/workflow/flow/GraphListener.html

Sourabh Jain

unread,
Dec 13, 2020, 7:14:24 AM12/13/20
to Jenkins Developers
Hi Jesse,

I have a pipeline as below

/**
 * [Pipeline home: BUILD-JENKINS]
 * Required named parameters: assemblyGroup
 */
pipeline {
    agent none
    options {
      timeout(time: 2, unit: 'HOURS')
    }
    stages {
      stage("[Release]") {
          environment {
                Sumo_event_type = "bar"
            }
        stages {
          stage("Prepare") {
            agent any
            steps {
              script {
                echo "Performing Git Checkout $Sumo_event_type"
              }
            }
          }
        }
      }
      stage("[Deploy]") {
        stages {
          stage("Testing") {
            agent any
            steps {
              script {
                echo "building using maven"
                sumoAgrEnv("sourabh-CheckIn", "check")
              }
            }
          }
        }
      }
    }
}

def sumoAgrEnv(String assemblyGroup, String branch){
    env.Sumo_GIT_BRANCH = branch
    env.Sumo_SERVICE_NAME = assemblyGroup
    if (env.Sumo_GIT_BRANCH == 'master') {
        env.Sumo_SERVICE_NAME = 'test-sourabh'
    } else {
       env.Sumo_SERVICE_NAME = 'test' +  env.SUMO_GIT_BRANCH
    }
}


As per the pipeline
- In stage "Release" I am creating environment as Sumo_event_type which should be available to only Release and prepare stage.
- In "Testing" I am creating env. which should be only available to testing stage.

So, is there an API that I can use to fetch the environment variables at each stage level?

Jesse Glick

unread,
Dec 14, 2020, 11:53:32 AM12/14/20
to Jenkins Dev
Not exactly sure what you are asking for. API from a plugin? Defining
a step? Some other kind of listener?

BTW do not use

env.KEY = value

Use the `withEnv` step.

Sourabh Jain

unread,
Dec 15, 2020, 2:16:00 AM12/15/20
to jenkin...@googlegroups.com
Hi Jesse,

Thanks for suggestions.

I will use withenv or environment block to set environment variables.

But how to access the class which implements graphlistener class? I tried using run.getAction(EnvActionImpl.class); but it does not have the environment variables.

Please advise.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/CDKvVuClyJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

Jesse Glick

unread,
Dec 15, 2020, 11:44:31 AM12/15/20
to Jenkins Dev
On Tue, Dec 15, 2020 at 2:15 AM Sourabh Jain <sou...@sumologic.com> wrote:
I tried using run.getAction(EnvActionImpl.class); but it does not have the environment variables.

No, do not use that. It would only have global environment variables (and not even all of those). `StepContext.get(EnvVars.class)` is used from within a step implementation to access contextual variables.

Sourabh Jain

unread,
Dec 15, 2020, 1:01:24 PM12/15/20
to jenkin...@googlegroups.com
Hi Jesse,

Interesting, but do the flownode object in graph listener have the step context?

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/CDKvVuClyJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

Jesse Glick

unread,
Dec 15, 2020, 3:29:23 PM12/15/20
to Jenkins Dev
On Tue, Dec 15, 2020 at 1:01 PM Sourabh Jain <sou...@sumologic.com> wrote:
> do the flownode object in graph listener have the step context?

No. There is a `StepListener` that does, but this smells like the
wrong approach to whatever problem it is you are trying to solve,
which you have never explained.

Sourabh Jain

unread,
Dec 16, 2020, 12:31:37 AM12/16/20
to jenkin...@googlegroups.com
Hi Jesse,

The problem we have is to send some data(all environment variables, global or local) after each stage is completed in a jenkins Pipeline.

So, we are using GraphListener to listen to every stage end and then send env variables to a HTTP url.

But we are not getting all environment variables as explained previously.

I hope the above helps.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/CDKvVuClyJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

Björn Pedersen

unread,
Dec 16, 2020, 6:40:57 AM12/16/20
to Jenkins Developers
This seems flawed in 2 ways:
 1) As a stage may contain many steps the env vars at the end of the stage may not be what the main workload saw
   

      stage ('xxx') {
           withEnv(XXX=1) {
                   do work
           }
         withEnv(XXX=2) {}
      } <== XXX has a non-interesting value

2) Security: The collection mechanism needs to carefully filter all 'sensitive' variables (akin to https://www.jenkins.io/security/advisory/2018-02-26/#SECURITY-248

Sourabh Jain

unread,
Dec 16, 2020, 6:53:44 AM12/16/20
to jenkin...@googlegroups.com
Yes, for security reasons we will only send the env variables that will start with some prefix like Sumo_, others will be discarded.

Also, even if a stage has lot of env variables it will not matter to us, as we would like to send all env variables prefixed with Sumo_ in that particular stage.

Jesse Glick

unread,
Dec 16, 2020, 7:01:42 AM12/16/20
to Jenkins Dev
On Wed, Dec 16, 2020 at 12:31 AM Sourabh Jain <sou...@sumologic.com> wrote:
send some data(all environment variables, global or local) after each stage is completed in a jenkins Pipeline.

I do not believe there is any supported way to do this. Environment variables are intended as an implementation detail in particular blocks, for use solely by steps, not exposed as metadata in the flow graph. You will need to rethink your design at a higher level. Again, from your messages I cannot offer real advice, because you are describing what you are attempting to do but not what user problem you are attempting to solve or why you selected this approach.

Sourabh Jain

unread,
Dec 16, 2020, 7:36:34 AM12/16/20
to jenkin...@googlegroups.com
Yes, we ask our user to send a deploy event or build event (based on a task performed in an stage). We already have sumoupload function that can be used as a step to send the map (which is mostly env variables) to Sumo Logic.

What we were trying to achieve is to remove the overhead of calling the sumoupload step and identifying a way where plugin can send the same data after the stage is complete.

I hope the above helps.
--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/CDKvVuClyJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

Jesse Glick

unread,
Dec 16, 2020, 7:49:01 AM12/16/20
to Jenkins Dev
On Wed, Dec 16, 2020 at 7:36 AM Sourabh Jain <sou...@sumologic.com> wrote:
We already have sumoupload function that can be used as a step to send the map (which is mostly env variables) to Sumo Logic.

What we were trying to achieve is to remove the overhead of calling the sumoupload step and identifying a way where plugin can send the same data after the stage is complete.

That clears things up, yes. You cannot do this. Be transparent and send events if and when your step is called.

Sourabh Jain

unread,
Dec 16, 2020, 8:38:06 AM12/16/20
to jenkin...@googlegroups.com
Thanks Jesse for clarifcation.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/CDKvVuClyJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

Sourabh Jain

unread,
Dec 17, 2020, 1:56:13 AM12/17/20
to jenkin...@googlegroups.com
Hi Jesse,


It is a small file to read from step context, identify parent stage and gather the data. Can you please give it a quick look to see if everything is in order?

Jesse Glick

unread,
Dec 17, 2020, 1:25:57 PM12/17/20
to Jenkins Dev
From a quick glance it seems OK. A `Run` can be gotten from a `StepContext` BTW.
Reply all
Reply to author
Forward
0 new messages