Source Repositories¶
Source Repositories is a service that provides Git version control to support collaborative development of any application or service. The SourceRepository
class in gcp-pilot provides a high-level interface for interacting with Google Cloud Source Repositories.
Installation¶
To use the Source Repositories functionality, you need to install gcp-pilot:
Usage¶
Initialization¶
from gcp_pilot.source import SourceRepository
source_repo = SourceRepository() # (1)!
source_repo = SourceRepository(project_id="my-project") # (2)!
source_repo = SourceRepository(impersonate_account="service-account@project-id.iam.gserviceaccount.com") # (3)!
- Initialize with default credentials
- Initialize with specific project
- Initialize with service account impersonation
Listing Repositories¶
repos = source_repo.list_repos( # (1)!
project_id="my-project", # (2)!
)
for repo in repos:
print(f"Repository: {repo['name']}")
print(f"URL: {repo['url']}")
print(f"Size: {repo.get('size', 'Unknown')}")
- List all repositories in a project
- Optional: defaults to the project associated with credentials
Getting a Repository¶
repo = source_repo.get_repo( # (1)!
name="my-repo",
project_id="my-project", # (2)!
)
print(f"Repository: {repo['name']}")
print(f"URL: {repo['url']}")
- Get information about a specific repository
- Optional: defaults to the project associated with credentials
Creating a Repository¶
repo = source_repo.create_repo( # (1)!
name="my-new-repo",
project_id="my-project", # (2)!
exists_ok=True, # (3)!
)
print(f"Repository created: {repo['name']}")
- Create a new repository
- Optional: defaults to the project associated with credentials
- Optional: if True, returns the existing repository if it already exists
Deleting a Repository¶
source_repo.delete_repo( # (1)!
name="my-repo-to-delete",
project_id="my-project", # (2)!
)
- Delete a repository
- Optional: defaults to the project associated with credentials
Common Git Commands with Source Repositories¶
# Clone a repository (1)!
git clone https://source.developers.google.com/p/my-project/r/my-repo
# Add a remote for Source Repositories (2)!
git remote add google https://source.developers.google.com/p/my-project/r/my-repo
# Push to Source Repositories (3)!
git push google master
# Pull from Source Repositories (4)!
git pull google master
- Clone a repository
- Add a remote for Source Repositories
- Push to Source Repositories
- Pull from Source Repositories
Authenticate for Source Repositories CLI Access¶
# Authenticate using gcloud (1)!
gcloud auth login
gcloud auth application-default login
# Configure Git to use the gcloud credential helper (2)!
git config --global credential.helper gcloud.sh
- Authenticate using gcloud
- Configure Git to use the gcloud credential helper
Working with Service Account Impersonation¶
Service account impersonation allows you to act as a service account without having its key file. This is a more secure approach than downloading and storing service account keys.
source_repo = SourceRepository(impersonate_account="service-account@project-id.iam.gserviceaccount.com") # (1)!
repos = source_repo.list_repos() # (2)!
- Initialize with service account impersonation
- Now all operations will be performed as the impersonated service account
For more information on service account impersonation, see the Authentication documentation.
Integration with Cloud Build
Source Repositories can be integrated with Cloud Build to automatically build and deploy your code when changes are pushed to the repository. For more information, see the Cloud Build documentation.
Best Practices for Source Repositories
Here are some best practices for working with Source Repositories:
- Use descriptive repository names: Choose repository names that clearly indicate their purpose and content.
- Set up branch protection: Configure branch protection rules to prevent direct pushes to important branches.
- Use meaningful commit messages: Write clear and descriptive commit messages to make it easier to understand changes.
- Implement a branching strategy: Use a consistent branching strategy like Git Flow or GitHub Flow.
- Regularly clean up old branches: Delete branches that are no longer needed to keep the repository clean.
- Set up code reviews: Use pull requests and code reviews to maintain code quality.
- Integrate with CI/CD: Set up continuous integration and continuous deployment pipelines to automate testing and deployment.