Skip to content

Commit

Permalink
feat: Set USER_ID and GROUP_ID env-vars for docker-compose-run script…
Browse files Browse the repository at this point in the history
… execution context
  • Loading branch information
stmh committed Nov 11, 2022
1 parent bd8b507 commit 05e0fe6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docs/scripts.md
Expand Up @@ -183,7 +183,9 @@ These script execution-contexts are available

* `docker-compose-run`

the script will be executed in a specific service of a docker-compose-setup. This will give you greater control when your app needs specific services running. When setting the context to `docker-compose-run` you need to provide the path to the `docker-compose.yml` file, the name of the service phab should use to execute the commands in and some other, optional parameters. Here's a full-fledge example:
the script will be executed in a specific service of a docker-compose-setup. This will give you greater control when your app needs specific services running. When setting the context to `docker-compose-run` you need to provide the path to the `docker-compose.yml` file, the name of the service phab should use to execute the commands in and some other, optional parameters. Phab will autopopulate the environment variables `USER_ID` and `GROUP_ID` with the current user- and group-id, so you can workaround permission issues.

Here's a full-fledge example:

```yaml
scripts:
Expand All @@ -194,6 +196,7 @@ These script execution-contexts are available
- vendor/bin/phpunit
context: docker-compose-run
rootFolder: ./hosting/tests
workingDir: /app
pullLatestImage: true
shellExecutable: /bin/bash # defaults to /bin/sh
service: php
Expand Down
11 changes: 10 additions & 1 deletion src/Method/ScriptExecutionContext.php
Expand Up @@ -105,8 +105,17 @@ public function enter(ShellProviderInterface $shell): ShellProviderInterface
switch ($this->currentContextName) {
case self::DOCKER_COMPOSE_RUN:
$this->dockerComposeRootDir = $this->getArgument('rootFolder');
$shell->applyEnvironment($this->getArgument('environment', []));
$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);
if ($this->getArgument('pullLatestImage', true)) {
$shell->run($this->getDockerComposeCmd('pull'), false, true);
}
Expand Down
5 changes: 5 additions & 0 deletions src/ShellProvider/CommandResult.php
Expand Up @@ -37,6 +37,11 @@ public function getOutput(): array
return $this->lines;
}

public function getTrimmedOutput(): string
{
return trim(implode("\n", $this->lines));
}

public function getExitCode()
{
return $this->exitCode;
Expand Down

0 comments on commit 05e0fe6

Please sign in to comment.