Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-60284

build_agent_name not working with Global Listener

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • influxdb-plugin
    • None
    • Jenkins 2.190.1
      InfluxDB Plugin 2.0.2
    • 2.5

      I just tried out the Global Listener feature and it seems to work great, except build_agent_name is always blank. This works fine for me if I disable Global Listener and use influxDbPublisher() in my Jenkinsfile so at least there is a workaround. I'm guessing this has something to do with the global listener not seeing the NODE_NAME environment variable.

          [JENKINS-60284] build_agent_name not working with Global Listener

          Andy Taylor added a comment -

          Quick update: I've noticed that build_agent_name is correctly set to 'master' when a job is running on the master, but if a job runs on any of the slaves it is null.

          Andy Taylor added a comment - Quick update: I've noticed that build_agent_name is correctly set to 'master' when a job is running on the master, but if a job runs on any of the slaves it is null.

          Aleksi Simell added a comment -

          Digging a bit deeper into this I noticed that the `build_agent_name` is correctly set in freestyle jobs, but not in pipelines. In `GlobalRunListener.java` there's a part

           

          EnvVars env;
          try {
              env = build.getEnvironment(listener);
          } catch (IOException | InterruptedException e) {
              env = new EnvVars();
          }
          

          So, basically pipelines cannot use build.getEnvironment(listener) and I need to figure a way for pipelines to access environment variables.

           

          Aleksi Simell added a comment - Digging a bit deeper into this I noticed that the `build_agent_name` is correctly set in freestyle jobs, but not in pipelines. In `GlobalRunListener.java` there's a part   EnvVars env; try {     env = build.getEnvironment(listener); } catch (IOException | InterruptedException e) {     env = new EnvVars(); } So, basically pipelines cannot use build.getEnvironment(listener) and I need to figure a way for pipelines to access environment variables.  

          Aleksi Simell added a comment - - edited

          andytaylorzeetta as by the PR mentioned to fix https://issues.jenkins.io/browse/JENKINS-62753, you need to separately make your pipeline job see your environment variables. If you add something like this to your pipeline:

          public class PublishEnvVarAction extends InvisibleAction implements EnvironmentContributingAction {
          
              private final Map<String, String> envOverrides;
          
              public PublishEnvVarAction(final Map<String, String> env) {
                  this.envOverrides = env;
              }
          
              @NonCPS
              @Override
              public void buildEnvironment(Run<?, ?> run, EnvVars env) {
                  if (env != null && envOverrides != null){
                      env.putAllNonNull(envOverrides);
                  }
              }
          }
          
          def publishEnv(Map config) {
               currentBuild.getRawBuild().addAction(new PublishEnvVarAction(config.collectEntries{ k, v -> [(k.toString()) : v.toString()] }))
          }
          

          you can then call it from the pipeline like this

          pipeline {
              agent { label "my_label" }
          
              stages {
                  stage("Do stuff") {
                      steps {
                          script {
                              myVars = [:]
                              myVars["NODE_NAME"] = NODE_NAME
                              publishEnv(myVars)
                          }
                      }
                  }
              }
          }
          

          This makes the global listener find the NODE_NAME environment variable and the field shows correctly.

          Aleksi Simell added a comment - - edited andytaylorzeetta as by the PR mentioned to fix https://issues.jenkins.io/browse/JENKINS-62753 , you need to separately make your pipeline job see your environment variables. If you add something like this to your pipeline: public class PublishEnvVarAction extends InvisibleAction implements EnvironmentContributingAction { private final Map< String , String > envOverrides; public PublishEnvVarAction(final Map< String , String > env) { this .envOverrides = env; } @NonCPS @Override public void buildEnvironment(Run<?, ?> run, EnvVars env) { if (env != null && envOverrides != null ){ env.putAllNonNull(envOverrides); } } } def publishEnv(Map config) { currentBuild.getRawBuild().addAction( new PublishEnvVarAction(config.collectEntries{ k, v -> [(k.toString()) : v.toString()] })) } you can then call it from the pipeline like this pipeline { agent { label "my_label" } stages { stage( "Do stuff" ) { steps { script { myVars = [:] myVars[ "NODE_NAME" ] = NODE_NAME publishEnv(myVars) } } } } } This makes the global listener find the NODE_NAME environment variable and the field shows correctly.

          Aleksi Simell added a comment -

          Closing this as fixed as mentioned in the comments.

          Aleksi Simell added a comment - Closing this as fixed as mentioned in the comments.

            aleksisimell Aleksi Simell
            andytaylorzeetta Andy Taylor
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: