Python packages via GCP’s Artifact Registry
Publish and install
When you start to build company-wide Python code bases or enable teams to share code using best practices, you will most certainly need a private Python repository. There are several products out there, but I recently looked into GCP’s Artifact Registry. The setup is a bit different than what I experienced before and the information I have found online is a bit verbose and dispersed (especially for poetry
). In this post, I’ll show you how to publish and install packages from GCP’s Artifact Factory using pip
and poetry
.
Prerequisites
- Verify that the user account or service account you are using has the required permissions to access the repository. Below is a rule of thumb on how to apply permissions via GCP’s roles:
- roles/artifactregistry.reader
: for all developers regardless of their proficiency level in software engineering.
- roles/artifactregistry.writer
: for a selected group of users with mature software engineering experience, for example, technical leads of the different teams. Also for service accounts used to automate the package deployment via CI/CD.
- roles/artifactregistry.repoAdmin
: for a selected group of users with mature software engineering experience who are part of a centralized team responsible for maintaining the GCP Artifact Registry.
- Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
Publish using Poetry
Update your poetry version, and install keyring, and the GCP Artifact Registry backend in the core poetry virtual environment.
poetry self update && poetry self add keyrings.google-artifactregistry-auth
Add the URL endpoint for your Python repository in Poetry’s configuration
poetry config repositories.gcp GCP-PYTHON-REPO-URL
Build and publish your package
poetry publish --build --repository gcp
Install using poetry
Update your poetry version, and install keyring, and the GCP Artifact Registry backend in the core poetry virtual environment.
poetry self update && poetry self add keyrings.google-artifactregistry-auth
Configure the package source as an explicit package source for your project.
poetry source add --priority=explicit gcp GCP-PYTHON-REPO-URL-SIMPLE
Add the Python package.
poetry add --source gcp PACKAGE-NAME
Install using pip
Install keyring and the GCP Artifact Registry backend.
pip install keyring && pip install keyrings.google-artifactregistry-auth
Install the Python package.
pip install PACKAGE-NAME --index-url GCP-PYTHON-REPO-URL-SIMPLE