Trying to support pipeline, can't read env vars or build result.

28 views
Skip to first unread message

Prabhas Gupte

unread,
Jul 11, 2017, 10:54:33 AM7/11/17
to Jenkins Developers
Hi, 
I am completely new to Jenkins plugin development, and have no prior experience with this.

I have a post-build action, which I am trying to support in pipeline. 

My class heading reads like this:
public class GetDataNotifier extends Notifier implements SimpleBuildStep {
...

I also have:
@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener taskListener) throws InterruptedException, IOException {
...

In this perform method, I need to do this:
Check build result, and if it is successful, read a user defined env var named PACKAGE_ID and use it to make some external API call. 

To read the build result, I am trying this:
Result r = run.getResult();
String result = r.toString();
taskListener.getLogger().println("Build Result = " + result);

And, to get that PACKAGE_ID, I am trying this:
EnvVars envVars = run.getEnvironment(listener);
packageId = envVars.get("PACKAGE_ID", "");
taskListener.getLogger().println("PACKAGE_ID read from EnvVars is " + packageId);

My pipeline script is like this:
pipeline {
     environment { 
        PACKAGE_ID = 'coming_from_environment'
    }
    
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post { 
        always { 
            echo 'in post build stage'
            step([$class: 'GetDataNotifier', apiPass: 'password', apiServer: 'https://api.example.com', apiUser: 'user'])
        }
    }
}

However, I get neither Result r (its found to be null always), nor the env var PACKAGE_ID - it is never found. 
How can I that PACKAGE_ID at least? 

I have came across this link, but I am not sure how that can be done. 
I also referred this answer on another message in this group, but I still have no clue how I can get Step class that works with pipeline and what all things I need to do for that. 
According to this ticket, It seems EnvVars are not available to SimpleBuildStep, but then how that can be achieved in similarly simple way?

Slide

unread,
Jul 11, 2017, 12:27:06 PM7/11/17
to Jenkins Developers
If the result is null, then you can consider it to be success.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/5fceec96-8056-4e1e-ac72-e9d527f9b372%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Prabhas Gupte

unread,
Jul 12, 2017, 3:18:51 AM7/12/17
to Jenkins Developers
That is fine. I can ignore the result for now. But getting that PACKAGE_ID Env Var is my top-most priority at this moment. Any help in that direction would be appreciated. 

Jesse Glick

unread,
Jul 12, 2017, 10:32:38 AM7/12/17
to Jenkins Dev
On Tue, Jul 11, 2017 at 10:34 AM, Prabhas Gupte <prabha...@gmail.com> wrote:
> read a user defined env var

Not possible: https://issues.jenkins-ci.org/browse/JENKINS-29144
Reply all
Reply to author
Forward
0 new messages