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 Settings → Developer settings → Personal access tokens → Tokens (classic)
  2. Click “Generate new token (classic)”
  3. Select scopes:
    • repo (full repository access)
    • workflow (if modifying workflows)
  4. Generate and copy the token

More secure than classic tokens:

  1. Go to GitHub Settings → Developer settings → Personal access tokens → Fine-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:

  1. Generate SSH key:

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

    • Settings → SSH and GPG keys → New SSH key
    • Or repository Settings → Deploy keys (for single repo)

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 copybara_deploy
  2. Add to repository:

    • Repository Settings → Deploy keys
    • Add deploy key
    • Enable “Allow write access”

Best for enterprise and multi-repo automation:

  1. Create GitHub App:

    • Organization Settings → Developer settings → GitHub 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

For Gerrit destinations:

Terminal window
# HTTP password
git config --global credential.helper store
echo "https://username:http-password@gerrit.example.com" > ~/.git-credentials

Generate HTTP password in Gerrit: Settings → HTTP Credentials

For workflows accessing multiple repositories:

# Use a PAT with access to all repos
- name: Configure Git
run: |
echo "https://x-access-token:${{ secrets.MULTI_REPO_PAT }}@github.com" > ~/.git-credentials

Or use separate credentials:

- name: Configure credentials
run: |
cat > ~/.git-credentials << 'EOF'
https://x-access-token:${{ secrets.ORIGIN_TOKEN }}@github.com
https://x-access-token:${{ secrets.DEST_TOKEN }}@github.com
EOF
  1. Use fine-grained tokens when possible
  2. Limit scope to only required repositories
  3. Set expiration on tokens
  4. Rotate regularly (especially after team changes)
  5. Use secrets in CI/CD (never hardcode)
  6. Audit access periodically
Terminal window
# Test credentials
git ls-remote https://github.com/org/repo
# Check configured credentials
cat ~/.git-credentials
  • Verify token has required scopes
  • Check repository access settings
  • For fine-grained tokens, verify repository is selected
Terminal window
ssh-keyscan github.com >> ~/.ssh/known_hosts