Home > Article > Backend Development > Pushing Python Packages to Artifact Registry Using Cloud Build
Google Artifact Registry is a powerful solution for managing and hosting Python package artifacts in a private, secure, and scalable way. This guide provides a step-by-step walkthrough to push Python package .whl files to the Artifact Registry using Google Cloud Build and a secret (creds) from Google Secret Manager for authentication.
Artifact Registry Setup:
gcloud artifacts repositories create python-packages \ --repository-format=python \ --location=us-central1 \ --description="Python packages repository"
Secret Setup:
gcloud secrets create creds --data-file=path/to/key.json
Grant Cloud Build access to the secret:(Optional, can also be done using IAM)
gcloud secrets add-iam-policy-binding creds \ --member="serviceAccount:$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')@cloudbuild.gserviceaccount.com" \ --role="roles/secretmanager.secretAccessor"
Here's the full working cloudbuild.yaml file:
options: machineType: E2_HIGHCPU_8 substitutionOption: ALLOW_LOOSE logging: CLOUD_LOGGING_ONLY steps: # Step 1: Access the secret `creds` and save it as `key.json` - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' entrypoint: bash args: - '-c' - | gcloud secrets versions access latest --secret=creds > /workspace/key.json # Step 2: Configure `.pypirc` with the Artifact Registry credentials - name: 'python' entrypoint: bash args: - '-c' - | cat > ~/.pypirc << EOL [distutils] index-servers = tower-common-repo [tower-common-repo] repository: https://us-central1-python.pkg.dev/$PROJECT_ID/python-packages/ username: _json_key_base64 password: $(base64 -w0 /workspace/key.json) EOL # Step 3: Build and upload the Python package pip install twine build && \ python -m build && \ twine upload --repository tower-common-repo dist/* --verbose
Define Build Options:
Retrieve key.json Secret:
Configure .pypirc:
Build and Push Package:
Save the cloudbuild.yaml file and trigger the build or can connect to github repository:
gcloud artifacts repositories create python-packages \ --repository-format=python \ --location=us-central1 \ --description="Python packages repository"
After the build completes:
gcloud secrets create creds --data-file=path/to/key.json
The above is the detailed content of Pushing Python Packages to Artifact Registry Using Cloud Build. For more information, please follow other related articles on the PHP Chinese website!