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中文網其他相關文章!