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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ under jenkinsci.plugins.influxdb.**InfluxDbStep**.DescriptorImpl.

## Configuration

### Configuration as Code

To configure InfluxDB plugin in Jenkins add the following to `jenkins.yaml`:

```yaml
unclassified:
influxDbGlobalConfig:
targets:
- database: "some_database"
description: "some description"
exposeExceptions: true
globalListener: true
globalListenerFilter: "some filter"
jobScheduledTimeAsPointsTimestamp: true
password: "some password"
retentionPolicy: "some_policy"
url: "http://some/url"
username: "some username"
usingJenkinsProxy: true
```

### Via Jenkins UI

Create a database in InfluxDB and a user with access rights. In Jenkins,
Expand Down Expand Up @@ -91,23 +112,36 @@ influxdb.save()

## Usage

### Global Listener

When `globalListener` is set to `true` for a target in which no results were published during the build, it will automatically publish the result for this target when the build is completed.

To configure the global listener, you can use environment variables prefixed with `INFLUXDB_PLUGIN`. The following variables are supported and all correspond to an `influxDbPublisher` optional parameter.

- `INFLUXDB_PLUGIN_CUSTOM_PROJECT_NAME` -> `customProjectName`
- `INFLUXDB_PLUGIN_CUSTOM_PREFIX` -> `customPrefix`
- `INFLUXDB_PLUGIN_CUSTOM_FIELDS` -> `jenkinsEnvParameterField`
- `INFLUXDB_PLUGIN_CUSTOM_TAGS` -> `jenkinsEnvParameterTag`

**_NOTE:_** The environment variables must be set on the final build object. If you are creating or updating these variables in a pipeline, you should make sure they are exported with an [EnvironmentContributingAction](https://javadoc.jenkins.io/hudson/model/EnvironmentContributingAction.html).

### Freestyle Jobs

Select the InfluxDB target you wish to publish the data to.

![](doc/img/select-influxdb-target.png)

From the "Advanced" tab you can choose to set a custom prefix for your `project_name` field,
a custom project name to be used instead of the default job name and custom fields and tags
for your `jenkins_data` metric.
a custom project name to be used instead of the default job name, custom fields for your
`jenkins_data` metric, and custom tags for your all your metrics.

![](doc/img/advanced-options.png)

### Pipelines

The plugin can be used by calling either the `influxDbPublisher()` or the `step()` function.

The `influxDbPublisher()` function is only supported from version 1.21 onwards.
**_NOTE:_** The `influxDbPublisher()` function is only supported from version 1.21 onwards.

**Pipeline syntax**

Expand All @@ -131,7 +165,7 @@ Optional parameters
- `customDataMap` (Map) - custom fields in custom measurements
- `customDataMapTags` (Map) - custom tags in custom measurements
- `jenkinsEnvParameterField` (String) - custom fields in "jenkins_data" measurement (newline-separated KEY=VALUE pairs)
- `jenkinsEnvParameterTag` (String) - custom tags in "jenkins_data" measurement (newline-separated KEY=VALUE pairs)
- `jenkinsEnvParameterTag` (String) - custom tags in all measurements (newline-separated KEY=VALUE pairs)
- `measurementName` (String) - custom measurement name (replaces default "jenkins_data" and "jenkins_custom_data")

All `customData*` parameters contain custom data generated during the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class InfluxDbPublicationService {
private final String jenkinsEnvParameterField;

/**
* Jenkins parameter(s) which will be added as tag set to measurement 'jenkins_data'.
* Jenkins parameter(s) which will be added as tag set to all measurements.
* If parameter value has a $-prefix, it will be resolved from current Jenkins job environment properties.
*/
private final String jenkinsEnvParameterTag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
@Extension
public class GlobalRunListener extends RunListener<Run<?, ?>> {

private static final String VARIABLE_PREFIX = "INFLUXDB_PLUGIN_";

@Override
public void onCompleted(Run<?, ?> build, @Nonnull TaskListener listener) {
// Gets the full path of the build's project
Expand All @@ -42,28 +44,29 @@ public void onCompleted(Run<?, ?> build, @Nonnull TaskListener listener) {
}
// If some targets are selected
if (!selectedTargets.isEmpty()) {

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

// Creates the publication service
InfluxDbPublicationService publicationService = new InfluxDbPublicationService(
selectedTargets,
null,
null,
env.get(VARIABLE_PREFIX + "CUSTOM_PROJECT_NAME"),
env.get(VARIABLE_PREFIX + "CUSTOM_PREFIX"),
null,
null,
null,
null,
System.currentTimeMillis() * 1000000,
null,
null,
env.expand(env.get(VARIABLE_PREFIX + "CUSTOM_FIELDS")),
env.expand(env.get(VARIABLE_PREFIX + "CUSTOM_TAGS")),
"jenkins_data"
);

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

// Publication
publicationService.perform(build, listener, env);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Custom tag set that will be added to the default measurement 'jenkins_data', configured as key-value pairs (one per line, in Java Properties file format).
Custom tag set that will be added to all measurements, configured as key-value pairs (one per line, in Java Properties file format).
<p>Current build parameters and/or environment variables can be used in the form of ${PARAM}</p>
<ul>
<li>KEY=${PARAM}</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Custom tag set that will be added to the default measurement 'jenkins_data', configured as key-value pairs (one per line, in Java Properties file format).
Custom tag set that will be added to all measurements, configured as key-value pairs (one per line, in Java Properties file format).
<p>Current build parameters and/or environment variables can be used in the form of ${PARAM}</p>
<ul>
<li>KEY=${PARAM}</li>
Expand Down