Gitpod Prebuild State Persistence for Interactive Setup
0 reputation · 24 Jan 2021, 23:19 UTC
0 reputation · 24 Jan 2021, 23:19 UTC
Gitpod Workspace Prebuilds utilize a snapshot mechanism to capture the filesystem state after .gitpod.yml tasks complete. This allows environments to launch with dependencies already installed, bypassing time-consuming initialization scripts.
A challenge arises when the environment setup requires a specific state that cannot be fully automated via non-interactive scripts, or when the filesystem snapshot must be conditionally applied based on external runtime variables not available during the prebuild phase.
Since runtime memory and active network connections are not captured in the snapshot, it is unclear how to handle dependencies that require a one-time interactive authentication or a dynamic token exchange during the prebuild process without stalling the automation.
34775 reputation · 25 Jan 2021, 06:03 UTC
1. Can prebuild snapshots pause for external input without triggering a timeout?
No. A prebuild runs in a headless, non‑interactive environment. All commands in the prebuild section must finish within the allotted prebuild timeout (usually 15 minutes). If a step requires user interaction, it will fail and the prebuild will be marked as failed.
2. Is there a mechanism to inject runtime secrets into a prebuilt snapshot that preserves the filesystem state?
Secrets cannot be baked into the prebuild snapshot because the snapshot is a read‑only image of the filesystem. The only supported way to supply secrets is at workspace start via Gitpod secrets or environment variables. Those secrets are injected into the workspace’s runtime environment, not into the prebuilt image itself.
However, you can use the prebuild to create configuration files that reference placeholders. At workspace start, an onCreate or tasks script can replace those placeholders with secret values, thereby persisting the final state in the workspace’s filesystem.
Define a prebuild that writes a template config file. This file should include a placeholder for the secret (e.g., __TOKEN__).
prebuild:
tasks:
- init: |
echo "apiToken=__TOKEN__" > .config/token.conf
Use the onCreate section to run an interactive script after the workspace starts. This script can prompt the user for a token or read it from a Gitpod secret and replace the placeholder.
onCreate:
- init: |
if [[ -z "$TOKEN" ]]; then
read -p "Enter your API token: " TOKEN
fi
sed -i "s/__TOKEN__/$TOKEN/g" .config/token.conf
Optionally, cache the dependency installation in the prebuild to speed subsequent workspaces.
cache:
- node_modules
Commit the .gitpod.yml and any template files to the repository.
Verify the prebuild by opening the Prebuilds tab in the Gitpod UI. Check that the image shows a cache hit and that the .config/token.conf file exists after workspace launch.
Use gp status in the terminal to confirm that the secret environment variable $TOKEN is present.
.gitpod.yml and confirm prebuild, onCreate, and cache sections are present..config/token.conf containing the token value.gp secret list to ensure the secret is defined and not stored in the repository.To tailor the recommendation further, could you confirm whether the token you need to inject is already available as a Gitpod secret, or will it have to be entered manually during workspace start?
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 25 Jan 2021, 08:47 UTC
A key distinction often missed is that Gitpod prebuild snapshots capture only the static filesystem state produced by init tasks; they do not preserve runtime credentials, network sockets, or in-memory sessions. When a prebuild writes a config file with placeholders, those are safely persisted in the snapshot, but the actual secret values must be resolved at workspace start via onCreate or command tasks that access Gitpod-provided environment variables. This two-phase approach-prebuild for dependency setup, runtime for secret injection-prevents timeouts from interactive prompts while keeping the prebuilt image lean and shareable. Note that exact timeout limits and env-var scoping can vary between Gitpod SaaS, Flex, and self-hosted runner versions, so verifying behavior per deployment is recommended.