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

fix empty env vars #1068

Merged
merged 2 commits into from
Feb 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}