אחסון חבילות Python ב-Artifact Registry

במדריך למתחילים הזה נסביר איך להגדיר מאגר Python פרטי ב-Artifact Registry, להעלות חבילה ואז להתקין את החבילה.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  6. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.

הפעלת Cloud Shell

במדריך למתחילים הזה תשתמשו ב-Cloud Shell, שהיא סביבת מעטפת לניהול משאבים שמתארחים ב-Google Cloud.

ב-Cloud Shell מותקנים מראש Google Cloud CLI ו-Python. ה-CLI של gcloud הוא ממשק שורת הפקודה העיקרי ל- Google Cloud.

מפעילים את Cloud Shell:

  1. עוברים אל Google Cloud המסוף.

    Google Cloud console

  2. בסרגל הכלים של המסוף, לוחצים על 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.

יצירת מאגר

יוצרים את המאגר של החבילות.

  1. מריצים את הפקודה הבאה כדי ליצור מאגר חדש של חבילות Python בפרויקט הנוכחי בשם quickstart-python-repo במיקום us-central1.

    gcloud artifacts repositories create quickstart-python-repo \
        --repository-format=python \
        --location=us-central1 \
        --description="Python package repository"
    
  2. מריצים את הפקודה הבאה כדי לוודא שהמאגר נוצר:

    gcloud artifacts repositories list
    
  3. כדי לפשט את הפקודות של 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 כדי לפשט את המדריך למתחילים הזה, תורידו קבצי חבילה מוכנים מראש.

  1. יוצרים תיקיית פרויקט ב-Python בשם python-quickstart.

    mkdir python-quickstart
    
  2. יוצרים ספריית משנה בשם dist ועוברים לספרייה.

    mkdir python-quickstart/dist
    cd python-quickstart/dist
    
  3. מורידים את חבילות Python לדוגמה שבהן נעשה שימוש במדריך למשתמש בנושא Python Packaging‏ Packaging Python Projects.

    pip download sampleproject
    

    הפקודה מורידה את החבילה sampleproject ואת התלות שלה, peppercorn.

העלאת החבילה למאגר

משתמשים ב-Twine כדי להעלות את החבילות למאגר.

  1. מהספרייה dist, עוברים לספריית האב python-quickstart.

    cd ..
    
  2. מעלים את החבילות למאגר מהספרייה 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 על המשאבים שבהם השתמשתם בדף הזה, פועלים לפי השלבים הבאים:

לפני שמסירים את המאגר, צריך לוודא שכל החבילות שרוצים לשמור זמינות במיקום אחר.

כדי למחוק את המאגר:

  1. כדי למחוק את מאגר quickstart-python-repo, מריצים את הפקודה הבאה:

    gcloud artifacts repositories delete quickstart-python-repo
    
  2. אם רוצים להסיר את הגדרות ברירת המחדל של המאגר והמיקום שהגדרתם עבור התצורה הפעילה של gcloud, מריצים את הפקודות הבאות:

    gcloud config unset artifacts/repository
    gcloud config unset artifacts/location
    

המאמרים הבאים