Completed
Push — master ( 4fe608...5ceeaf )
by Jindun
12s
created

CleanupKubernetesJob   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 28
dl 0
loc 48
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A newCleanup() 0 36 4
1
<?php
2
3
4
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job;
5
6
use TheAentMachine\AentGitLabCI\Exception\JobException;
7
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel;
8
9
final class CleanupKubernetesJob extends AbstractCleanupJob
10
{
11
    /**
12
     * @param string $identifier
13
     * @param string $registryDomainName
14
     * @param string $projectGroup
15
     * @param string $projectName
16
     * @param BranchesModel $branchesModel
17
     * @param bool $isManual
18
     * @return CleanupKubernetesJob
19
     * @throws JobException
20
     */
21
    public static function newCleanup(string $identifier, string $registryDomainName, string $projectGroup, string $projectName, BranchesModel $branchesModel, bool $isManual): self
22
    {
23
        $self = new self($identifier);
24
25
        $self->image = 'thecodingmachine/k8s-gitlabci:latest';
26
        $self->variables = [
27
            'GCLOUD_SERVICE_KEY_BASE64' => 'You should put this value in your secrets CI variables!',
28
            'GCLOUD_PROJECT' => 'You should put this value in your secrets CI variables!',
29
            'GKE_CLUSTER' => 'You should put this value in your secrets CI variables!',
30
            'ZONE' => 'You should put this value in your secrets CI variables!',
31
            'KUBECONFIG' => '/root/.kube/config',
32
            'REGISTRY_DOMAIN_NAME' => $registryDomainName,
33
            'PROJECT_GROUP' => $projectGroup,
34
            'PROJECT_NAME' => $projectName
35
        ];
36
        $scriptTag = $branchesModel->isSingleBranch() ? strtolower($branchesModel->getBranches()[0]) : '${CI_COMMIT_REF_SLUG}';
37
        $self->script = [
38
            'delete_image.sh ${REGISTRY_DOMAIN_NAME}/${PROJECT_GROUP}/${PROJECT_NAME}:' . $scriptTag,
39
            'echo $GCLOUD_SERVICE_KEY_BASE64 | base64 -d > /secret.json',
40
            'gcloud auth activate-service-account --key-file /secret.json',
41
            'gcloud config set project $GCLOUD_PROJECT',
42
            'gcloud container clusters get-credentials $GKE_CLUSTER --zone $ZONE --project $GCLOUD_PROJECT',
43
            'chmod +x /kubectl',
44
            '/kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all',
45
            '/kubectl delete namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG}',
46
        ];
47
48
        foreach ($branchesModel->getBranches() as $branch) {
49
            $self->addOnly($branch);
50
        }
51
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
52
            $self->addExcept($branch);
53
        }
54
        $self->manual = $isManual;
55
56
        return $self;
57
    }
58
}
59