From 4fa9cad2cc1fb77e5bdb2664cb180bcc05f954f8 Mon Sep 17 00:00:00 2001 From: Jeroen de Bruijn Date: Sun, 3 May 2020 19:14:08 +0200 Subject: [PATCH] fix: mount volume with config file to Docker container --- src/renovate.ts | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/renovate.ts b/src/renovate.ts index eb7eee38..6095810a 100644 --- a/src/renovate.ts +++ b/src/renovate.ts @@ -1,27 +1,53 @@ import Docker from './docker'; import child from 'child_process'; +import fs from 'fs'; +import path from 'path'; class Renovate { - private configurationFileEnv = 'RENOVATE_CONFIG_FILE'; + private configFileEnv = 'RENOVATE_CONFIG_FILE'; private tokenEnv = 'RENOVATE_TOKEN'; + private configFileMountDir = '/github-action'; + private configFile: string; private docker: Docker; - constructor(private configurationFile: string, private token: string) { + constructor(configFile: string, private token: string) { + this.configFile = path.resolve(configFile); + + this.validateArguments(); + this.docker = new Docker(); } runDockerContainer(): void { const commandArguments = [ '--rm', - `--env ${this.configurationFileEnv}='${this.configurationFile}'`, + `--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); child.execSync(command, { stdio: 'inherit' }); } + + private validateArguments(): void { + if (!fs.existsSync(this.configFile)) { + throw new Error( + `Could not locate configuration file '${this.configFile}'.`, + ); + } + } + + private configFileName(): string { + return path.basename(this.configFile); + } + + private configFileMountPath(): string { + return path.join(this.configFileMountDir, this.configFileName()); + } } export default Renovate;