← Sandbox SDK / sandbox / guides
Práce s Gitem
Tato příručka ukazuje, jak v sandboxu klonovat repozitáře, spravovat větve a automatizovat operace Gitu.
Klonujte repozitáře
import { getSandbox } from "@cloudflare/sandbox";
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
// Basic clone
await sandbox.gitCheckout("https://github.com/user/repo");
// Clone specific branch
await sandbox.gitCheckout("https://github.com/user/repo", {
branch: "develop",
});
// Shallow clone (faster for large repos)
await sandbox.gitCheckout("https://github.com/user/large-repo", {
depth: 1,
});
// Clone to specific directory
await sandbox.gitCheckout("https://github.com/user/my-app", {
targetDir: "/workspace/project",
});import { getSandbox } from '@cloudflare/sandbox';
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
// Basic clone
await sandbox.gitCheckout('https://github.com/user/repo');
// Clone specific branch
await sandbox.gitCheckout('https://github.com/user/repo', {
branch: 'develop'
});
// Shallow clone (faster for large repos)
await sandbox.gitCheckout('https://github.com/user/large-repo', {
depth: 1
});
// Clone to specific directory
await sandbox.gitCheckout('https://github.com/user/my-app', {
targetDir: '/workspace/project'
});Klonujte soukromé repozitáře
V URL použijte osobní přístupový token:
const token = env.GITHUB_TOKEN;
const repoUrl = `https://${token}@github.com/user/private-repo.git`;
await sandbox.gitCheckout(repoUrl);const token = env.GITHUB_TOKEN;
const repoUrl = `https://${token}@github.com/user/private-repo.git`;
await sandbox.gitCheckout(repoUrl);Naklonování a sestavení
Naklonujte repozitář a spusťte kroky sestavení:
await sandbox.gitCheckout("https://github.com/user/my-app");
const repoName = "my-app";
// Install and build
await sandbox.exec(`cd ${repoName} && npm install`);
await sandbox.exec(`cd ${repoName} && npm run build`);
console.log("Build complete");await sandbox.gitCheckout('https://github.com/user/my-app');
const repoName = 'my-app';
// Install and build
await sandbox.exec(`cd ${repoName} && npm install`);
await sandbox.exec(`cd ${repoName} && npm run build`);
console.log('Build complete');Práce s větvemi
await sandbox.gitCheckout("https://github.com/user/repo");
// Switch branches
await sandbox.exec("cd repo && git checkout feature-branch");
// Create new branch
await sandbox.exec("cd repo && git checkout -b new-feature");await sandbox.gitCheckout('https://github.com/user/repo');
// Switch branches
await sandbox.exec('cd repo && git checkout feature-branch');
// Create new branch
await sandbox.exec('cd repo && git checkout -b new-feature');Proveďte úpravy a commitněte je
await sandbox.gitCheckout("https://github.com/user/repo");
// Modify a file
const readme = await sandbox.readFile("/workspace/repo/README.md");
await sandbox.writeFile(
"/workspace/repo/README.md",
readme.content + "\n\n## New Section",
);
// Commit changes
await sandbox.exec('cd repo && git config user.name "Sandbox Bot"');
await sandbox.exec('cd repo && git config user.email "[email protected]"');
await sandbox.exec("cd repo && git add README.md");
await sandbox.exec('cd repo && git commit -m "Update README"');await sandbox.gitCheckout('https://github.com/user/repo');
// Modify a file
const readme = await sandbox.readFile('/workspace/repo/README.md');
await sandbox.writeFile('/workspace/repo/README.md', readme.content + '\n\n## New Section');
// Commit changes
await sandbox.exec('cd repo && git config user.name "Sandbox Bot"');
await sandbox.exec('cd repo && git config user.email "[email protected]"');
await sandbox.exec('cd repo && git add README.md');
await sandbox.exec('cd repo && git commit -m "Update README"');Osvědčené postupy
- Používejte mělké klony - Rychlejší u velkých repozitářů s
depth: 1 - Bezpečné uložení přihlašovacích údajů - Pro tokeny používejte proměnné prostředí
- Úklid - Nepoužívané repozitáře mažte, abyste ušetřili místo
Řešení potíží
Ověřování selže
Ověřte, že je token nastaven:
if (!env.GITHUB_TOKEN) {
throw new Error("GITHUB_TOKEN not configured");
}
const repoUrl = `https://${env.GITHUB_TOKEN}@github.com/user/private-repo.git`;
await sandbox.gitCheckout(repoUrl);if (!env.GITHUB_TOKEN) {
throw new Error('GITHUB_TOKEN not configured');
}
const repoUrl = `https://${env.GITHUB_TOKEN}@github.com/user/private-repo.git`;
await sandbox.gitCheckout(repoUrl);Časový limit pro velký repozitář
Použijte mělký klon:
await sandbox.gitCheckout("https://github.com/user/large-repo", {
depth: 1,
});await sandbox.gitCheckout('https://github.com/user/large-repo', {
depth: 1
});Související zdroje
- Referenční dokumentace API souborů - Operace se soubory po klonování
- Průvodce spouštěním příkazů - Spouštění příkazů git
- Průvodce správou souborů - Práce s klonovanými soubory