<p>So, I have a specific chain of kubernetes and bash commands that I have to execute on gcloud, I'd like to know if there is any way to execute those commands automatically via scripting, without the need of actually having to open and interact with gcloud CLI. Maybe using a npm package but i don't know if there is a package for this usage.</p>
<p>I already have gcloud cli installed locally, should exec and/or spawn commands work? I've tried but ultimately failed.</p>
<p>TL;DR: I just want to know how to automate gcloud commands using code! Preferably on node.js, but i can learn it on another language too.</p>

<p>Please provide a little more detail as to what it is you're trying to automate as this will help guide recommendations.</p>
<p>There are several challenges automating subprocesses (e.g. invoking bash and it running <code>gcloud</code>) from within another program. High among these is that shell-scripting doesn't provide very strong parameter passing (and no typing beyond strings) and error handling is difficult.</p>
<p>For NodeJS you can use <a href="https://nodejs.org/api/child_process.html" rel="nofollow noreferrer"><code>child_process</code></a>. You'll need to ensure that the child process environment is able to access the <code>gcloud</code> binary and its configuration and that it can authenticate too. Since you're running a program to interact with <code>gcloud</code>, I recommend you use a service account to authenticate if you choose this approach and then you may need (if you run off-GCP) to provide a service account key to your code too.</p>
<p>There are alternatives although these require more work, they provide much more robust solutions:</p>
<ol>
<li>Use a definitive &quot;infrastructure as code&quot; tools (e.g. <a href="https://terraform.io" rel="nofollow noreferrer">terraform</a>). Google Cloud provides <a href="https://cloud.google.com/blog/products/devops-sre/google-cloud-templates-for-terraform-and-deployment-manager-now-available" rel="nofollow noreferrer">templates</a> for Terraform</li>
<li>Use Google Cloud SDKs (there are 2 flavors <a href="https://cloud.google.com/apis/docs/client-libraries-explained" rel="nofollow noreferrer">explained</a>) and these cover everything except Kubernetes itself (you get management of GKE clusters) and a NodeJS Kubernetes <a href="https://kubernetes.io/docs/reference/using-api/client-libraries/" rel="nofollow noreferrer">client library</a></li>
</ol>
<p>If you add more details to your question, I can add more details to this answer.</p>
