Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support env variable configuration in global listener [JENKINS-62753] #105

Merged
merged 2 commits into from Sep 24, 2020

Conversation

agaudreault
Copy link
Contributor

@agaudreault agaudreault commented Aug 26, 2020

Fix: https://issues.jenkins-ci.org/browse/JENKINS-62753

Example Pipeline

This is the pipeline I used to test

@Library('jenkins-pipeline-library@test-influx') _

import com.example.action.PublishEnvVarAction

pipeline {
  options {
    timestamps()
    skipDefaultCheckout()
  }
  
  agent { label "master" as String }

  stages {
    stage ("Test") {
      steps {
        script {
          publishEnv([
            "INFLUXDB_PLUGIN_CUSTOM_TAGS": 'my_tag=${TEST_REPLACE}\nmultiline=works', 
            "TEST_REPLACE": "foo",
            "INFLUXDB_PLUGIN_CUSTOM_FIELDS": 'my_field=1',
            "INFLUXDB_PLUGIN_CUSTOM_PROJECT_NAME": "custom-test-project",
            "INFLUXDB_PLUGIN_CUSTOM_PREFIX": "custom/prefix"
          ])
        }
      }
    }
  }
}

Note that publishEnv is defined in a shared library

File: vars/publishEnv.groovy

package com.example

import com.example.action.PublishEnvVarAction

def call(Map config) {
     currentBuild.getRawBuild().addAction(new PublishEnvVarAction(config.collectEntries{ k, v -> [(k.toString()) : v.toString()] }))
}

File: scr/com/example/action/PublishEnvVarAction.groovy

package com.example.action;

import hudson.EnvVars;
import hudson.model.Run;
import hudson.model.EnvironmentContributingAction;
import hudson.model.InvisibleAction;

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);
        }
    }
}

The class PublishEnvVarAction will need to be whitelisted for serialization with -Dhudson.remoting.ClassFilter=com.example.action.PublishEnvVarAction or you will get an error like Refusing to marshal com.example.action.PublishEnvVarAction for security reasons; see https://jenkins.io/redirect/class-filter/

Result

I can see the 2 tags multiline=works and my_tag=foo expanded correctly with multiline working.
The custom field my_field="1" is present.
Both prefix and project_name values are the proper one.

jenkins_data,
build_result=SUCCESS,multiline=works,my_tag=foo,prefix=custom/prefix,project_name=custom/prefix_custom-test-project,project_path=agaudreault/tests/influx-db 
build_agent_name="",build_scheduled_time=1598555993110i,project_build_health=80i,project_path="agaudreault/tests/influx-db",time_in_queue=15i,build_exec_time=1598555993142i,build_result="SUCCESS",last_stable_build=4i,my_field="1",project_name="custom/prefix_custom-test-project",build_causer="Started by user Alexandre Gaudreault",build_measured_time=1598556002618i,build_number=4i,build_status_message="stable",last_successful_build=4i,build_branch_name="",build_result_ordinal=0i,build_successful=true,build_time=9409i
1598556002618000000

TODOs

  • Test it

@agaudreault
Copy link
Contributor Author

@asimell You are an assignee on Jira so perhaps you want to take a look.

README.md Outdated Show resolved Hide resolved
@asimell
Copy link
Contributor

asimell commented Aug 27, 2020

@agaudreault-jive Thank you so much for the PR! I started working on this a few weeks ago, but got distracted once again and almost forgot about it. Without testing the PR looks good already, I really appreciate the updated documentation even for parts that aren't really part of this feature.

I'll test this out and merge if everything works as expeced.

Clarify that only tags are for all metrics
@agaudreault agaudreault marked this pull request as ready for review August 27, 2020 19:53
@agaudreault
Copy link
Contributor Author

@asimell tested on my side and everything is working as expected. I added instructions in the PR description that might be useful.

I hope this can be part of 2.5 release soon 😄 🚀

@agaudreault agaudreault changed the title feat: support env variable configuration in global listener feat: support env variable configuration in global listener [JENKINS-62753] Aug 28, 2020
@agaudreault agaudreault changed the title feat: support env variable configuration in global listener [JENKINS-62753] support env variable configuration in global listener [JENKINS-62753] Aug 28, 2020
@asimell
Copy link
Contributor

asimell commented Aug 31, 2020

@agaudreault-jive Am I assuming correctly here that I can't simply set the environment variables in "traditional" pipeline manner

pipeline {
    environment {
        ...
    }
...
}

and in order to use this feature I should create a separate library just to extend the environment variables into the onCompleted in GlobalRunListener.java? This works fine in freestyle jobs without having to create anything extra, which is the behaviour I would also like to see in the pipeline version.

@agaudreault
Copy link
Contributor Author

agaudreault commented Aug 31, 2020

@asimell Correct. From what I tried, the "traditional" way will simply set the variable for the duration of the pipeline, but they won't be available on the Run object. It is available on the AbstractRun, but this is not used with workflow pipelines.

I don't know if there is another way to set the environment variables in a way that they are persisted outside the pipeline, but I do not think this should be part of this plugin. Perhaps there’s a way with the EnvInject plugin, but this one is up for adoption and pipeline syntax is not much documented.

@agaudreault
Copy link
Contributor Author

@asimell Is there another blocker for this PR ?

@asimell
Copy link
Contributor

asimell commented Sep 18, 2020

@agaudreault-jive No other blockers, just my time allocation 😅. My civil life got slightly "in the way" of having any energy to look at this. Sorry for the delay, I'll try to check this as soon as possible.

Copy link
Contributor

@asimell asimell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After getting all the permissions set to Jenkins, this works like a charm.

@asimell asimell merged commit 02eeb1b into jenkinsci:development Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants