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

JENKINS-61735 Get all environment variable #92

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.EnvVars;
import hudson.Extension;
import hudson.console.ConsoleLogFilter;
import hudson.model.AbstractBuild;
Expand All @@ -16,14 +17,16 @@
public class LogstashConsoleLogFilter extends ConsoleLogFilter implements Serializable
{

private EnvVars envVars;
private static final Logger LOGGER = Logger.getLogger(LogstashConsoleLogFilter.class.getName());

private transient Run<?, ?> run;
public LogstashConsoleLogFilter() {}

public LogstashConsoleLogFilter(Run<?, ?> run)
public LogstashConsoleLogFilter(Run<?, ?> run, EnvVars envVars)
{
this.run = run;
this.envVars = envVars;
}
private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -62,7 +65,7 @@ public OutputStream decorateLogger(Run build, OutputStream logger) throws IOExce

LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream)
{
return new LogstashWriter(build, errorStream, null, build.getCharset());
return new LogstashWriter(build, errorStream, null, build.getCharset(), envVars);
}

private boolean isLogstashEnabled(Run<?, ?> build)
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/jenkins/plugins/logstash/LogstashWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package jenkins.plugins.logstash;


import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.model.Run;
Expand Down Expand Up @@ -61,8 +62,26 @@ public class LogstashWriter {
private final LogstashIndexerDao dao;
private boolean connectionBroken;
private final Charset charset;
private final EnvVars envVars;

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset) {
this.envVars = null;
this.errorStream = error != null ? error : System.err;
this.build = run;
this.listener = listener;
this.charset = charset;
this.dao = this.getDaoOrNull();
if (this.dao == null) {
this.jenkinsUrl = "";
this.buildData = null;
} else {
this.jenkinsUrl = getJenkinsUrl();
this.buildData = getBuildData();
}
}

Choose a reason for hiding this comment

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

This is a duplication of code. Instead just call the new constructor forwarding all arguments and pass null for the envvars

Copy link
Author

Choose a reason for hiding this comment

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

OK


public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, EnvVars envVars) {
this.envVars = envVars;
this.errorStream = error != null ? error : System.err;
this.build = run;
this.listener = listener;
Expand Down Expand Up @@ -154,7 +173,7 @@ BuildData getBuildData() {
if (build instanceof AbstractBuild) {
return new BuildData((AbstractBuild<?, ?>) build, new Date(), listener);
} else {
return new BuildData(build, new Date(), listener);
return new BuildData(build, new Date(), listener, envVars);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package jenkins.plugins.logstash.persistence;

import hudson.EnvVars;
import hudson.model.Action;
import hudson.model.Environment;
import hudson.model.Executor;
Expand Down Expand Up @@ -212,20 +213,23 @@ public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener liste
}

// Pipeline project build
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener) {
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, EnvVars envVars) {
initData(build, currentTime);

rootProjectName = projectName;
rootFullProjectName = fullProjectName;
rootProjectDisplayName = displayName;
rootBuildNum = buildNum;

try {
// TODO: sensitive variables are not filtered, c.f. https://stackoverflow.com/questions/30916085
buildVariables = build.getEnvironment(listener);
} catch (IOException | InterruptedException e) {
LOGGER.log(WARNING,"Unable to get environment for " + build.getDisplayName(),e);
buildVariables = new HashMap<>();
if (envVars != null) {
buildVariables = envVars;
} else {
try {
// TODO: sensitive variables are not filtered, c.f. https://stackoverflow.com/questions/30916085
buildVariables = build.getEnvironment(listener);
} catch (IOException | InterruptedException e) {
LOGGER.log(WARNING,"Unable to get environment for " + build.getDisplayName(),e);
buildVariables = new HashMap<>();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected Void run() throws Exception
Run<?, ?> run = getContext().get(Run.class);
TaskListener listener = getContext().get(TaskListener.class);
PrintStream errorStream = listener.getLogger();
LogstashWriter logstash = new LogstashWriter(run, errorStream, listener, run.getCharset());
LogstashWriter logstash = new LogstashWriter(run, errorStream, listener, run.getCharset(), null);
logstash.writeBuildLog(maxLines);
if (failBuild && logstash.isConnectionBroken())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.EnvVars;
import hudson.Extension;
import hudson.console.ConsoleLogFilter;
import hudson.model.Run;
Expand Down Expand Up @@ -59,6 +60,7 @@ public boolean start() throws Exception {
context
.newBodyInvoker()
.withContext(createConsoleLogFilter(context))
.withContext(context.get(EnvVars.class))
.withCallback(BodyExecutionCallback.wrap(context))
.start();
return false;
Expand All @@ -68,7 +70,8 @@ private ConsoleLogFilter createConsoleLogFilter(StepContext context)
throws IOException, InterruptedException {
ConsoleLogFilter original = context.get(ConsoleLogFilter.class);
Run<?, ?> build = context.get(Run.class);
ConsoleLogFilter subsequent = new LogstashConsoleLogFilter(build);
EnvVars envVars = context.get(EnvVars.class);
ConsoleLogFilter subsequent = new LogstashConsoleLogFilter(build, envVars);
return BodyInvoker.mergeConsoleLogFilters(original, subsequent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static LogstashWriter createLogstashWriter(final AbstractBuild<?, ?> testBuild,
final String url,
final LogstashIndexerDao indexer,
final BuildData data) {
return new LogstashWriter(testBuild, error, null, testBuild.getCharset()) {
return new LogstashWriter(testBuild, error, null, testBuild.getCharset(), null) {
@Override
LogstashIndexerDao getIndexerDao() {
return indexer;
Expand Down