feat(gradle): Use centralized docker execution (#5231)

This commit is contained in:
Sergio Zharinov 2020-02-09 16:39:10 +04:00 committed by GitHub
parent 936434497c
commit 556a073232
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 27 deletions

View file

@ -532,7 +532,7 @@ Array [
exports[`manager/gradle extractPackageFile should use docker even if gradlew is available 1`] = `
Array [
Object {
"cmd": "docker run --rm -v \\"localDir\\":\\"localDir\\" -w \\"localDir\\" renovate/gradle gradle --init-script renovate-plugin.gradle renovate",
"cmd": "docker run --rm -v \\"localDir\\":\\"localDir\\" -w \\"localDir\\" renovate/gradle bash -l -c \\"gradle --init-script renovate-plugin.gradle renovate\\"",
"options": Object {
"cwd": "localDir",
"encoding": "utf-8",
@ -554,7 +554,13 @@ Array [
exports[`manager/gradle extractPackageFile should use docker if required 1`] = `
Array [
Object {
"cmd": "docker run --rm -v \\"localDir\\":\\"localDir\\" -w \\"localDir\\" renovate/gradle gradle --init-script renovate-plugin.gradle renovate",
"cmd": "docker pull renovate/gradle",
"options": Object {
"encoding": "utf-8",
},
},
Object {
"cmd": "docker run --rm -v \\"localDir\\":\\"localDir\\" -w \\"localDir\\" renovate/gradle bash -l -c \\"gradle --init-script renovate-plugin.gradle renovate\\"",
"options": Object {
"cwd": "localDir",
"encoding": "utf-8",

View file

@ -8,6 +8,7 @@ import { envMock, mockExecAll } from '../../../test/execUtil';
import * as _env from '../../util/exec/env';
import { mocked } from '../../../test/util';
import { BinarySource } from '../../util/exec/common';
import { setUtilConfig } from '../../util';
jest.mock('fs-extra');
jest.mock('child_process');
@ -47,6 +48,7 @@ describe('manager/gradle', () => {
platform.getFile.mockResolvedValue('some content');
env.getChildProcessEnv.mockReturnValue(envMock.basic);
setUtilConfig(config);
});
describe('extractPackageFile', () => {
@ -192,6 +194,7 @@ describe('manager/gradle', () => {
});
it('should use docker if required', async () => {
setUtilConfig({ ...config, binarySource: BinarySource.Docker });
const execSnapshots = mockExecAll(exec, gradleOutput);
const configWithDocker = {
@ -200,11 +203,11 @@ describe('manager/gradle', () => {
};
await manager.extractAllPackageFiles(configWithDocker, ['build.gradle']);
expect(exec.mock.calls[0][0].includes('docker run')).toBe(true);
expect(execSnapshots).toMatchSnapshot();
});
it('should use docker even if gradlew is available', async () => {
setUtilConfig({ ...config, binarySource: BinarySource.Docker });
const execSnapshots = mockExecAll(exec, gradleOutput);
const configWithDocker = {
@ -214,7 +217,6 @@ describe('manager/gradle', () => {
};
await manager.extractAllPackageFiles(configWithDocker, ['build.gradle']);
expect(exec.mock.calls[0][0].includes('docker run')).toBe(true);
expect(execSnapshots).toMatchSnapshot();
});
});

View file

@ -1,7 +1,7 @@
import { access, constants, exists } from 'fs-extra';
import upath from 'upath';
import { exec } from '../../util/exec';
import { exec, ExecOptions } from '../../util/exec';
import { logger } from '../../logger';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';
import { VERSION_SCHEME_MAVEN } from '../../constants/version-schemes';
@ -44,27 +44,17 @@ async function getGradleCommandLine(
config: ExtractConfig,
cwd: string
): Promise<string> {
let cmd: string;
const args = GRADLE_DEPENDENCY_REPORT_OPTIONS;
if (config.binarySource === BinarySource.Docker) return `gradle ${args}`;
const gradlewPath = upath.join(cwd, 'gradlew');
const gradlewExists = await exists(gradlewPath);
const gradlewExecutable = gradlewExists && (await canExecute(gradlewPath));
if (gradlewExecutable) return `./gradlew ${args}`;
if (gradlewExists) return `sh gradlew ${args}`;
if (config.binarySource === BinarySource.Docker) {
cmd = `docker run --rm `;
// istanbul ignore if
if (config.dockerUser) {
cmd += `--user=${config.dockerUser} `;
}
cmd += `-v "${cwd}":"${cwd}" -w "${cwd}" `;
cmd += `renovate/gradle gradle`;
} else if (gradlewExecutable) {
cmd = './gradlew';
} else if (gradlewExists) {
cmd = 'sh gradlew';
} else {
cmd = 'gradle';
}
return cmd + ' ' + GRADLE_DEPENDENCY_REPORT_OPTIONS;
return `gradle ${args}`;
}
async function executeGradle(
@ -73,17 +63,21 @@ async function executeGradle(
): Promise<void> {
let stdout: string;
let stderr: string;
const gradleTimeout =
const timeout =
config.gradle && config.gradle.timeout
? config.gradle.timeout * 1000
: undefined;
const cmd = await getGradleCommandLine(config, cwd);
const execOptions: ExecOptions = {
timeout,
cwd,
docker: {
image: 'renovate/gradle',
},
};
try {
logger.debug({ cmd }, 'Start gradle command');
({ stdout, stderr } = await exec(cmd, {
cwd,
timeout: gradleTimeout,
}));
({ stdout, stderr } = await exec(cmd, execOptions));
} catch (err) {
// istanbul ignore if
if (err.code === TIMEOUT_CODE) {