Completed
Pull Request — master (#1)
by Jindun
04:17
created

DeployKubernetesJob   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 26
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A newDeployOnGCloud() 0 32 1
1
<?php
2
3
4
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job;
5
6
use TheAentMachine\AentGitLabCI\Exception\JobException;
7
8
final class DeployKubernetesJob extends AbstractDeployJob
9
{
10
    /**
11
     * @param string $identifier
12
     * @param string $k8sBasePath
13
     * @param bool $isManual
14
     * @return DeployKubernetesJob
15
     * @throws JobException
16
     */
17
    public static function newDeployOnGCloud(string $identifier, string $k8sBasePath, bool $isManual): self
18
    {
19
        $self = new self($identifier);
20
21
        $self->image = 'claranet/gcloud-kubectl-docker:1.2.0';
22
        $self->variables = [
23
            'KUBECONFIG' => '/root/.kube/config',
24
            'GCLOUD_SERVICE_KEY_BASE64' => 'You should put this value in your secrets CI variables!',
25
            'GCLOUD_PROJECT' => 'You should put this value in your secrets CI variables!',
26
            '$ZONE' => 'You should put this value in your secrets CI variables!',
27
            'GKE_CLUSTER' => 'You should put this value in your secrets CI variables!',
28
            'K8S_BASE_PATH' => $k8sBasePath,
29
        ];
30
        $self->script = [
31
            'echo $GCLOUD_SERVICE_KEY_BASE64 | base64 -d > /secret.json',
32
            'gcloud auth activate-service-account --key-file /secret.json',
33
            'gcloud config set project $GCLOUD_PROJECT',
34
            'gcloud container clusters get-credentials $GKE_CLUSTER --zone $ZONE --project $GCLOUD_PROJECT',
35
            'chmod +x /kubectl',
36
            '/kubectl create namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} || true',
37
            '/kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} delete all --all',
38
            'cd ${K8S_BASE_PATH}', '# Looping through directories and applying k8s config files',
39
            'for template_file in $(find . -type f -name "*.templates"); do sed -e "s/#ENVIRONMENT#/${CI_COMMIT_REF_SLUG}/g" $template_file > ${template_file::-9}; done',
40
            'for yml_file in $(find . -type f -name "*.yml" -or -name "*.yaml"); do /kubectl -n ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} apply -f yml_file; done',
41
            'sed -e "s/#ENVIRONMENT#/${CI_COMMIT_REF_SLUG}/g" web.yaml.template > web.yaml',
42
        ];
43
44
        $self->addOnly('branches');
45
        $self->addExcept('master');
46
        $self->manual = $isManual;
47
48
        return $self;
49
    }
50
}
51