Skip to content

Commit

Permalink
fix: Apply environment correctly when using script execution context
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Nov 11, 2022
1 parent 6098127 commit 3890447
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/Method/ScriptExecutionContext.php
Expand Up @@ -106,16 +106,7 @@ public function enter(ShellProviderInterface $shell): ShellProviderInterface
case self::DOCKER_COMPOSE_RUN:
$this->dockerComposeRootDir = $this->getArgument('rootFolder');
$shell->cd($this->dockerComposeRootDir);
$environment = $this->getArgument('environment', []);
$environment['USER_ID'] = $this->getArgument(
'user',
$shell->run('id -u', true, true)->getTrimmedOutput()
);
$environment['GROUP_ID'] = $this->getArgument(
'group',
$shell->run('id -g', true, true)->getTrimmedOutput()
);
$shell->applyEnvironment($environment);
$this->applyEnvironmentToHostShell($shell);
if ($this->getArgument('pullLatestImage', true)) {
$shell->run($this->getDockerComposeCmd('pull'), false, true);
}
Expand All @@ -135,7 +126,7 @@ public function enter(ShellProviderInterface $shell): ShellProviderInterface
if (!$working_dir) {
throw new \RuntimeException(sprintf('Can\'t resolve working dir %s!', $root_folder));
}
$shell->applyEnvironment($this->getArgument('environment', []));
$this->applyEnvironmentToHostShell($shell);
if ($this->getArgument('pullLatestImage', true)) {
$shell->run(sprintf('docker pull %s', $this->getArgument('image')));
}
Expand Down Expand Up @@ -194,6 +185,8 @@ public function exit()
$this->shell->cd($this->initialWorkingDir);
$this->shell->cd($this->dockerComposeRootDir);

$this->applyEnvironmentToHostShell($this->shell);

$this->shell->run($this->getDockerComposeCmd('down', '-v'));
}
}
Expand Down Expand Up @@ -230,4 +223,23 @@ private function getDockerComposeCmd($cmd, ...$args): string
$result = $this->getDockerComposeCmdAsArray($cmd, ...$args);
return implode(' ', $result);
}

/**
* @param \Phabalicious\ShellProvider\ShellProviderInterface $shell
*
* @return void
*/
protected function applyEnvironmentToHostShell(ShellProviderInterface $shell): void
{
$environment = $this->getArgument('environment', []);
$environment['USER_ID'] = $this->getArgument(
'user',
$shell->run('id -u', true, true)->getTrimmedOutput()
);
$environment['GROUP_ID'] = $this->getArgument(
'group',
$shell->run('id -g', true, true)->getTrimmedOutput()
);
$shell->applyEnvironment($environment);
}
}

0 comments on commit 3890447

Please sign in to comment.