Skip to content

Commit

Permalink
Allow access to user defined variables [Cherry pick from master and b…
Browse files Browse the repository at this point in the history
…uild the code for releases] (#17)

* Allowing user-defined env variable to be accessible (#16)

* Allowing user-defined env variable to be accessible

* Resolving PR comments

* Cherry pick and compile the code
  • Loading branch information
jalajmsft authored Feb 4, 2020
1 parent 2c2c8b0 commit c34f2e3
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ const run = () => __awaiter(void 0, void 0, void 0, function* () {
inlineScript = ` set -e >&2; echo '${START_SCRIPT_EXECUTION_MARKER}' >&2; ${inlineScript}`;
scriptFileName = yield utils_1.createScriptFile(inlineScript);
let startCommand = ` ${BASH_ARG}${CONTAINER_TEMP_DIRECTORY}/${scriptFileName} `;
let gitHubEnvironmentVariables = '';
let environmentVariables = '';
for (let key in process.env) {
if (key.toUpperCase().startsWith("GITHUB_") && key.toUpperCase() !== 'GITHUB_WORKSPACE' && process.env[key]) {
gitHubEnvironmentVariables += ` -e "${key}=${process.env[key]}" `;
if (!utils_1.checkIfEnvironmentVariableIsOmitted(key) && process.env[key]) {
environmentVariables += ` -e "${key}=${process.env[key]}" `;
}
}
/*
Expand All @@ -62,7 +62,7 @@ const run = () => __awaiter(void 0, void 0, void 0, function* () {
*/
let command = `run --workdir ${CONTAINER_WORKSPACE} -v ${process.env.GITHUB_WORKSPACE}:${CONTAINER_WORKSPACE} `;
command += ` -v ${process.env.HOME}/.azure:/root/.azure -v ${utils_1.TEMP_DIRECTORY}:${CONTAINER_TEMP_DIRECTORY} `;
command += ` ${gitHubEnvironmentVariables} `;
command += ` ${environmentVariables} `;
command += `-e GITHUB_WORKSPACE=${CONTAINER_WORKSPACE} `;
command += `--name ${CONTAINER_NAME} `;
command += ` mcr.microsoft.com/azure-cli:${azcliversion} ${startCommand}`;
Expand Down
45 changes: 45 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,48 @@ class NullOutstreamStringWritable extends stream.Writable {
}
exports.NullOutstreamStringWritable = NullOutstreamStringWritable;
;
exports.checkIfEnvironmentVariableIsOmitted = (key) => {
const omitEnvironmentVariables = [
'LANG',
'HOSTNAME',
'PWD',
'HOME',
'PYTHON_VERSION',
'PYTHON_PIP_VERSION',
'SHLVL',
'PATH',
'GPG_KEY',
'CONDA',
'AGENT_TOOLSDIRECTORY',
'GITHUB_WORKSPACE',
'RUNNER_PERFLOG',
'RUNNER_WORKSPACE',
'RUNNER_TEMP',
'RUNNER_TRACKING_ID',
'RUNNER_TOOL_CACHE',
'DOTNET_SKIP_FIRST_TIME_EXPERIENCE',
'JOURNAL_STREAM',
'DEPLOYMENT_BASEPATH',
'VCPKG_INSTALLATION_ROOT',
'PERFLOG_LOCATION_SETTING'
];
const omitEnvironmentVariablesWithPrefix = [
'JAVA_',
'LEIN_',
'M2_',
'BOOST_',
'GOROOT',
'ANDROID_',
'GRADLE_',
'ANT_',
'CHROME',
'SELENIUM_',
'INPUT_'
];
for (let i = 0; i < omitEnvironmentVariables.length; i++) {
if (omitEnvironmentVariables[i] === key.toUpperCase()) {
return true;
}
}
return omitEnvironmentVariablesWithPrefix.some((prefix) => key.toUpperCase().startsWith(prefix));
};
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as io from '@actions/io';
import * as os from 'os';
import * as path from 'path';

import { createScriptFile, TEMP_DIRECTORY, NullOutstreamStringWritable, deleteFile, getCurrentTime } from './utils';
import { createScriptFile, TEMP_DIRECTORY, NullOutstreamStringWritable, deleteFile, getCurrentTime, checkIfEnvironmentVariableIsOmitted } from './utils';

const START_SCRIPT_EXECUTION_MARKER: string = `Starting script execution via docker image mcr.microsoft.com/azure-cli:`;
const BASH_ARG: string = `bash --noprofile --norc -e `;
Expand Down Expand Up @@ -35,10 +35,10 @@ const run = async () => {
inlineScript = ` set -e >&2; echo '${START_SCRIPT_EXECUTION_MARKER}' >&2; ${inlineScript}`;
scriptFileName = await createScriptFile(inlineScript);
let startCommand: string = ` ${BASH_ARG}${CONTAINER_TEMP_DIRECTORY}/${scriptFileName} `;
let gitHubEnvironmentVariables = '';
let environmentVariables = '';
for (let key in process.env){
if (key.toUpperCase().startsWith("GITHUB_") && key.toUpperCase() !== 'GITHUB_WORKSPACE' && process.env[key]){
gitHubEnvironmentVariables += ` -e "${key}=${process.env[key]}" `;
if(!checkIfEnvironmentVariableIsOmitted(key) && process.env[key]){
environmentVariables += ` -e "${key}=${process.env[key]}" `;
}
}
/*
Expand All @@ -50,7 +50,7 @@ const run = async () => {
*/
let command: string = `run --workdir ${CONTAINER_WORKSPACE} -v ${process.env.GITHUB_WORKSPACE}:${CONTAINER_WORKSPACE} `;
command += ` -v ${process.env.HOME}/.azure:/root/.azure -v ${TEMP_DIRECTORY}:${CONTAINER_TEMP_DIRECTORY} `;
command += ` ${gitHubEnvironmentVariables} `;
command += ` ${environmentVariables} `;
command += `-e GITHUB_WORKSPACE=${CONTAINER_WORKSPACE} `;
command += `--name ${CONTAINER_NAME} `;
command += ` mcr.microsoft.com/azure-cli:${azcliversion} ${startCommand}`;
Expand Down
51 changes: 50 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,53 @@ export class NullOutstreamStringWritable extends stream.Writable {
callback();
}
}
};
};

export const checkIfEnvironmentVariableIsOmitted = (key: string): boolean => {

const omitEnvironmentVariables: string [] = [
'LANG',
'HOSTNAME',
'PWD',
'HOME',
'PYTHON_VERSION',
'PYTHON_PIP_VERSION',
'SHLVL',
'PATH',
'GPG_KEY',
'CONDA',
'AGENT_TOOLSDIRECTORY',
'GITHUB_WORKSPACE',
'RUNNER_PERFLOG',
'RUNNER_WORKSPACE',
'RUNNER_TEMP',
'RUNNER_TRACKING_ID',
'RUNNER_TOOL_CACHE',
'DOTNET_SKIP_FIRST_TIME_EXPERIENCE',
'JOURNAL_STREAM',
'DEPLOYMENT_BASEPATH',
'VCPKG_INSTALLATION_ROOT',
'PERFLOG_LOCATION_SETTING'
];

const omitEnvironmentVariablesWithPrefix: string [] = [
'JAVA_',
'LEIN_',
'M2_',
'BOOST_',
'GOROOT',
'ANDROID_',
'GRADLE_',
'ANT_',
'CHROME',
'SELENIUM_',
'INPUT_'
];
for (let i = 0; i < omitEnvironmentVariables.length; i++){
if (omitEnvironmentVariables[i] === key.toUpperCase()){
return true;
}
}

return omitEnvironmentVariablesWithPrefix.some((prefix: string) => key.toUpperCase().startsWith(prefix));
}

0 comments on commit c34f2e3

Please sign in to comment.