BuildDockerfileJob   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 30 5
1
<?php
2
3
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job;
4
5
use TheAentMachine\AentGitLabCI\Context\BaseGitLabCIContext;
6
use TheAentMachine\AentGitLabCI\Exception\JobException;
7
8
final class BuildDockerfileJob extends AbstractBuildJob
9
{
10
    /**
11
     * BuildDockerfileJob constructor.
12
     * @param string $serviceName
13
     * @param string $dockerfileName
14
     * @param BaseGitLabCIContext $context
15
     * @throws JobException
16
     */
17
    public function __construct(string $serviceName, string $dockerfileName, BaseGitLabCIContext $context)
18
    {
19
        parent::__construct($context->getEnvironmentName(), $serviceName);
20
        $branchesModel = $context->getBranchesModel();
21
        $registryDomainName = $context->getRegistryDomainName();
22
        $projectGroup = $context->getProjectGroup();
23
        $projectName = $context->getProjectName();
24
        $this->isSingleBranch = $branchesModel->isSingleBranch();
25
        $tag = $this->isSingleBranch ? strtolower($branchesModel->getBranches()[0]) : '#ENVIRONMENT#';
26
        $this->dockerImageName = "$registryDomainName/$projectGroup/$projectName:$tag";
27
        $this->image = 'docker:git';
28
        $this->services[] = 'docker:dind';
29
        $this->variables = [
30
            'DOCKER_DRIVER' => 'overlay2',
31
            'REGISTRY_DOMAIN_NAME' => $registryDomainName,
32
            'PROJECT_GROUP' => $projectGroup,
33
            'PROJECT_NAME' => $projectName,
34
            'DOCKERFILE_NAME' => $dockerfileName
35
        ];
36
        $scriptTag = $this->isSingleBranch ? strtolower($branchesModel->getBranches()[0]) : '${CI_COMMIT_REF_SLUG}';
37
        $this->script = [
38
            'docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${REGISTRY_DOMAIN_NAME}',
39
            'docker build -t ${REGISTRY_DOMAIN_NAME}/${PROJECT_GROUP}/${PROJECT_NAME}:' . $scriptTag . ' -f ${DOCKERFILE_NAME} .',
40
            'docker push ${REGISTRY_DOMAIN_NAME}/${PROJECT_GROUP}/${PROJECT_NAME}:' . $scriptTag
41
        ];
42
        foreach ($branchesModel->getBranches() as $branch) {
43
            $this->addOnly($branch);
44
        }
45
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
46
            $this->addExcept($branch);
47
        }
48
    }
49
}
50