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 #103: Dealing with un-escaped environment variables by using args #105

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

41 changes: 22 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const START_SCRIPT_EXECUTION_MARKER: string = `Starting script execution via doc
const BASH_ARG: string = `bash --noprofile --norc -e `;
const AZ_CLI_VERSION_DEFAULT_VALUE = 'agentazcliversion'

export async function main(){
export async function main() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any linter tool for TypeScript? A linter can help us normalize the code style and avoid distracting diffs like this.

Just a reminder. No need to do that right now.

var scriptFileName: string = '';
const CONTAINER_NAME = `MICROSOFT_AZURE_CLI_${getCurrentTime()}_CONTAINER`;
try {
Expand Down Expand Up @@ -49,14 +49,6 @@ export async function main(){
}
inlineScript = ` set -e >&2; echo '${START_SCRIPT_EXECUTION_MARKER}' >&2; ${inlineScript}`;
scriptFileName = await createScriptFile(inlineScript);
let startCommand: string = ` ${BASH_ARG}${TEMP_DIRECTORY}/${scriptFileName} `;
let environmentVariables = '';
for (let key in process.env) {
// if (key.toUpperCase().startsWith("GITHUB_") && key.toUpperCase() !== 'GITHUB_WORKSPACE' && process.env[key]){
if (!checkIfEnvironmentVariableIsOmitted(key) && process.env[key]) {
environmentVariables += ` -e "${key}=${process.env[key]}" `;
}
}

/*
For the docker run command, we are doing the following
Expand All @@ -65,15 +57,26 @@ export async function main(){
- voulme mount .azure session token file between host and container,
- volume mount temp directory between host and container, inline script file is created in temp directory
*/
let command: string = `run --workdir ${process.env.GITHUB_WORKSPACE} -v ${process.env.GITHUB_WORKSPACE}:${process.env.GITHUB_WORKSPACE} `;
command += ` -v ${process.env.HOME}/.azure:/root/.azure -v ${TEMP_DIRECTORY}:${TEMP_DIRECTORY} `;
command += ` ${environmentVariables} `;
command += `--name ${CONTAINER_NAME} `;
command += ` mcr.microsoft.com/azure-cli:${azcliversion} ${startCommand}`;
let command: string = `run`;
var args = ["--workdir", `${process.env.GITHUB_WORKSPACE}`,
"-v", `${process.env.GITHUB_WORKSPACE}:${process.env.GITHUB_WORKSPACE}`,
"-v", `${process.env.HOME}/.azure:/root/.azure`,
"-v", `${TEMP_DIRECTORY}:${TEMP_DIRECTORY}`
];
for (let key in process.env) {
if (!checkIfEnvironmentVariableIsOmitted(key) && process.env[key]) {
args = args.concat("-e", `${key}=${process.env[key]}`);
MoChilia marked this conversation as resolved.
Show resolved Hide resolved
}
}
args = args.concat("--name", `${CONTAINER_NAME}`,
`mcr.microsoft.com/azure-cli:${azcliversion}`,
`bash`, `--noprofile`, `--norc`, `-e`, `${TEMP_DIRECTORY}/${scriptFileName}`);
MoChilia marked this conversation as resolved.
Show resolved Hide resolved

console.log(`${START_SCRIPT_EXECUTION_MARKER}${azcliversion}`);
await executeDockerCommand(command);
await executeDockerCommand(command, args);
console.log("az script ran successfully.");
} catch (error) {
}
catch (error) {
core.error(error);
throw error;
}
Expand All @@ -82,7 +85,7 @@ export async function main(){
const scriptFilePath: string = path.join(TEMP_DIRECTORY, scriptFileName);
await deleteFile(scriptFilePath);
console.log("cleaning up container...");
await executeDockerCommand(` container rm --force ${CONTAINER_NAME} `, true);
await executeDockerCommand(`container rm`, ["--force", `${CONTAINER_NAME}`], true);
}
};

Expand Down Expand Up @@ -115,7 +118,7 @@ const getAllAzCliVersions = async (): Promise<Array<string>> => {
return [];
}

const executeDockerCommand = async (dockerCommand: string, continueOnError: boolean = false): Promise<void> => {
const executeDockerCommand = async (dockerCommand: string, args: any = [], continueOnError: boolean = false): Promise<void> => {
MoChilia marked this conversation as resolved.
Show resolved Hide resolved

const dockerTool: string = await io.which("docker", true);
var errorStream: string = '';
Expand All @@ -140,7 +143,7 @@ const executeDockerCommand = async (dockerCommand: string, continueOnError: bool
};
var exitCode;
try {
exitCode = await exec.exec(`"${dockerTool}" ${dockerCommand}`, [], execOptions);
exitCode = await exec.exec(`"${dockerTool}" ${dockerCommand}`, args, execOptions);
MoChilia marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
if (!continueOnError) {
throw error;
Expand Down