automatically import env in autobuild

This commit is contained in:
Robert Brignull 2020-08-28 17:22:26 +01:00
parent c3d6602e8a
commit aa7e2fe91b
3 changed files with 38 additions and 28 deletions

30
lib/runner.js generated
View file

@ -54,11 +54,13 @@ function getToolsDir(userInput) {
} }
return toolsDir; return toolsDir;
} }
function checkEnvironmentSetup(config) { const codeqlEnvJsonFilename = 'codeql-env.json';
if (config.languages.some(languages_1.isTracedLanguage) && !('ODASA_TRACER_CONFIGURATION' in process.env)) { // Imports the environment from codeqlEnvJsonFilename if not already present
throw new Error("Could not detect 'ODASA_TRACER_CONFIGURATION' in environment. " + function importTracerEnvironment(config) {
"Make sure that environment variables were correctly exported to future processes. " + if (!('ODASA_TRACER_CONFIGURATION' in process.env)) {
"See end of output from 'init' command for instructions."); const jsonEnvFile = path.join(config.tempDir, codeqlEnvJsonFilename);
const env = JSON.parse(fs.readFileSync(jsonEnvFile).toString('utf-8'));
Object.keys(env).forEach(key => process.env[key] = env[key]);
} }
} }
program program
@ -96,6 +98,9 @@ program
if (tracerConfig === undefined) { if (tracerConfig === undefined) {
return; return;
} }
// Always output a json file of the env that can be consumed programatically
const jsonEnvFile = path.join(config.tempDir, codeqlEnvJsonFilename);
fs.writeFileSync(jsonEnvFile, JSON.stringify(tracerConfig.env));
if (process.platform === 'win32') { if (process.platform === 'win32') {
const batEnvFile = path.join(config.tempDir, 'codeql-env.bat'); const batEnvFile = path.join(config.tempDir, 'codeql-env.bat');
const batEnvFileContents = Object.entries(tracerConfig.env) const batEnvFileContents = Object.entries(tracerConfig.env)
@ -107,22 +112,22 @@ program
.map(([key, value]) => `$env:${key}="${value}"`) .map(([key, value]) => `$env:${key}="${value}"`)
.join('\n'); .join('\n');
fs.writeFileSync(powershellEnvFile, powershellEnvFileContents); fs.writeFileSync(powershellEnvFile, powershellEnvFileContents);
logger.info(`\nCodeQL environment output to "${batEnvFileContents}" and "${powershellEnvFile}". ` + logger.info(`\nCodeQL environment output to "${jsonEnvFile}", "${batEnvFileContents}" and "${powershellEnvFile}". ` +
`Please export these variables to future processes so the build can be traced. ` + `Please export these variables to future processes so the build can be traced. ` +
`If using cmd/batch run "call ${batEnvFileContents}" ` + `If using cmd/batch run "call ${batEnvFileContents}" ` +
`or if using PowerShell run "cat ${powershellEnvFile} | Invoke-Expression".`); `or if using PowerShell run "cat ${powershellEnvFile} | Invoke-Expression".`);
} }
else { else {
// Assume that anything that's not windows is using a unix-style shell // Assume that anything that's not windows is using a unix-style shell
const envFile = path.join(config.tempDir, 'codeql-env.sh'); const shEnvFile = path.join(config.tempDir, 'codeql-env.sh');
const envFileContents = Object.entries(tracerConfig.env) const shEnvFileContents = Object.entries(tracerConfig.env)
// Some vars contain ${LIB} that we do not want to be expanded when executing this script // Some vars contain ${LIB} that we do not want to be expanded when executing this script
.map(([key, value]) => `export ${key}="${value.replace('$', '\\$')}"`) .map(([key, value]) => `export ${key}="${value.replace('$', '\\$')}"`)
.join('\n'); .join('\n');
fs.writeFileSync(envFile, envFileContents); fs.writeFileSync(shEnvFile, shEnvFileContents);
logger.info(`\nCodeQL environment output to "${envFile}". ` + logger.info(`\nCodeQL environment output to "${jsonEnvFile}" and "${shEnvFile}". ` +
`Please export these variables to future processes so the build can be traced, ` + `Please export these variables to future processes so the build can be traced, ` +
`for example by running "source ${envFile}".`); `for example by running ". ${shEnvFile}".`);
} }
} }
catch (e) { catch (e) {
@ -145,7 +150,7 @@ program
throw new Error("Config file could not be found at expected location. " + throw new Error("Config file could not be found at expected location. " +
"Was the 'init' command run with the same '--temp-dir' argument as this command."); "Was the 'init' command run with the same '--temp-dir' argument as this command.");
} }
checkEnvironmentSetup(config); importTracerEnvironment(config);
let language = undefined; let language = undefined;
if (cmd.language !== undefined) { if (cmd.language !== undefined) {
language = languages_1.parseLanguage(cmd.language); language = languages_1.parseLanguage(cmd.language);
@ -190,7 +195,6 @@ program
throw new Error("Config file could not be found at expected location. " + throw new Error("Config file could not be found at expected location. " +
"Was the 'init' command run with the same '--temp-dir' argument as this command."); "Was the 'init' command run with the same '--temp-dir' argument as this command.");
} }
checkEnvironmentSetup(config);
await analyze_1.runAnalyze(repository_1.parseRepositoryNwo(cmd.repository), cmd.commit, cmd.ref, undefined, undefined, undefined, cmd.checkoutPath || process.cwd(), undefined, cmd.githubAuth, parseGithubUrl(cmd.githubUrl), cmd.upload, 'runner', outputDir, config, logger); await analyze_1.runAnalyze(repository_1.parseRepositoryNwo(cmd.repository), cmd.commit, cmd.ref, undefined, undefined, undefined, cmd.checkoutPath || process.cwd(), undefined, cmd.githubAuth, parseGithubUrl(cmd.githubUrl), cmd.upload, 'runner', outputDir, config, logger);
} }
catch (e) { catch (e) {

File diff suppressed because one or more lines are too long

View file

@ -8,7 +8,7 @@ import { determineAutobuildLanguage, runAutobuild } from './autobuild';
import { CodeQL, getCodeQL } from './codeql'; import { CodeQL, getCodeQL } from './codeql';
import { Config, getConfig } from './config-utils'; import { Config, getConfig } from './config-utils';
import { initCodeQL, initConfig, runInit } from './init'; import { initCodeQL, initConfig, runInit } from './init';
import { isTracedLanguage, Language, parseLanguage } from './languages'; import { Language, parseLanguage } from './languages';
import { getRunnerLogger } from './logging'; import { getRunnerLogger } from './logging';
import { parseRepositoryNwo } from './repository'; import { parseRepositoryNwo } from './repository';
import * as upload_lib from './upload-lib'; import * as upload_lib from './upload-lib';
@ -54,11 +54,14 @@ function getToolsDir(userInput: string | undefined): string {
return toolsDir; return toolsDir;
} }
function checkEnvironmentSetup(config: Config) { const codeqlEnvJsonFilename = 'codeql-env.json';
if (config.languages.some(isTracedLanguage) && !('ODASA_TRACER_CONFIGURATION' in process.env)) {
throw new Error("Could not detect 'ODASA_TRACER_CONFIGURATION' in environment. " + // Imports the environment from codeqlEnvJsonFilename if not already present
"Make sure that environment variables were correctly exported to future processes. " + function importTracerEnvironment(config: Config) {
"See end of output from 'init' command for instructions."); if (!('ODASA_TRACER_CONFIGURATION' in process.env)) {
const jsonEnvFile = path.join(config.tempDir, codeqlEnvJsonFilename);
const env = JSON.parse(fs.readFileSync(jsonEnvFile).toString('utf-8'));
Object.keys(env).forEach(key => process.env[key] = env[key]);
} }
} }
@ -133,6 +136,10 @@ program
return; return;
} }
// Always output a json file of the env that can be consumed programatically
const jsonEnvFile = path.join(config.tempDir, codeqlEnvJsonFilename);
fs.writeFileSync(jsonEnvFile, JSON.stringify(tracerConfig.env));
if (process.platform === 'win32') { if (process.platform === 'win32') {
const batEnvFile = path.join(config.tempDir, 'codeql-env.bat'); const batEnvFile = path.join(config.tempDir, 'codeql-env.bat');
const batEnvFileContents = Object.entries(tracerConfig.env) const batEnvFileContents = Object.entries(tracerConfig.env)
@ -146,23 +153,23 @@ program
.join('\n'); .join('\n');
fs.writeFileSync(powershellEnvFile, powershellEnvFileContents); fs.writeFileSync(powershellEnvFile, powershellEnvFileContents);
logger.info(`\nCodeQL environment output to "${batEnvFileContents}" and "${powershellEnvFile}". ` + logger.info(`\nCodeQL environment output to "${jsonEnvFile}", "${batEnvFileContents}" and "${powershellEnvFile}". ` +
`Please export these variables to future processes so the build can be traced. ` + `Please export these variables to future processes so the build can be traced. ` +
`If using cmd/batch run "call ${batEnvFileContents}" ` + `If using cmd/batch run "call ${batEnvFileContents}" ` +
`or if using PowerShell run "cat ${powershellEnvFile} | Invoke-Expression".`); `or if using PowerShell run "cat ${powershellEnvFile} | Invoke-Expression".`);
} else { } else {
// Assume that anything that's not windows is using a unix-style shell // Assume that anything that's not windows is using a unix-style shell
const envFile = path.join(config.tempDir, 'codeql-env.sh'); const shEnvFile = path.join(config.tempDir, 'codeql-env.sh');
const envFileContents = Object.entries(tracerConfig.env) const shEnvFileContents = Object.entries(tracerConfig.env)
// Some vars contain ${LIB} that we do not want to be expanded when executing this script // Some vars contain ${LIB} that we do not want to be expanded when executing this script
.map(([key, value]) => `export ${key}="${value.replace('$', '\\$')}"`) .map(([key, value]) => `export ${key}="${value.replace('$', '\\$')}"`)
.join('\n'); .join('\n');
fs.writeFileSync(envFile, envFileContents); fs.writeFileSync(shEnvFile, shEnvFileContents);
logger.info(`\nCodeQL environment output to "${envFile}". ` + logger.info(`\nCodeQL environment output to "${jsonEnvFile}" and "${shEnvFile}". ` +
`Please export these variables to future processes so the build can be traced, ` + `Please export these variables to future processes so the build can be traced, ` +
`for example by running "source ${envFile}".`); `for example by running ". ${shEnvFile}".`);
} }
} catch (e) { } catch (e) {
@ -192,7 +199,7 @@ program
throw new Error("Config file could not be found at expected location. " + throw new Error("Config file could not be found at expected location. " +
"Was the 'init' command run with the same '--temp-dir' argument as this command."); "Was the 'init' command run with the same '--temp-dir' argument as this command.");
} }
checkEnvironmentSetup(config); importTracerEnvironment(config);
let language: Language | undefined = undefined; let language: Language | undefined = undefined;
if (cmd.language !== undefined) { if (cmd.language !== undefined) {
language = parseLanguage(cmd.language); language = parseLanguage(cmd.language);
@ -249,7 +256,6 @@ program
throw new Error("Config file could not be found at expected location. " + throw new Error("Config file could not be found at expected location. " +
"Was the 'init' command run with the same '--temp-dir' argument as this command."); "Was the 'init' command run with the same '--temp-dir' argument as this command.");
} }
checkEnvironmentSetup(config);
await runAnalyze( await runAnalyze(
parseRepositoryNwo(cmd.repository), parseRepositoryNwo(cmd.repository),
cmd.commit, cmd.commit,