@@ -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 }
0 commit comments