Skip to content

Authentication

Copybara needs authentication to read from origins and write to destinations.

MethodUse CaseSecurity
Personal Access Token (Classic)Quick setupMedium
Fine-Grained TokenScoped accessHigh
SSH KeyTraditional Git authMedium
Deploy KeySingle repo writeHigh
GitHub AppEnterprise automationHigh
  1. Go to GitHub SettingsDeveloper settingsPersonal access tokensTokens (classic)
  2. Click “Generate new token (classic)”
  3. Select scopes:
    • repo (full repository access)
    • workflow (if modifying workflows)
  4. Generate and copy the token

See GitHub’s token documentation for details.

More secure than classic tokens with repository-scoped access:

  1. Go to GitHub SettingsDeveloper settingsPersonal access tokensFine-grained tokens
  2. Click “Generate new token”
  3. Set expiration
  4. Select repository access: “Only select repositories”
  5. Choose the destination repository
  6. Set permissions:
    • Contents: Read and write
    • Pull requests: Read and write
    • Metadata: Read

Traditional Git authentication using SSH:

  1. Generate SSH key:

    Terminal window
    ssh-keygen -t ed25519 -C "copybara@example.com" -f ~/.ssh/copybara_key
  2. Add public key to GitHub:

    • SettingsSSH and GPG keysNew SSH key
    • Or repository SettingsDeploy keys (for single repo)

See GitHub’s SSH documentation for complete setup.

Use SSH URL in config:

destination = git.destination(
url = "git@github.com:org/repo.git",
push = "main",
)

SSH key scoped to a single repository:

  1. Generate SSH key:

    Terminal window
    ssh-keygen -t ed25519 -C "copybara-deploy" -f ~/.ssh/deploy_key
  2. Add to repository:

    • Repository SettingsDeploy keys
    • Add deploy key (paste the .pub file contents)
    • Enable “Allow write access”

Best for enterprise and multi-repo automation:

  1. Create GitHub App:

    • Organization SettingsDeveloper settingsGitHub Apps
    • New GitHub App
    • Set permissions:
      • Contents: Read and write
      • Pull requests: Read and write
      • Metadata: Read
    • Install on required repositories
  2. Note the App ID and generate a private key

See GitHub Apps documentation for complete setup.

For GitLab repositories, create a personal access token:

  1. Go to User SettingsAccess Tokens
  2. Create token with scopes: read_repository, write_repository
  3. Use oauth2 as username with your token as password

See GitLab token documentation.

For Gerrit destinations, generate an HTTP password:

  1. Go to Gerrit SettingsHTTP Credentials
  2. Generate new password
  3. Configure Git credential helper with your username and HTTP password
  1. Use fine-grained tokens when possible
  2. Limit scope to only required repositories
  3. Set expiration on tokens (30-90 days recommended)
  4. Rotate regularly (especially after team changes)
  5. Use CI/CD secrets - never commit credentials to repositories
  6. Audit access periodically via GitHub/GitLab audit logs
  7. Use GitHub Apps for production automation
  • Verify token hasn’t expired
  • Check token has required scopes (repo for full access)
  • Test with: git ls-remote <repository-url>
  • For fine-grained tokens, verify the repository is selected
  • Verify SSH key is added to ssh-agent: ssh-add -l
  • Test SSH connection: ssh -T git@github.com
  • Check key is added to GitHub/GitLab

Add the host to known hosts:

Terminal window
ssh-keyscan github.com >> ~/.ssh/known_hosts
ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
OperationRequired GitHub Scope
Read public repo(none)
Read private reporepo
Push to reporepo
Create pull requestrepo
Modify GitHub Actionsrepo, workflow