Cut GitHub Codespaces Cold Start with Prebuilds and Lifecycle Hooks
Use prebuilds and dev container lifecycle hooks to start GitHub Codespaces faster without baking secrets into images.
16 Oct 2025, 20:54 UTC

Wait less, code sooner
You open a GitHub Codespace for a feature branch and watch the terminal scroll through package installs, database migrations, and dotfiles setup for several minutes. By the time the editor is ready, the interruption has broken your focus. GitHub Codespaces prebuilds address this by building the dev container on GitHub's infrastructure before you open it, so the codespace starts from a prepared image instead of building from scratch.
This post shows how to combine prebuilds with the dev container lifecycle hooks postCreateCommand and postStartCommand, where secrets fit in, and how to keep prebuild minutes from inflating your bill.
1. What prebuilds actually skip
A prebuild runs the container build defined in .devcontainer/devcontainer.json ahead of time, typically triggered by push or pull request events. When you create a codespace from that branch, GitHub starts from the cached image. Build steps such as installing operating-system packages and language dependencies have already run, so the "Creating your codespace" phase is shorter.
Prebuilds are not a replacement for every setup step. They run without access to user secrets or codespace-specific environment variables. Secrets are injected only after you start the codespace from the prebuilt image.
2. Choose the right lifecycle hook
The devcontainer.json file supports two hooks that sound similar but run at different times:
postCreateCommand: runs once, immediately after the container is created. Use it for one-time work such as installing project dependencies or initializing a local database.postStartCommand: runs every time the codespace starts, including after you resume from sleep. Use it for commands that must execute on each session, such as starting a development server or refreshing environment-specific state.
Putting one-time dependency installation in postStartCommand repeats work on every start. Putting secret-dependent work in postCreateCommand can fail during a prebuild because secrets are not available there.
Worked example: dependency install and dev server
This example assumes a Node.js project. Add or edit .devcontainer/devcontainer.json in your repository:
{
"name": "Node project",
"image": "mcr.microsoft.com/devcontainers/javascript-node:20",
"postCreateCommand": "npm ci",
"postStartCommand": "npm run dev"
}
Make this change from your local terminal or the GitHub web editor. You need write access to the repository to modify .devcontainer. After pushing, GitHub triggers a prebuild for the branch only if prebuilds are enabled for that branch.
The intent is that the first codespace created from the prebuilt image skips the npm ci wait because it ran during the prebuild. Each time you start that codespace, npm run dev launches the dev server. Risk: if npm run dev requires a token that exists only at runtime, the command may fail or start without credentials.
3. Keep secrets out of prebuilds
Prebuilds cannot read encrypted secrets, organization secrets, or codespace-specific environment variables. If your postCreateCommand needs a package registry token or database password, do not hardcode it in devcontainer.json. Instead:
- Run secret-dependent installation in
postStartCommand, after secrets are injected at codespace start. - Or use a separate startup script that reads injected environment variables and configures tools after the codespace is running.
Verify this boundary by checking the Actions tab after a push. Prebuild workflows run without your personal secrets, so any step that references $SECRET_TOKEN will fail or resolve to empty during the prebuild.
4. Control prebuild cost and scope
GitHub bills Codespaces compute and storage, and prebuilds consume Actions build minutes. Enabling prebuilds on every active branch can multiply build minutes, especially if you change devcontainer.json frequently, because each change triggers a new prebuild.
Practical limits:
- Enable prebuilds for the default branch and release branches first.
- Limit feature branches unless your team pays for the extra minutes and needs fast starts on short-lived work.
- Review idle timeout and machine type settings. A high-core codespace left running accrues charges even while idle.
To check consumption, open the GitHub billing page for your account or organization and review Codespaces and Actions usage. Prebuild availability and rates vary by account type and region, so confirm current quotas before enabling organization-wide prebuilds.
Check that it worked
- Open the repository in GitHub Codespaces and inspect
.devcontainer/devcontainer.json. - Check the Actions tab for prebuild workflow runs on push or pull request events.
- Create a codespace from a prebuilt branch and compare the "Creating your codespace" duration against a branch without prebuilds.
- Review billing usage after a few days to confirm prebuild minutes match your expectations.
Prebuilds reduce cold-start time, but they are not free and they cannot see your secrets. Start with the default branch, put one-time installs in postCreateCommand, keep per-start work in postStartCommand, and verify in Actions and billing before expanding to more branches.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.