Is there any way to persist files when restarting a coder workspace?

I’m using coder_agent, and in the startup_script, I have logic that clones a Git repo and runs yarn install. The problem is that the yarn install step takes about 10 minutes, which significantly slows down workspace startup. However, I only really need this to run the first time the workspace is created — not every time it’s restarted. I tried adding a check like this:
if [ ! -d "/home/coder/my-repo" ]; then
echo "Repository not found. Cloning and installing dependencies..."
git clone https://example.com/my-repo.git /home/coder/my-repo
cd /home/coder/my-repo
yarn install
else
echo "Repository already exists. Skipping setup."
fi
if [ ! -d "/home/coder/my-repo" ]; then
echo "Repository not found. Cloning and installing dependencies..."
git clone https://example.com/my-repo.git /home/coder/my-repo
cd /home/coder/my-repo
yarn install
else
echo "Repository already exists. Skipping setup."
fi
But the condition always returns false (the repo isn't present). Is there a recommended way to run expensive setup steps only on initial workspace creation, and skip them on restarts? Thanks in advance!
16 Replies
Codercord
Codercord2w ago
<#1358895812154888452>
Category
Help needed
Product
Coder (v2)
Platform
Linux
Logs
Please post any relevant logs/error messages.
Igor Zimmer
Igor ZimmerOP2w ago
Still can't make it work
Scott
Scott2w ago
What runtime environment are you running Coder in? Docker, k8s, VM?
Igor Zimmer
Igor ZimmerOP2w ago
VM, on an EC2 Instance /close
Phorcys
Phorcys2w ago
hey, were you able to get this solved? apologies for the delays
Igor Zimmer
Igor ZimmerOP2w ago
Yes, I just needed to alocate an EIP manually in my main.tf template I think it's because of my specific need (VPN, private subnets, etc) But now it works I have one question, is there a way to force a port forward in the template? I currently need to add a port (10000) manually in my VSCode port forward
Phorcys
Phorcys2w ago
good thanks for following up what do you mean? you can define a coder_app resource to have a custom button in the web ui like VSCode and others that points to a specific URL https://registry.terraform.io/providers/coder/coder/latest/docs/resources/app
Igor Zimmer
Igor ZimmerOP2w ago
My application needs a specific port, it automatically forwards some of them (like in the image), but some of them (like the 10000) I needed to add manually
No description
Igor Zimmer
Igor ZimmerOP2w ago
I don't really know if it's a Coder specific question tbh
Phorcys
Phorcys2w ago
VSCode is detecting these ports automatically by what's running in the env and what you're running in the terminal there should be a way to tell VSCode to automatically forward certain ports but I can't seem to find it, it should be some kind of json file in your project but honestly i would recommend using coder_apps instead, or Coder's port-forwarding feature, it will save you the hassle of having to forward something locally
Phorcys
Phorcys2w ago
all these buttons are coder_apps and can be configured to reverse-proxy a specific URL through Coder
No description
Phorcys
Phorcys2w ago
and the port-forwarding lets you open any http server that's listening in the workspace
No description
Phorcys
Phorcys2w ago
of course this is all limited to http/https in the web, so if you need other protocols then VSCode's port-forwarding or coder port-forward is your best bet
Igor Zimmer
Igor ZimmerOP7d ago
For my team's setup we ended up just extending our config.json file that had some other configs to port forward what we needed, it was only 5 ports so it wasn't much of a problem to do it this way But thank you so much for taking the time to explain the coder_app feature, it will be useful for another part of our project
Phorcys
Phorcys7d ago
yeah pretty much sounds good!
Codercord
Codercord7d ago
@Phorcys closed the thread.

Did you find this page helpful?