Skip to content

Commit

Permalink
Merge pull request #1039 from relative-ci/update-environment-variable…
Browse files Browse the repository at this point in the history
…s-logging

Improve env vars resolving
  • Loading branch information
vio authored Dec 10, 2023
2 parents b304749 + 2414667 commit bdf8476
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 46 deletions.
48 changes: 24 additions & 24 deletions src/agent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @typedef {import('../').AgentConfig} AgentConfig
* @typedef {import('../').AgentArgs} AgentArgs
* @typedef {import('../').EnvVars} EnvVars
*/
import dotenv from 'dotenv';
import { set } from 'lodash';
Expand All @@ -10,10 +11,9 @@ import packageInfo from '../package.json';
import * as LOCALES from '../locales/en';
import send from './send';
import {
debug, getCommitMessage, getEnvCI,
debug, getCommitMessage, getEnvVars,
} from './utils';

const DEFAULT_ENDPOINT = 'https://api.relative-ci.com/save';
const WEBPACK_STATS = 'webpack.stats';
const SOURCE_EXTRACTORS = {
[WEBPACK_STATS]: filter,
Expand All @@ -33,35 +33,35 @@ const getFilteredData = (artifactsData) => artifactsData.reduce(
*/
export const agent = (artifactsData, config, args = {}, logger = console) => {
dotenv.config();
const envCIVars = getEnvCI();
debug('env-ci environment variables', envCIVars);

// Resolved params
const envVars = {
slug: args.slug || envCIVars.slug,
// env-ci is reporting the branch of the PR as prBranch
branch: args.branch || envCIVars.branch,
pr: args.pr || envCIVars.pr,
commit: args.commit || envCIVars.commit,

build: envCIVars.build,
buildUrl: envCIVars.buildUrl,
isCi: envCIVars.isCi,
service: envCIVars.service,

commitMessage: args.commitMessage || envCIVars.commitMessage,

const envVars = getEnvVars();

// Normalized env vars - merge provided args with env vars
// @type {EnvVars}
const normalizedEnvVars = {
slug: args.slug || envVars.slug,
branch: args.branch || envVars.branch,
pr: args.pr || envVars.pr,
commit: args.commit || envVars.commit,

build: envVars.build,
buildUrl: envVars.buildUrl,
isCi: envVars.isCi,
service: envVars.service,

commitMessage: args.commitMessage || envVars.commitMessage,
key: envVars.key,
endpoint: envVars.endpoint,
};

debug('resolved parameters', envVars);
debug('normalized env vars ', normalizedEnvVars);

const { includeCommitMessage } = config;

const params = {
agentVersion: packageInfo.version,

key: process.env.RELATIVE_CI_KEY,
endpoint: process.env.RELATIVE_CI_ENDPOINT || DEFAULT_ENDPOINT,

...envVars,
...normalizedEnvVars,

// Get commit message using git if includeCommitMessage is set and
// there is no --commit-message argument
Expand Down
56 changes: 37 additions & 19 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/**
* @typedef {import('../').EnvCI} EnvCI
* @typedef {import('../').EnvVars} EnvVars
*/
import childProcess from 'child_process';
import envCI from 'env-ci';
import getDebug from 'debug';

const DEFAULT_ENDPOINT = 'https://api.relative-ci.com/save';

export const debug = getDebug('relative-ci:agent');

export function getCommitMessage() {
Expand Down Expand Up @@ -55,11 +57,6 @@ export function extractRepoSlug(repoURL) {
* @returns {string}
*/
function resolveSlug(envVars) {
// Use custom slug environment variable if available
if (process.env.RELATIVE_CI_SLUG) {
return process.env.RELATIVE_CI_SLUG;
}

// env-ci does not provide a slug for jenkins
// https://github.com/semantic-release/env-ci/blob/master/services/jenkins.js#LL18
// https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
Expand All @@ -78,22 +75,43 @@ function resolveSlug(envVars) {
}

/**
* Extract CI environment variables using env-ci
* @returns {EnvCI}
* Extract CI environment variables using env-ci and custom fallback env vars
* @returns {EnvVars}
*/
export function getEnvCI() {
// Get normalized CI env variables
export function getEnvVars() {
// Get env-ci environment variables
const envCIvars = envCI();
debug('env-ci environment variables', envCIvars);

return {
isCi: envCIvars.isCi, // process.env.CI
service: ('service' in envCIvars && envCIvars.service) || process.env.RELATIVE_CI_SERVICE,
slug: resolveSlug(envCIvars),
branch: ('prBranch' in envCIvars && envCIvars.prBranch) || ('branch' in envCIvars && envCIvars.branch) || process.env.RELATIVE_CI_BRANCH,
pr: ('pr' in envCIvars && envCIvars.pr) || process.env.RELATIVE_CI_PR,
build: ('build' in envCIvars && envCIvars.build) || process.env.RELATIVE_CI_BUILD,
buildUrl: ('buildUrl' in envCIvars && envCIvars.buildUrl) || process.env.RELATIVE_CI_BUILD_URL,
commit: envCIvars.commit || process.env.RELATIVE_CI_COMMIT,
// Get custom environment variables
const customEnvVars = {
key: process.env.RELATIVE_CI_KEY,
endpoint: process.env.RELATIVE_CI_ENDPOINT || DEFAULT_ENDPOINT,
service: process.env.RELATIVE_CI_SERVICE,
slug: process.env.RELATIVE_CI_SLUG,
branch: process.env.RELATIVE_CI_BRANCH,
pr: process.env.RELATIVE_CI_PR,
build: process.env.RELATIVE_CI_BUILD,
buildUrl: process.env.RELATIVE_CI_BUILD_URL,
commit: process.env.RELATIVE_CI_COMMIT,
commitMessage: process.env.RELATIVE_CI_COMMIT_MESSAGE,
};
debug('custom environment variables', customEnvVars);

const resolvedEnvVars = {
key: customEnvVars.key,
endpoint: customEnvVars.endpoint,
isCi: envCIvars.isCi, // process.env.CI
service: customEnvVars.service || ('service' in envCIvars && envCIvars.service),
slug: customEnvVars.slug || resolveSlug(envCIvars),
branch: customEnvVars.branch || ('prBranch' in envCIvars && envCIvars.prBranch) || ('branch' in envCIvars && envCIvars.branch),
pr: customEnvVars.pr || ('pr' in envCIvars && envCIvars.pr),
build: customEnvVars.build || ('build' in envCIvars && envCIvars.build),
buildUrl: customEnvVars.buildUrl || ('buildUrl' in envCIvars && envCIvars.buildUrl),
commit: customEnvVars.commit || envCIvars.commit,
commitMessage: customEnvVars.commitMessage,
};
debug('resolved environment variables', envCIvars);

return resolvedEnvVars;
}
4 changes: 2 additions & 2 deletions src/webpack-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { merge } from 'lodash';
import validate from '@bundle-stats/plugin-webpack-validate';

import { agent } from './agent';
import { debug, getEnvCI } from './utils';
import { debug, getEnvVars } from './utils';

const PLUGIN_NAME = 'RelativeCiAgent';

Expand Down Expand Up @@ -43,7 +43,7 @@ export class RelativeCiAgentWebpackPlugin {
}

apply(compiler) {
const { isCi } = getEnvCI();
const { isCi } = getEnvVars();

const options = merge(
{},
Expand Down
10 changes: 9 additions & 1 deletion typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AgentArgs {
commitMessage?: string;
}

export interface EnvCI {
export interface EnvVars {
isCi: boolean;
service?: string;
slug?: string;
Expand All @@ -33,6 +33,14 @@ export interface EnvCI {
buildUrl?: string;
commit?: string;
commitMessage?: string;
/**
* RelativeCI project API key
*/
key: string;
/**
* RelativeCI ingest endpoint
*/
endpoint: string;
}

export class RelativeCiAgentWebpackPlugin {
Expand Down

0 comments on commit bdf8476

Please sign in to comment.