Google ArtifactRegistry 是一个强大的解决方案,用于以私密、安全且可扩展的方式管理和托管 Python 包工件。本指南提供了使用 Google Cloud Build 和来自 Google Secret Manager 的密钥(信用)将 Python 包 .whl 文件推送到 Artifact Registry 的分步演练,以进行身份验证。
工件注册表设置:
gcloud artifacts repositories create python-packages \ --repository-format=python \ --location=us-central1 \ --description="Python packages repository"
秘密设置:
gcloud secrets create creds --data-file=path/to/key.json
授予 Cloud Build 对密钥的访问权限:(可选,也可以使用 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"
这是完整的工作 cloudbuild.yaml 文件:
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
定义构建选项:
检索 key.json 秘密:
配置.pypirc:
构建并推送包:
保存cloudbuild.yaml文件并触发构建或者可以连接到github存储库:
gcloud artifacts repositories create python-packages \ --repository-format=python \ --location=us-central1 \ --description="Python packages repository"
构建完成后:
gcloud secrets create creds --data-file=path/to/key.json
以上是使用 Cloud Build 将 Python 包推送到 ArtifactRegistry的详细内容。更多信息请关注PHP中文网其他相关文章!