Mount Git repositories in a sandbox¶
When you create a sandbox, you can ask Snowflake to mount one or more of your GitHub repositories into it. A repository holds a project’s files and their full version history, and each one appears as a directory in the sandbox’s filesystem, such as /mnt/repos/monorepo, with that history intact. Code in the sandbox can read, run, and edit the files and use Git commands on them. For example:
- A coding agent can make changes in your codebase, run its tests, and produce a patch for you to review.
- A batch job can run a script stored in a repository against your Snowflake data.
- An evaluation can mount the same repository in many sandboxes so that each run starts from the same code.
Snowflake clones the repository for you through a security integration, so your code doesn’t need to run git clone.
Before you begin¶
Sandboxes reach GitHub through the same GitHub integration that Cortex Code uses. If your account already uses Cortex Code with GitHub, the setup is done, and you can reuse that integration.
Before you mount a repository, make sure that the following setup is complete:
- You can create a sandbox. Your account has Cortex Sandboxes enabled, and you have the Python SDK and a Snowflake connection set up. If you haven’t created a sandbox before, complete the Cortex Sandboxes quickstart first.
- Your account has a GitHub integration. This is a one-time setup by administrators: a GitHub organization administrator installs the Snowflake CoCo GitHub App (the GitHub app for Cortex Code), and a Snowflake account administrator creates a security integration for it. For instructions, see Set up the GitHub integration.
- Your role can use the integration. The administrator grants the
USAGEprivilege on the integration to your role and tells you the integration’s name. You pass that name when you mount a repository. - You’ve connected your GitHub account. You authorize the integration with your own GitHub account once, in Snowsight or with SQL. For instructions, see Connect your GitHub account. Your GitHub account must have access to the repositories that you mount.
- You’re using the Python SDK. The
snow sandboxCLI doesn’t support Git repositories yet.
Mount a repository¶
To mount a repository, create a GitRepo object for it and pass a list of these objects to Sandbox.create in the git_repos argument. Each GitRepo needs two values:
- The repository’s URL.
- The name of the GitHub security integration from Before you begin. Snowflake uses this integration to access GitHub on your behalf when it clones the repository. Ask your administrator for the name if you don’t know it.
The following example mounts one repository and prints its five most recent commits:
The with block terminates the sandbox when the block ends, which discards any changes made in the repository. To keep a sandbox and come back to it later, see Reconnect to a running sandbox.
GitRepo also takes an optional mount_path, which defaults to /mnt/repos/<repo>. For every argument and its rules, such as the URLs and mount paths that are accepted, see GitRepo in the SDK reference.
How mounting works¶
Snowflake clones each repository once per account, through the integration, the first time a sandbox asks for it. That first sandbox waits for the clone, which can take several minutes for a large repository. Later sandboxes in the account reuse the clone and start without that wait.
Snowflake refreshes each clone from GitHub every hour. Every new sandbox mounts the most recent refreshed copy, not the copy from when an earlier sandbox was created, so it can be up to about an hour behind the repository’s default branch. To get newer commits inside a running sandbox, see Credentials and access.
Each sandbox gets the repository’s default branch, with a writable layer on top. Your changes stay in your sandbox and don’t affect the shared clone or other sandboxes.
Credentials and access¶
Local Git operations, such as git checkout, git branch, git commit, and git diff, work in a mounted repository. Operations that contact GitHub, such as git fetch, git pull, and git push, fail unless you give the sandbox both of the following:
- Network access to
github.com, through an external access integration. By default, a sandbox can’t reach GitHub; see Isolation model. - A GitHub credential, such as a GitHub token stored in a Snowflake SECRET and brokered into the sandbox.
For how to set up both, see Give a sandbox secure access to an external API. Code in the sandbox can then use that credential for any request it makes to GitHub, so scope the token to the repositories and permissions the workload needs.
Mounting a repository doesn’t give the sandbox a GitHub credential or network access to GitHub. Snowflake clones and refreshes each repository outside the sandbox, using the integration, and serves the files to the sandbox from Snowflake storage, so reading a mounted repository never contacts GitHub from inside the sandbox. Code and agents in the sandbox can’t read a GitHub token, because none is placed there, and the sandbox’s egress configuration is unchanged.
Before a sandbox mounts a repository, Snowflake checks that your GitHub account has access to it, even when another user’s sandbox has already cloned it for the account. If your GitHub account can’t access the repository, you can’t mount it.
Persistence and getting your work out¶
When a sandbox is suspended, Snowflake saves the changes that you made to a mounted repository and restores them when the sandbox resumes, on top of the same commit. Committed and uncommitted changes are both kept, whether the sandbox was suspended automatically after being idle or by suspend(). For how suspension works, see Lifecycle.
Your changes are discarded in the following cases:
- You terminate the sandbox.
- The sandbox stays suspended for more than 30 days. Each new suspension starts a new 30-day period.
- You change the sandbox’s stage mounts while it’s suspended.
- Snowflake upgrades the sandbox’s image in a way that discards its saved state while it’s suspended. This is rare.
To keep your work, get it out of the sandbox before any of these happen. For example, produce a patch and read it from the program that created the sandbox:
If you’ve given the sandbox network access to GitHub and a credential with write access, you can instead commit and push a branch from inside the sandbox.
Limits¶
- A sandbox can mount up to five repositories.
- Mount paths can’t overlap each other or a stage mount.
- Only the default branch is mounted. You can’t yet mount a specific branch, tag, or commit.
Next steps¶
- Cortex Sandboxes concepts and security: how identity, egress, and suspension work in a sandbox.
- Cortex Sandboxes SDK reference:
Sandbox.createand its other options. - Give a sandbox secure access to an external API: set up GitHub network access and a credential so you can fetch, pull, and push.
- Reconnect to a running sandbox: keep a sandbox and its repository changes across sessions.