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

refactor: move preCommands out of docker options #12938

Merged
merged 4 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 lib/manager/bundler/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ export async function updateArtifacts(
image: 'ruby',
tagScheme: 'ruby',
tagConstraint: await getRubyConstraint(updateArtifact),
preCommands,
},
preCommands,
};
await exec(cmd, execOptions);

Expand Down
2 changes: 1 addition & 1 deletion lib/manager/gradle/deep/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export async function executeGradle(
tagConstraint:
config.constraints?.java ?? (await getDockerConstraint(gradleRoot)),
tagScheme: getJavaVersioning(),
preCommands: await getDockerPreCommands(gradleRoot),
},
preCommands: await getDockerPreCommands(gradleRoot),
extraEnv,
};
try {
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/mix/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export async function updateArtifacts({
cwdFile: packageFileName,
docker: {
image: 'elixir',
preCommands,
},
preCommands,
};
const command = [
'mix',
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/lerna.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export async function generateLockFiles(
image: 'node',
tagScheme: 'node',
tagConstraint,
preCommands,
},
preCommands,
};
// istanbul ignore if
if (GlobalConfig.get('exposeAllEnv')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export async function generateLockFile(
image: 'node',
tagScheme: 'node',
tagConstraint,
preCommands,
},
preCommands,
};
// istanbul ignore if
if (GlobalConfig.get('exposeAllEnv')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export async function generateLockFile(
image: 'node',
tagScheme: 'node',
tagConstraint,
preCommands,
},
preCommands,
};
// istanbul ignore if
if (GlobalConfig.get('exposeAllEnv')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export async function generateLockFile(
image: 'node',
tagScheme: 'node',
tagConstraint,
preCommands,
},
preCommands,
};
// istanbul ignore if
if (GlobalConfig.get('exposeAllEnv')) {
Expand Down
6 changes: 3 additions & 3 deletions lib/manager/pip-compile/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export async function updateArtifacts({
image: 'python',
tagConstraint,
tagScheme: 'pep440',
preCommands: [
`pip install --user ${quote(`pip-tools${pipToolsConstraint}`)}`,
],
},
preCommands: [
`pip install --user ${quote(`pip-tools${pipToolsConstraint}`)}`,
],
};
logger.debug({ cmd }, 'pip-compile command');
await exec(cmd, execOptions);
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/pip_requirements/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export async function updateArtifacts({
docker: {
image: 'python',
tagScheme: 'pip_requirements',
preCommands: ['pip install hashin'],
},
preCommands: ['pip install hashin'],
};
await exec(cmd, execOptions);
const newContent = await readLocalFile(packageFileName, 'utf8');
Expand Down
4 changes: 1 addition & 3 deletions lib/manager/pipenv/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,8 @@ export async function updateArtifacts({
image: 'python',
tagConstraint,
tagScheme: 'pep440',
preCommands: [
`pip install --user ${quote(`pipenv${pipenvConstraint}`)}`,
],
},
preCommands: [`pip install --user ${quote(`pipenv${pipenvConstraint}`)}`],
};
logger.debug({ cmd }, 'pipenv lock command');
await exec(cmd, execOptions);
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/poetry/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ export async function updateArtifacts({
image: 'python',
tagConstraint,
tagScheme: 'poetry',
preCommands: [poetryInstall],
},
preCommands: [poetryInstall],
};
await exec(cmd, execOptions);
const newPoetryLockContent = await readLocalFile(lockFileName, 'utf8');
Expand Down
2 changes: 0 additions & 2 deletions lib/util/exec/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export interface DockerOptions {
volumes?: Opt<VolumeOption[]>;
envVars?: Opt<Opt<string>[]>;
cwd?: Opt<string>;
preCommands?: DockerExtraCommands;
postCommands?: DockerExtraCommands;
}

export interface RawExecOptions extends ChildProcessExecOptions {
Expand Down
50 changes: 34 additions & 16 deletions lib/util/exec/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ describe('util/exec/docker/index', () => {
const envVars = ['FOO', 'BAR'];
const image = 'sample_image';
const dockerOptions = {
preCommands,
postCommands,
image,
cwd: '/tmp/foobar',
envVars,
Expand All @@ -236,7 +234,12 @@ describe('util/exec/docker/index', () => {

it('returns executable command', async () => {
mockExecAll(exec);
const res = await generateDockerCommand(commands, dockerOptions);
const res = await generateDockerCommand(
commands,
preCommands,
postCommands,
dockerOptions
);
expect(res).toBe(command(image));
});

Expand All @@ -247,10 +250,15 @@ describe('util/exec/docker/index', () => {
['/tmp/bar', `/tmp/bar`],
['/tmp/baz', `/home/baz`],
];
const res = await generateDockerCommand(commands, {
...dockerOptions,
volumes: [...volumes, ...volumes],
});
const res = await generateDockerCommand(
commands,
preCommands,
postCommands,
{
...dockerOptions,
volumes: [...volumes, ...volumes],
}
);
expect(res).toBe(
command(
image,
Expand All @@ -261,10 +269,15 @@ describe('util/exec/docker/index', () => {

it('handles tag parameter', async () => {
mockExecAll(exec);
const res = await generateDockerCommand(commands, {
...dockerOptions,
tag: '1.2.3',
});
const res = await generateDockerCommand(
commands,
preCommands,
postCommands,
{
...dockerOptions,
tag: '1.2.3',
}
);
expect(res).toBe(command(`${image}:1.2.3`));
});

Expand All @@ -277,11 +290,16 @@ describe('util/exec/docker/index', () => {
{ version: '2.0.0' },
],
} as never);
const res = await generateDockerCommand(commands, {
...dockerOptions,
tagScheme: 'npm',
tagConstraint: '^1.2.3',
});
const res = await generateDockerCommand(
commands,
preCommands,
postCommands,
{
...dockerOptions,
tagScheme: 'npm',
tagConstraint: '^1.2.3',
}
);
expect(res).toBe(command(`${image}:1.2.4`));
});
});
Expand Down
4 changes: 2 additions & 2 deletions lib/util/exec/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ export async function removeDanglingContainers(): Promise<void> {

export async function generateDockerCommand(
commands: string[],
preCommands: string[] = [],
postCommands: string[] = [],
options: DockerOptions
): Promise<string> {
const { envVars, cwd, tagScheme, tagConstraint } = options;
let image = options.image;
const volumes = options.volumes || [];
const preCommands = options.preCommands || [];
const postCommands = options.postCommands || [];
const {
localDir,
cacheDir,
Expand Down
6 changes: 2 additions & 4 deletions lib/util/exec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ describe('util/exec/index', () => {
inOpts: {
docker: {
image,
preCommands: ['preCommand1', 'preCommand2', null],
postCommands: ['postCommand1', undefined, 'postCommand2'],
},
preCommands: ['preCommand1', 'preCommand2', null],
postCommands: ['postCommand1', undefined, 'postCommand2'],
},
outCmd: [
dockerPullCmd,
Expand Down Expand Up @@ -496,8 +496,6 @@ describe('util/exec/index', () => {
inOpts: {
docker: {
image,
preCommands: null,
postCommands: undefined,
},
},
outCmd: [
Expand Down
11 changes: 9 additions & 2 deletions lib/util/exec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TEMPORARY_ERROR } from '../../constants/error-messages';
import { logger } from '../../logger';
import { generateInstallCommands } from './buildpack';
import {
DockerExtraCommands,
DockerOptions,
ExecResult,
Opt,
Expand All @@ -22,6 +23,8 @@ export interface ExecOptions extends ChildProcessExecOptions {
extraEnv?: Opt<ExtraEnv>;
docker?: Opt<DockerOptions>;
toolConstraints?: Opt<ToolConstraint[]>;
preCommands?: DockerExtraCommands;
rarkins marked this conversation as resolved.
Show resolved Hide resolved
postCommands?: DockerExtraCommands;
}

function getChildEnv({
Expand Down Expand Up @@ -74,6 +77,8 @@ function getRawExecOptions(opts: ExecOptions): RawExecOptions {
delete execOptions.docker;
delete execOptions.cwdFile;
delete execOptions.toolConstraints;
delete execOptions.preCommands;
delete execOptions.postCommands;

const childEnv = getChildEnv(opts);
const cwd = getCwd(opts);
Expand Down Expand Up @@ -125,12 +130,14 @@ async function prepareRawExec(
const envVars = dockerEnvVars(extraEnv, childEnv);
const cwd = getCwd(opts);
const dockerOptions: DockerOptions = { ...docker, cwd, envVars };
dockerOptions.preCommands = [
const preCommands = [
...(await generateInstallCommands(opts.toolConstraints)),
...(dockerOptions.preCommands || []),
...(opts.preCommands || []),
];
const dockerCommand = await generateDockerCommand(
rawCommands,
preCommands,
opts.postCommands,
dockerOptions
);
rawCommands = [dockerCommand];
Expand Down