fix: use @actions/exec to execute docker child process

This commit is contained in:
Jeroen de Bruijn 2020-05-03 21:43:00 +02:00
parent 23c4b5f227
commit b435cadb65
No known key found for this signature in database
GPG key ID: 3A2677A1DF38FF9F
4 changed files with 47 additions and 19 deletions

13
package-lock.json generated
View file

@ -9,6 +9,19 @@
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.4.tgz",
"integrity": "sha512-YJCEq8BE3CdN8+7HPZ/4DxJjk/OkZV2FFIf+DlZTC/4iBlzYCD5yjRR6eiOS5llO11zbRltIRuKAjMKaWTE6cg=="
},
"@actions/exec": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.0.4.tgz",
"integrity": "sha512-4DPChWow9yc9W3WqEbUj8Nr86xkpyE29ZzWjXucHItclLbEW6jr80Zx4nqv18QL6KK65+cifiQZXvnqgTV6oHw==",
"requires": {
"@actions/io": "^1.0.1"
}
},
"@actions/io": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.2.tgz",
"integrity": "sha512-J8KuFqVPr3p6U8W93DOXlXW6zFvrQAJANdS+vw0YhusLIq+bszW8zmK2Fh1C2kDPX8FMvwIl1OUcFgvJoXLbAg=="
},
"@babel/code-frame": {
"version": "7.8.3",
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.8.3.tgz",

View file

@ -31,7 +31,8 @@
},
"homepage": "https://github.com/renovatebot/github-action#readme",
"dependencies": {
"@actions/core": "1.2.4"
"@actions/core": "1.2.4",
"@actions/exec": "1.0.4"
},
"devDependencies": {
"@commitlint/cli": "8.3.5",

View file

@ -2,11 +2,14 @@ import * as core from '@actions/core';
import Input from './input';
import Renovate from './renovate';
try {
function run(): Promise<void> {
const input = new Input();
const renovate = new Renovate(input.configurationFile, input.token);
renovate.runDockerContainer();
} catch (error) {
console.log(error);
core.setFailed(error.message);
return renovate.runDockerContainer();
}
run().catch((error) => {
console.error(error);
core.setFailed(error.message);
});

View file

@ -1,5 +1,5 @@
import Docker from './docker';
import child from 'child_process';
import { exec } from '@actions/exec';
import fs from 'fs';
import path from 'path';
@ -19,24 +19,35 @@ class Renovate {
this.docker = new Docker();
}
runDockerContainer(): void {
const commandArguments = [
'--rm',
`--env ${this.configFileEnv}='${this.configFileMountPath()}'`,
`--env ${this.tokenEnv}='${this.token}'`,
`--volume ${this.configFile}:${this.configFileMountPath()}`,
this.docker.image(),
];
const command = `docker run ${commandArguments.join(' ')}`;
// console.log(commandArguments, command);
runDockerContainer(): Promise<void> {
return new Promise((resolve, reject) => {
const commandArguments = [
'--rm',
`--env ${this.configFileEnv}=${this.configFileMountPath()}`,
`--env ${this.tokenEnv}=${this.token}`,
`--volume ${this.configFile}:${this.configFileMountPath()}`,
this.docker.image(),
];
const command = `docker run ${commandArguments.join(' ')}`;
child.execSync(command, { stdio: 'inherit' });
exec(command)
.then((code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`'docker run' failed with exit code ${code}.`));
}
})
.catch((error) => {
reject(error);
});
});
}
private validateArguments(): void {
if (!fs.existsSync(this.configFile)) {
throw new Error(
`Could not locate configuration file '${this.configFile}'.`,
`Could not locate configuration file '${this.configFile}'.`
);
}
}