אחסון חבילות Python ב-Artifact Registry
במדריך למתחילים הזה נסביר איך להגדיר מאגר Python פרטי ב-Artifact Registry, להעלות חבילה ואז להתקין את החבילה.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Make sure that you have the following role or roles on the project: Artifact Registry Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Make sure that you have the following role or roles on the project: Artifact Registry Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
הפעלת Cloud Shell
במדריך למתחילים הזה תשתמשו ב-Cloud Shell, שהיא סביבת מעטפת לניהול משאבים שמתארחים ב-Google Cloud.
ב-Cloud Shell מותקנים מראש Google Cloud CLI ו-Python. ה-CLI של gcloud הוא ממשק שורת הפקודה העיקרי ל- Google Cloud.
מפעילים את Cloud Shell:
עוברים אל Google Cloud המסוף.
בסרגל הכלים של המסוף, לוחצים על Activate Cloud Shell (הפעלת Cloud Shell):
Google Cloud
בחלק התחתון של המסוף ייפתח סשן של Cloud Shell בתוך מסגרת.
משתמשים בשורת הפקודות הזו כדי להריץ פקודות gcloud.
התקנת החבילות הנדרשות
Twine הוא כלי לפרסום חבילות Python. תשתמשו ב-Twine כדי להעלות חבילה ל-Artifact Registry.
במדריך למתחילים הזה משתמשים בהתקנת Python שכלולה ב-Cloud Shell. ההתקנה הזו כוללת את הקצה העורפי של מחזיק המפתחות של Artifact Registry כדי לטפל באימות ב-Artifact Registry. אם יוצרים סביבה וירטואלית או מגדירים Python מחוץ ל-Cloud Shell, צריך להתקין את הקצה העורפי של keyring לאימות. פרטים נוספים זמינים במאמר אימות באמצעות keyring.
כדי להתקין את Twine, מריצים את הפקודה:
pip install twine
עכשיו אפשר להגדיר את Artifact Registry.
יצירת מאגר
יוצרים את המאגר של החבילות.
מריצים את הפקודה הבאה כדי ליצור מאגר חדש של חבילות Python בפרויקט הנוכחי בשם
quickstart-python-repoבמיקוםus-central1.gcloud artifacts repositories create quickstart-python-repo \ --repository-format=python \ --location=us-central1 \ --description="Python package repository"מריצים את הפקודה הבאה כדי לוודא שהמאגר נוצר:
gcloud artifacts repositories listכדי לפשט את הפקודות של
gcloud, מגדירים את מאגר ברירת המחדל ל-quickstart-python-repoואת מיקום ברירת המחדל ל-us-central1. אחרי שמגדירים את הערכים, לא צריך לציין אותם בפקודותgcloudשדורשות מאגר או מיקום.כדי להגדיר את המאגר, מריצים את הפקודה:
gcloud config set artifacts/repository quickstart-python-repoכדי להגדיר את המיקום, מריצים את הפקודה:
gcloud config set artifacts/location us-central1מידע נוסף על הפקודות האלה זמין במאמר בנושא gcloud config set.
הגדרת אימות
הקצה העורפי של מחזיק המפתחות של Artifact Registry מוצא את פרטי הכניסה שלכם באמצעות Application Default Credentials (ADC), אסטרטגיה שמחפשת פרטי כניסה בסביבה שלכם.
במדריך למתחילים הזה תלמדו:
- יצירת פרטי כניסה של משתמש ל-ADC. בסביבת ייצור, מומלץ להשתמש בחשבון שירות ולספק פרטי כניסה באמצעות משתנה הסביבה
GOOGLE_APPLICATION_CREDENTIALS. - כוללים את כתובת ה-URL של מאגר Artifact Registry בפקודות
pipו-twineכדי שלא תצטרכו להגדיר את pip ואת Twine עם כתובת ה-URL של המאגר.
כדי ליצור פרטי כניסה ל-ADC, מריצים את הפקודה הבאה:
gcloud auth application-default login
פרטים על שיטות אימות ועל הוספת מאגרי מידע להגדרות של pip ו-Twine זמינים במאמר הגדרת אימות למאגרי חבילות Python.
קבלת חבילה לדוגמה
כשיוצרים פרויקט Python, קובצי ההפצה נשמרים בספריית משנה בפרויקט Python.dist כדי לפשט את המדריך למתחילים הזה, תורידו קבצי חבילה מוכנים מראש.
יוצרים תיקיית פרויקט ב-Python בשם
python-quickstart.mkdir python-quickstartיוצרים ספריית משנה בשם
distועוברים לספרייה.mkdir python-quickstart/dist cd python-quickstart/distמורידים את חבילות Python לדוגמה שבהן נעשה שימוש במדריך למשתמש בנושא Python Packaging Packaging Python Projects.
pip download sampleprojectהפקודה מורידה את החבילה
sampleprojectואת התלות שלה,peppercorn.
העלאת החבילה למאגר
משתמשים ב-Twine כדי להעלות את החבילות למאגר.
מהספרייה
dist, עוברים לספריית האבpython-quickstart.cd ..מעלים את החבילות למאגר מהספרייה
dist.python3 -m twine upload --repository-url https://us-central1-python.pkg.dev/PROJECT_ID/quickstart-python-repo/ dist/*כשמריצים את הפקודה עם
python3 -m, Python מאתר את twine ומריץ את הפקודה. אם הפקודהtwineנמצאת בנתיב המערכת, אפשר להריץ אותה בליpython3 -m.
מערכת Twine מעלה את הקבצים sampleproject ו-peppercorn למאגר שלכם.
הצגת החבילה במאגר
כדי לוודא שהחבילה נוספה, מציגים את רשימת החבילות במאגר quickstart-python-repo.
מריצים את הפקודה הבאה:
gcloud artifacts packages list --repository=quickstart-python-repo
כדי לראות את הגרסאות של חבילה, מריצים את הפקודה הבאה:
gcloud artifacts versions list --package=PACKAGE
כאשר PACKAGE הוא מזהה החבילה.
התקנת החבילה
כדי להתקין את החבילה, מריצים את הפקודה הבאה:
pip install --index-url https://us-central1-python.pkg.dev/PROJECT_ID/quickstart-python-repo/simple/ sampleproject
פתרון בעיות
כברירת מחדל, כלים כמו pip ו-Twine לא מחזירים הודעות שגיאה מפורטות.
אם נתקלים בשגיאה, מריצים מחדש את הפקודה עם הדגל --verbose כדי לקבל פלט מפורט יותר. מידע נוסף זמין במאמר בנושא פתרון בעיות בחבילות Python.
הסרת המשאבים
כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, פועלים לפי השלבים הבאים:
לפני שמסירים את המאגר, צריך לוודא שכל החבילות שרוצים לשמור זמינות במיקום אחר.
כדי למחוק את המאגר:
כדי למחוק את מאגר
quickstart-python-repo, מריצים את הפקודה הבאה:gcloud artifacts repositories delete quickstart-python-repoאם רוצים להסיר את הגדרות ברירת המחדל של המאגר והמיקום שהגדרתם עבור התצורה הפעילה של
gcloud, מריצים את הפקודות הבאות:gcloud config unset artifacts/repository gcloud config unset artifacts/location
המאמרים הבאים
- מידע נוסף על הגדרת אימות
- מידע נוסף על ניהול מאגרי מידע
- מידע על ניהול חבילות
- אפשר גם לקרוא את המשאבים שלנו בנושא DevOps ולעיין בתוכנית המחקר שלנו.