How I used ChatGPT to automate a tedious process at work
As a developer, when I make a code change, I have to deploy it to a testing environment to verify the end-to-end working of the code. The process looks like this:
- I start deployment to a sandbox Kubernetes cluster.
- Once it’s deployed I find a pod from my latest deployment identifier. I wait till that pod is in a running state.
- Then I port forward my local port to that pod’s port.
I used to run a few commands in the terminal to achieve the above.
If you’re still thinking about how ChatGPT can automate this, let me break your heart. ChatGPT didn’t automate this. However, as I mentioned above, I used ChatGPT to automate this process. There’s a difference. I asked ChatGPT to write a shell script that automates the above process.
So don’t run away from here thinking it’s of no use. By the end of this article, you’ll be able to do the same with the help of ChatGPT. Here’s the outcome of that script. And I’ll show you how to run this script from any directory in your terminal.
% setupSandbox.sh 0459
Logged into Kubernetes cluster "sandbox-cell-001.prod-main-us-west-2". Try 'kubectl version' to test the connection.
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Waiting for pod to be in Running state...
Pod is running: web-sandbox-b-smit-0459-cc5d47cd4-kb8vc
Forwarding from 127.0.0.1:50051 -> 50051
Forwarding from [::1]:50051 -> 50051
Handling connection for 50051
Here’s my prompt to ChatGPT
write shell script that does following
export CELL=sandbox-cell-001.prod-main-us-west-2
export CLUSTER=teleport-production-sandbox-cell-001.prod-main-us-west-2
tsh kube login $CELL
run "kubectl get pods --context=$CLUSTER -n onboarding-service-sandbox | grep web-sandbox-b-smit-0346" until it returns data in format "onboarding-service-web-sandbox-b-smit-0346-5c55879889-7ztzz 1/1 Running 0 17m" format. make sure it's in "Running" state.
set "onboarding-service-web-sandbox-b-smit-0346-5c55879889-7ztzz" to POD_NAME variable
run "kubectl -n onboarding-service-sandbox port-forward --context=$CLUSTER $POD_NAME 50051:50051"
Once it returned the script, I prompted
take "0346" as argument
Now that I have a script, I went to my scripts directory and pasted it to setupSandbox.sh
file
cd ~/Projects/scripts
vi setupSandbox.sh
Make the file executable
chmod +x setupSandbox.sh
Now I can run this script
./setupSandbox.sh 0346
How to run it from any directory?
If my current directory is not ~/Projects/scripts
, I have to run scripts like this ~/Projects/scripts/setupSandbox.sh 0346
, and it's tedious. To overcome this, just update PATH
variable in the shell to include ~/Projects/scripts
.
export PATH=$PATH:~/Projects/scripts
or update it in bash file (~/.bashrc, ~/.zshrc)
Whenever you type a command in shell, it looks for that executable command in all directories mentioned via PATH variable.
Now I can run all scripts from ~/Projects/scripts
in any directory in my terminal.
Pro tip: If you type the first few chars of the command (e.g. setu
) and then press TAB, then the terminal will show you setupSandbox.sh
as a suggestion.