Skip to content

Commit

Permalink
Merge pull request #1068 from relative-ci/fix-empty-env-vars
Browse files Browse the repository at this point in the history
fix empty env vars
  • Loading branch information
vio authored Feb 1, 2024
2 parents f89f16f + 7487680 commit 71bdd8d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const agent = (artifactsData, config, args = {}, logger = console) => {

const envVars = getEnvVars();

// Normalized env vars - merge provided args with env vars
// Normalized params - merge provided args with env vars
// @type {EnvVars}
const normalizedEnvVars = {
const normalizedParams = {
slug: args.slug || envVars.slug,
branch: args.branch || envVars.branch,
pr: args.pr || envVars.pr,
Expand All @@ -54,23 +54,23 @@ export const agent = (artifactsData, config, args = {}, logger = console) => {
endpoint: envVars.endpoint,
};

debug('normalized env vars ', maskObjectProperties(normalizedEnvVars, ['key']));
debug('normalized parameters - agent configuration with environmental variables fallback', maskObjectProperties(normalizedParams, ['key']));

const { includeCommitMessage } = config;

const params = {
agentVersion: packageInfo.version,

...normalizedEnvVars,
...normalizedParams,

// Get commit message using git if includeCommitMessage is set and
// there is no --commit-message argument or RELATIVE_CI_COMMIT_MESSAGE
...includeCommitMessage && !normalizedEnvVars.commitMessage && {
...includeCommitMessage && !normalizedParams.commitMessage && {
commitMessage: getCommitMessage(),
},
};

debug('Job parameters', maskObjectProperties(params, ['key']));
debug('job parameters', maskObjectProperties(params, ['key']));

// Validate parameters
if (!params.key) {
Expand Down
27 changes: 20 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ function resolveSlug(envVars) {
return 'slug' in envVars ? envVars.slug : '';
}

/**
* @param {import('env-ci').CiEnv} data
* @param {string} key
* @returns {string | undefined}
*/
function getEnvCIVar(data, key) {
if (!data[key]) {
return undefined;
}

return data[key];
}

/**
* Extract CI environment variables using env-ci and custom fallback env vars
* @returns {EnvVars}
Expand All @@ -125,22 +138,22 @@ export function getEnvVars() {
commit: process.env.RELATIVE_CI_COMMIT,
commitMessage: process.env.RELATIVE_CI_COMMIT_MESSAGE,
};
debug('custom environment variables', maskObjectProperties(customEnvVars, ['key']));
debug('RELATIVE_CI environment variables', maskObjectProperties(customEnvVars, ['key']));

const resolvedEnvVars = {
key: customEnvVars.key,
endpoint: customEnvVars.endpoint,
isCi: envCIvars.isCi, // process.env.CI
service: customEnvVars.service || ('service' in envCIvars && envCIvars.service),
service: customEnvVars.service || getEnvCIVar(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),
branch: customEnvVars.branch || getEnvCIVar(envCIvars, 'prBranch') || getEnvCIVar(envCIvars, 'branch'),
pr: customEnvVars.pr || getEnvCIVar(envCIvars, 'pr'),
build: customEnvVars.build || getEnvCIVar(envCIvars, 'build'),
buildUrl: customEnvVars.buildUrl || getEnvCIVar(envCIvars, 'buildUrl'),
commit: customEnvVars.commit || envCIvars.commit,
commitMessage: customEnvVars.commitMessage,
};
debug('resolved environment variables', maskObjectProperties(envCIvars, ['key']));
debug('resolved environment variables', maskObjectProperties(resolvedEnvVars, ['key']));

return resolvedEnvVars;
}

0 comments on commit 71bdd8d

Please sign in to comment.