Skip to content

Commit 4621674

Browse files
committed
Add docker:push command
1 parent 121205d commit 4621674

File tree

7 files changed

+119
-42
lines changed

7 files changed

+119
-42
lines changed

bin/common-functions

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
check_laravel() {
4+
if ! [[ -f "${PWD}/public/index.php" ]]; then
5+
echo "Missing [/public/index.php], please run from Laravel base directory.";
6+
exit 1
7+
fi
8+
}
9+
10+
check_dockerfiles() {
11+
if ! [[ -f "$PWD/.docker/nginx.dockerfile" ]]; then
12+
echo "Dockerfile [/.docker/nginx.dockerfile] not found."
13+
echo "Run: php artisan docker:generate"
14+
exit 1
15+
fi
16+
17+
if ! [[ -f "$PWD/.docker/php.dockerfile" ]]; then
18+
echo "Dockerfile [/.docker/php.dockerfile] not found."
19+
echo "Run: php artisan docker:generate"
20+
exit 1
21+
fi
22+
}
23+
24+
check_tags() {
25+
NGINX_TAG="${DOCKER_NGINX_TAG}"
26+
PHP_TAG="${DOCKER_PHP_TAG}"
27+
28+
if [[ -z "${NGINX_TAG}" ]]; then
29+
echo "Environment variable [DOCKER_NGINX_TAG] not found."
30+
exit 1
31+
fi
32+
33+
if [[ -z "${PHP_TAG}" ]]; then
34+
echo "Environment variable [DOCKER_PHP_TAG] not found."
35+
exit 1
36+
fi
37+
}

bin/docker-build

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
#!/usr/bin/env bash
22

3-
PACKAGE="$(dirname "${BASH_SOURCE[0]}")/.."
3+
SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
4+
PACKAGE="${SCRIPT_DIR}/.."
45

5-
if ! [[ -f "${PWD}/public/index.php" ]]; then
6-
echo "Missing [/public/index.php], please run from Laravel base directory.";
7-
exit 1
8-
fi
9-
10-
if ! [[ -f "${PWD}/.dockerignore" ]]; then
11-
echo "Missing [/.dockerignore], copying...";
12-
cp "${PACKAGE}/docker/.dockerignore" "${PWD}/.dockerignore"
13-
fi
14-
15-
if ! [[ -f "$PWD/.docker/nginx.dockerfile" ]]; then
16-
echo "Dockerfile [/.docker/nginx.dockerfile] not found."
17-
echo "Run: php artisan docker:generate"
18-
exit 1
19-
fi
6+
source "${SCRIPT_DIR}/common-functions"
207

21-
if ! [[ -f "$PWD/.docker/php.dockerfile" ]]; then
22-
echo "Dockerfile [/.docker/php.dockerfile] not found."
23-
echo "Run: php artisan docker:generate"
24-
exit 1
25-
fi
8+
check_laravel
9+
check_dockerfiles
10+
check_tags
2611

2712
NGINX_TAG="${DOCKER_NGINX_TAG}"
2813
PHP_TAG="${DOCKER_PHP_TAG}"
2914

30-
if [[ -z "${NGINX_TAG}" ]]; then
31-
echo "Environment variable [DOCKER_NGINX_TAG] not found."
32-
exit 1
33-
fi
34-
35-
if [[ -z "${PHP_TAG}" ]]; then
36-
echo "Environment variable [DOCKER_PHP_TAG] not found."
37-
exit 1
15+
if ! [[ -f "${PWD}/.dockerignore" ]]; then
16+
echo "Missing [/.dockerignore], copying to [${PWD}/.dockerignore]";
17+
cp "${PACKAGE}/docker/.dockerignore" "${PWD}/.dockerignore"
3818
fi
3919

4020
docker build \

bin/docker-push

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
4+
5+
source "${SCRIPT_DIR}/common-functions"
6+
7+
check_laravel
8+
check_dockerfiles
9+
check_tags
10+
11+
NGINX_TAG="${DOCKER_NGINX_TAG}"
12+
PHP_TAG="${DOCKER_PHP_TAG}"
13+
14+
docker push "${NGINX_TAG}"
15+
docker push "${PHP_TAG}"

src/Commands/BaseDockerCommand.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands;
4+
5+
use Symfony\Component\Process\Process;
6+
7+
abstract class BaseDockerCommand extends BaseCommand
8+
{
9+
protected function getEnvironment(): array
10+
{
11+
return [
12+
'DOCKER_NGINX_TAG' => config('docker-builder.tags.nginx'),
13+
'DOCKER_PHP_TAG' => config('docker-builder.tags.php'),
14+
];
15+
}
16+
17+
protected function runProcess(Process $process): int
18+
{
19+
return $process->run(function ($type, $buffer) {
20+
match ($type) {
21+
Process::OUT => fwrite(STDOUT, $buffer),
22+
Process::ERR => fwrite(STDERR, $buffer),
23+
};
24+
});
25+
}
26+
}

src/Commands/DockerBuildCommand.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@
44

55
use Symfony\Component\Process\Process;
66

7-
class DockerBuildCommand extends BaseCommand
7+
class DockerBuildCommand extends BaseDockerCommand
88
{
99
protected $name = 'docker:build';
1010

11+
protected $description = 'Build Docker images';
12+
1113
public function handle(): int
1214
{
1315
$command = package_path('bin/docker-build');
1416

1517
$process = new Process(
1618
command: [$command],
1719
cwd: base_path(),
18-
env: [
19-
'DOCKER_NGINX_TAG' => config('docker-builder.tags.nginx'),
20-
'DOCKER_PHP_TAG' => config('docker-builder.tags.php'),
21-
],
20+
env: $this->getEnvironment(),
2221
);
2322

24-
$process->run(function ($type, $buffer) {
25-
match ($type) {
26-
Process::OUT => fwrite(STDOUT, $buffer),
27-
Process::ERR => fwrite(STDERR, $buffer),
28-
};
29-
});
30-
31-
return self::SUCCESS;
23+
return $this->runProcess($process);
3224
}
3325
}

src/Commands/DockerPushCommand.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands;
4+
5+
use Symfony\Component\Process\Process;
6+
7+
class DockerPushCommand extends BaseDockerCommand
8+
{
9+
protected $name = 'docker:push';
10+
11+
protected $description = 'Push Docker images';
12+
13+
public function handle(): int
14+
{
15+
$command = package_path('bin/docker-push');
16+
17+
$process = new Process(
18+
command: [$command],
19+
cwd: base_path(),
20+
env: $this->getEnvironment(),
21+
);
22+
23+
return $this->runProcess($process);
24+
}
25+
}

src/DockerServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlameButton\LaravelDockerBuilder\Commands\DockerBuildCommand;
66
use BlameButton\LaravelDockerBuilder\Commands\DockerGenerateCommand;
7+
use BlameButton\LaravelDockerBuilder\Commands\DockerPushCommand;
78
use Illuminate\Support\ServiceProvider;
89

910
class DockerServiceProvider extends ServiceProvider
@@ -14,6 +15,7 @@ public function boot(): void
1415
$this->commands([
1516
DockerBuildCommand::class,
1617
DockerGenerateCommand::class,
18+
DockerPushCommand::class,
1719
]);
1820
}
1921

0 commit comments

Comments
 (0)