backend-common: Use Record<string, string> for envVars in runDockerContainer

Signed-off-by: Himanshu Mishra <himanshu@orkohunter.net>
This commit is contained in:
Himanshu Mishra
2021-03-08 14:00:17 +01:00
parent c0c26244db
commit 3f2a0efe01
5 changed files with 16 additions and 9 deletions
@@ -62,7 +62,8 @@ describe('runDockerContainer', () => {
[path.join(rootDir, 'output')]: '/output',
};
const workingDir = path.join(rootDir, 'input');
const envVars = ['HOME=/tmp', 'LOG_LEVEL=debug'];
const envVars = { HOME: '/tmp', LOG_LEVEL: 'debug' };
const envVarsArray = ['HOME=/tmp', 'LOG_LEVEL=debug'];
it('should pull the docker container', async () => {
await runDockerContainer({
@@ -95,7 +96,7 @@ describe('runDockerContainer', () => {
args,
expect.any(Stream),
expect.objectContaining({
Env: envVars,
Env: envVarsArray,
WorkingDir: workingDir,
HostConfig: {
Binds: expect.arrayContaining([
+10 -4
View File
@@ -29,7 +29,7 @@ export type RunDockerContainerOptions = {
dockerClient: Docker;
mountDirs?: Record<string, string>;
workingDir?: string;
envVars?: string[];
envVars?: Record<string, string>;
createOptions?: Docker.ContainerCreateOptions;
};
@@ -43,7 +43,7 @@ export type RunDockerContainerOptions = {
* @param options.mountDirs A map of host directories to mount on the container.
* Object Key: Path on host machine, Value: Path on Docker container
* @param options.workingDir Working dir in the container
* @param options.envVars Environment variables to set in the container. e.g. ['HOME=/tmp']
* @param options.envVars Environment variables to set in the container. e.g. {'HOME': '/tmp'}
*/
export const runDockerContainer = async ({
imageName,
@@ -52,7 +52,7 @@ export const runDockerContainer = async ({
dockerClient,
mountDirs = {},
workingDir,
envVars = [],
envVars = {},
createOptions = {},
}: RunDockerContainerOptions) => {
// Show a better error message when Docker is unavailable.
@@ -99,6 +99,12 @@ export const runDockerContainer = async ({
Binds.push(`${realHostDir}:${containerDir}`);
}
// Create docker environment variables array
const Env = [];
for (const [key, value] of Object.entries(envVars)) {
Env.push(`${key}=${value}`);
}
const [{ Error: error, StatusCode: statusCode }] = await dockerClient.run(
imageName,
args,
@@ -109,7 +115,7 @@ export const runDockerContainer = async ({
Binds,
},
...(workingDir ? { WorkingDir: workingDir } : {}),
Env: envVars,
Env,
...userOptions,
...createOptions,
},
@@ -108,7 +108,7 @@ export class TechdocsGenerator implements GeneratorBase {
workingDir: '/input',
// Set the home directory inside the container as something that applications can
// write to, otherwise they will just fail trying to write to /
envVars: ['HOME=/tmp'],
envVars: { HOME: '/tmp' },
dockerClient,
});
this.logger.info(
@@ -86,7 +86,7 @@ export class CookieCutter implements TemplaterBase {
workingDir: '/input',
// Set the home directory inside the container as something that applications can
// write to, otherwise they will just fail trying to write to /
envVars: ['HOME=/tmp'],
envVars: { HOME: '/tmp' },
logStream,
dockerClient,
});
@@ -57,7 +57,7 @@ export class CreateReactAppTemplater implements TemplaterBase {
dockerClient: dockerClient,
// Set the home directory inside the container as something that applications can
// write to, otherwise they will just fail trying to write to /
envVars: ['HOME=/tmp'],
envVars: { HOME: '/tmp' },
createOptions: {
Entrypoint: ['npx'],
WorkingDir: '/result',