Skip to content

Commit db94ba7

Browse files
authored
Merge pull request #1 from nueip/gunter.chou-gke
[Dev] Add deploy GKE
2 parents 408ee36 + 1da57a9 commit db94ba7

File tree

4 files changed

+217
-5
lines changed

4 files changed

+217
-5
lines changed

config.inc.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,32 @@
6868
],
6969
'verbose' => false,
7070
],
71+
// This project config processes deploy to GKE
72+
'deployToGKE' => [
73+
'git' => [
74+
'enabled' => false,
75+
],
76+
'rsync' => [
77+
'enabled' => false,
78+
],
79+
'gke' => [
80+
'enabled' => true,
81+
'projectId' => 'gcp-deploy',
82+
'cluster' => 'gke-cluster',
83+
'region' => 'asia-east1',
84+
'docker' => [
85+
'name' => 'backend-docker',
86+
'tag' => date('Ymd.His'),
87+
'git' => [
88+
'url' => 'https://<username>:<deploy_token>@gitlab.com/tanuki/awesome_project.git',
89+
'branch' => 'release',
90+
],
91+
],
92+
'k8s' => [
93+
'namespace' => 'default',
94+
'deployment' => 'backend-deploy',
95+
'container' => 'backend-app',
96+
],
97+
],
98+
],
7199
];

src/Deployer.php

Lines changed: 169 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function run()
6969
$this->runTests();
7070
$this->runCommands('before');
7171
$this->runDeploy();
72+
$this->runDeployGke();
7273
$this->runCommands('after');
7374

7475
// Total cost time end
@@ -449,6 +450,170 @@ public function runDeploy()
449450
$this->_done("Deploy");
450451
}
451452

453+
/**
454+
* Deploy GKE Process
455+
*
456+
* @return void
457+
*/
458+
public function runDeployGke()
459+
{
460+
// Config
461+
$config = isset($this->_config['gke']) ? $this->_config['gke'] : [];
462+
463+
// Check enabled
464+
if (!$config['enabled']) {
465+
return;
466+
}
467+
468+
$projectId = $config['projectId'];
469+
$cluster = $config['cluster'];
470+
$region = $config['region'];
471+
472+
$imageName = $config['docker']['name'];
473+
$imageTag = $config['docker']['tag'];
474+
475+
$gitUrl = $config['docker']['git']['url'];
476+
$gitBranch = $config['docker']['git']['branch'];
477+
478+
$namespace = $config['k8s']['namespace'];
479+
$deployment = $config['k8s']['deployment'];
480+
$container = $config['k8s']['container'];
481+
482+
$gcrImage = "gcr.io/$projectId/$imageName";
483+
484+
485+
// Deploy GKE process
486+
$this->_verbose("");
487+
$this->_verbose("### Deploy GKE Process Start");
488+
489+
/**
490+
* Docker build process
491+
*/
492+
// Build command
493+
$cmd = ["docker build --no-cache"];
494+
$cmd[] = "--build-arg SHORT_SHA_ARG=$imageTag";
495+
$cmd[] = "--tag $gcrImage:$imageTag";
496+
$cmd[] = "--tag $gcrImage:latest";
497+
$cmd[] = "$gitUrl#$gitBranch";
498+
499+
$cmd = implode(' ', $cmd);
500+
501+
$this->_verbose("[Command]: $cmd");
502+
503+
// Shell execution
504+
$output = '';
505+
$result = $this->_cmd($cmd, $output);
506+
507+
// Check
508+
if (!$result) {
509+
$this->_verbose($output);
510+
$this->_error("Docker build Process Failed");
511+
}
512+
513+
$this->_verbose("### Docker build Process Result");
514+
$this->_verbose("----------------------------");
515+
$this->_verbose($output);
516+
$this->_verbose("----------------------------");
517+
$this->_verbose("");
518+
519+
/**
520+
* Docker push process
521+
*/
522+
// Push command
523+
$cmd = "docker push $gcrImage:$imageTag && docker push $gcrImage:latest";
524+
525+
$this->_verbose("[Command]: $cmd");
526+
527+
// Shell execution
528+
$output = '';
529+
$result = $this->_cmd($cmd, $output);
530+
531+
// Check
532+
if (!$result) {
533+
$this->_verbose($output);
534+
$this->_error("Docker push Process Failed");
535+
}
536+
537+
$this->_verbose("### Docker push Process Result");
538+
$this->_verbose("----------------------------");
539+
$this->_verbose($output);
540+
$this->_verbose("----------------------------");
541+
$this->_verbose("");
542+
543+
/**
544+
* Connect to GKE
545+
*/
546+
$cmd = "gcloud container clusters get-credentials $cluster --region $region --project $projectId";
547+
548+
$this->_verbose("[Command]: $cmd");
549+
550+
// Shell execution
551+
$output = '';
552+
$result = $this->_cmd($cmd, $output);
553+
554+
// Check
555+
if (!$result) {
556+
$this->_verbose($output);
557+
$this->_error("Connect to GKE Process Failed");
558+
}
559+
560+
$this->_verbose("### Connect to GKE Process Result");
561+
$this->_verbose("----------------------------");
562+
$this->_verbose($output);
563+
$this->_verbose("----------------------------");
564+
$this->_verbose("");
565+
566+
/**
567+
* Kubectl set image process
568+
*/
569+
// Set image command
570+
$cmd = "kubectl set image deployment $deployment $container=$gcrImage:$imageTag --namespace=$namespace --record";
571+
572+
$this->_verbose("[Command]: $cmd");
573+
574+
// Shell execution
575+
$output = '';
576+
$result = $this->_cmd($cmd, $output);
577+
578+
// Check
579+
if (!$result) {
580+
$this->_verbose($output);
581+
$this->_error("Kubectl set image Process Failed");
582+
}
583+
584+
$this->_verbose("### Kubectl set image Process Result");
585+
$this->_verbose("----------------------------");
586+
$this->_verbose($output);
587+
$this->_verbose("----------------------------");
588+
$this->_verbose("");
589+
590+
/**
591+
* Kubectl rollout status process
592+
*/
593+
// Rollout status command
594+
$cmd = "kubectl rollout status deployment $deployment --namespace=$namespace --watch=true";
595+
596+
$this->_verbose("[Command]: $cmd");
597+
598+
// Shell execution
599+
$output = '';
600+
$result = $this->_cmd($cmd, $output);
601+
602+
// Check
603+
if (!$result) {
604+
$this->_verbose($output);
605+
$this->_error("Kubectl rollout status Process Failed");
606+
}
607+
608+
$this->_verbose("### Kubectl rollout status Process Result");
609+
$this->_verbose("----------------------------");
610+
$this->_verbose($output);
611+
$this->_verbose("----------------------------");
612+
$this->_verbose("");
613+
614+
$this->_done("Deploy GKE");
615+
}
616+
452617
/**
453618
* Get project config
454619
*
@@ -466,11 +631,11 @@ public function getConfig()
466631
*/
467632
private function _setConfig($config)
468633
{
469-
if (!isset($config['servers']) || !$config['servers'] || !is_array($config['servers'])) {
634+
if (!$config['gke']['enabled'] && (!isset($config['servers']) || !$config['servers'] || !is_array($config['servers']))) {
470635
throw new Exception('Config not set: servers', 400);
471636
}
472637

473-
if (!isset($config['source']) || !$config['source']) {
638+
if (!$config['gke']['enabled'] && (!isset($config['source']) || !$config['source'])) {
474639
throw new Exception('Config not set: source', 400);
475640
}
476641

@@ -499,13 +664,13 @@ private function _checkConfig()
499664
$config = &$this->_config;
500665

501666
// Check for type of file / directory
502-
if (!is_dir($config['source'])) {
667+
if (!$config['gke']['enabled'] && !is_dir($config['source'])) {
503668

504669
throw new Exception('Source file is not a directory (project)');
505670
}
506671

507672
// Check for type of link
508-
if (is_link($config['source'])) {
673+
if (!$config['gke']['enabled'] && is_link($config['source'])) {
509674

510675
throw new Exception('File input is symblic link');
511676
}

src/ShellConsole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private function _exec($cmd, &$resultText = '', &$output = '', &$errorCode = '')
3333
$cmd = rtrim($cmd, ';');
3434

3535
// stdout
36-
$cmd = "{$cmd} 2>&1;";
36+
$cmd = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "{$cmd} 2>&1" : "{$cmd} 2>&1;";
3737
exec($cmd, $output, $errorCode);
3838

3939
// Build result text

src/default-config.inc.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@
3535
'timeout' => 60,
3636
'identityFile' => null,
3737
],
38+
'gke' => [
39+
'enabled' => false,
40+
'projectId' => '',
41+
'cluster' => '',
42+
'region' => '',
43+
'docker' => [
44+
'name' => '',
45+
'tag' => date('Ymd.His'),
46+
'git' => [
47+
'url' => '',
48+
'branch' => '',
49+
],
50+
],
51+
'k8s' => [
52+
'namespace' => '',
53+
'deployment' => '',
54+
'container' => '',
55+
],
56+
],
3857
'commands' => [
3958
'before' => [
4059
'',

0 commit comments

Comments
 (0)