|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentGitLabCI\Command; |
|
4
|
|
|
|
|
5
|
|
|
use TheAentMachine\AentGitLabCI\Aenthill\Metadata; |
|
6
|
|
|
use TheAentMachine\AentGitLabCI\Exception\PayloadException; |
|
7
|
|
|
use TheAentMachine\AentGitLabCI\Exception\GitLabCIFileException; |
|
8
|
|
|
use TheAentMachine\AentGitLabCI\Exception\JobException; |
|
9
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\GitLabCIFile; |
|
10
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\BuildDockerfileJob; |
|
11
|
|
|
use TheAentMachine\Aenthill\CommonEvents; |
|
12
|
|
|
use TheAentMachine\Aenthill\CommonMetadata; |
|
13
|
|
|
use TheAentMachine\Aenthill\Manifest; |
|
14
|
|
|
use TheAentMachine\Command\AbstractJsonEventCommand; |
|
15
|
|
|
use TheAentMachine\Exception\ManifestException; |
|
16
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
|
17
|
|
|
|
|
18
|
|
|
final class NewBuildImageJobCommand extends AbstractJsonEventCommand |
|
19
|
|
|
{ |
|
20
|
|
|
protected function getEventName(): string |
|
21
|
|
|
{ |
|
22
|
|
|
return CommonEvents::NEW_BUILD_IMAGE_JOB_EVENT; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param array<string,string> $payload |
|
27
|
|
|
* @return array|null |
|
28
|
|
|
* @throws GitLabCIFileException |
|
29
|
|
|
* @throws PayloadException |
|
30
|
|
|
* @throws JobException |
|
31
|
|
|
* @throws ManifestException |
|
32
|
|
|
* @throws MissingEnvironmentVariableException |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function executeJsonEvent(array $payload): ?array |
|
35
|
|
|
{ |
|
36
|
|
|
$aentHelper = $this->getAentHelper(); |
|
37
|
|
|
$aentHelper->title("GitLab CI: adding a build stage"); |
|
38
|
|
|
|
|
39
|
|
|
// TODO handle many branches |
|
40
|
|
|
$envName = Manifest::mustGetMetadata(CommonMetadata::ENV_NAME_KEY); |
|
41
|
|
|
$registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY); |
|
42
|
|
|
$projectGroup = Manifest::mustGetMetadata(Metadata::PROJECT_GROUP_KEY); |
|
43
|
|
|
$projectName = Manifest::mustGetMetadata(Metadata::PROJECT_NAME_KEY); |
|
44
|
|
|
$branch = Manifest::mustGetMetadata(Metadata::BRANCH_KEY); |
|
45
|
|
|
|
|
46
|
|
|
$this->validatePayload($payload); |
|
47
|
|
|
$serviceName = $payload['serviceName']; |
|
48
|
|
|
$dockerfileName = $payload['dockerfileName']; |
|
49
|
|
|
|
|
50
|
|
|
$aentHelper->spacer(); |
|
51
|
|
|
$this->output->writeln("🦊 Dockerfile: <info>$dockerfileName</info>"); |
|
52
|
|
|
$aentHelper->spacer(); |
|
53
|
|
|
|
|
54
|
|
|
$job = new BuildDockerfileJob( |
|
55
|
|
|
$envName, |
|
56
|
|
|
$serviceName, |
|
57
|
|
|
$registryDomainName, |
|
58
|
|
|
$projectGroup, |
|
59
|
|
|
$projectName, |
|
60
|
|
|
$branch |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$file = new GitLabCIFile(); |
|
64
|
|
|
$file->findOrCreate(); |
|
65
|
|
|
|
|
66
|
|
|
$this->output->writeln('🦊 <info>' . GitLabCIFile::DEFAULT_FILENAME . '</info> has been successfully updated!'); |
|
67
|
|
|
|
|
68
|
|
|
return [ |
|
69
|
|
|
'variableEnvironment' => $job->isVariableEnvironment(), |
|
70
|
|
|
'dockerImageName' => $job->getDockerImageName() |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array<string,string> $payload |
|
76
|
|
|
* @throws PayloadException |
|
77
|
|
|
*/ |
|
78
|
|
|
private function validatePayload(array $payload): void |
|
79
|
|
|
{ |
|
80
|
|
|
if (empty($payload)) { |
|
81
|
|
|
throw PayloadException::emptyPayload($this->getEventName()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!isset($payload['serviceName'])) { |
|
85
|
|
|
throw PayloadException::missingServiceName(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!isset($payload['dockerfileName'])) { |
|
89
|
|
|
throw PayloadException::missingDockerfileName(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|