אחסון חבילות Node.js ב-Artifact Registry

במדריך למתחילים הזה נסביר איך להגדיר מאגר חבילות פרטי של Node.js ב-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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Artifact Registry API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  6. Install the Google Cloud CLI.

  7. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. 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

  10. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  12. Enable the Artifact Registry API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  13. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    gcloud init
  16. אם רוצים להתחבר ל-Package Repository של Node.js מ-Windows, צריך להתקין את PowerShell.

התפקידים הנדרשים

כדי לקבל את ההרשאות שנדרשות ליצירה ולניהול של מאגרי חבילות Node.js ב-Artifact Registry, צריך לבקש מהאדמין להקצות לכם את התפקיד אדמין של Artifact Registry (roles/artifactregistry.admin) ב-IAM בפרויקט. כדי לקרוא הסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

הפעלת Cloud Shell

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

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

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

  1. נכנסים למסוף Google Cloud .

    Google Cloud console

  2. בסרגל הכלים של המסוף Google Cloud , לוחצים על Activate Cloud Shell:

בחלק התחתון של המסוף ייפתח סשן של Cloud Shell בתוך מסגרת. משתמשים בשורת הפקודה הזו כדי להריץ פקודות gcloud.

יצירת מאגר חבילות Node.js

יוצרים את המאגר של פריטי המידע שנוצרו בתהליך הפיתוח (Artifact) ב-Node.js.

  1. יוצרים את המאגר.

    המסוף

    1. פותחים את הדף Repositories במסוף Google Cloud .

      פתיחת הדף Repositories

    2. לוחצים על Create Repository.

    3. מציינים את quickstart-nodejs-repo כשם המאגר.

    4. בוחרים בפורמט npm ובמצב רגיל.

    5. בקטע Location Type, בוחרים באפשרות Region ואז בוחרים את המיקום us-central1.

    6. לוחצים על יצירה.

    המאגר נוסף לרשימת המאגרים.

    gcloud

    1. מריצים את הפקודה הבאה כדי ליצור מאגר חבילות חדש של Node.js בשם quickstart-nodejs-repo במיקום us-central1 עם התיאור Node.js repository.

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

      gcloud artifacts repositories list
      

    כדי לקבל מידע נוסף על פקודות Artifact Registry, מריצים את הפקודה gcloud artifacts.

  2. כדי לפשט את הפקודות של gcloud, מגדירים את מאגר ברירת המחדל ל-quickstart-nodejs-repo ואת מיקום ברירת המחדל ל-us-central1. אחרי שמגדירים את הערכים, אין צורך לציין אותם בפקודות gcloudשדורשות מאגר או מיקום.

    כדי להגדיר את המאגר, מריצים את הפקודה:

    gcloud config set artifacts/repository quickstart-nodejs-repo
    

    כדי להגדיר את המיקום, מריצים את הפקודה:

    gcloud config set artifacts/location us-central1
    

    מידע נוסף על הפקודות האלה זמין במאמר בנושא gcloud config set.

הגדרת npm

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

  1. מורידים את חבילת emoji-regex, חבילה שמספקת ביטוי רגולרי להתאמה של כל סמלי האימוג'י ורצפים שלהם.

    npm pack emoji-regex
    

    הפקודה מורידה ארכיון של החבילה.

  2. מחולצים את הארכיון לספרייה emoji-regex. הפקודה הבאה מיועדת לארכיון של emoji-regex בגרסה 10.1.0:

    mkdir emoji-regex && tar xvf emoji-regex-10.1.0.tgz -C emoji-regex --strip-components 1
    
  3. יוצרים חבילת npm שנייה שבה תשתמשו כדי להתקין את החבילה emoji-regex מ-Artifact Registry. כדי ליצור חבילה בסיסית, משתמשים בפקודה npm init.

    mkdir npm-package2
    cd npm-package2
    
    npm init -y
    

    כשמוצגת בקשה, מאשרים את ערכי ברירת המחדל.

  4. מגדירים את פרויקטים emoji-regex ו-npm-package2 כך שיאומתו באמצעות מאגר Artifact Registry שיצרתם.

    1. מריצים את הפקודה הבאה. הפקודה מחזירה את הגדרות התצורה שצריך להוסיף לקובץ התצורה של npm.

      gcloud artifacts print-settings npm --scope=@quickstart
      

      ההיקף של quickstart משויך למאגר. כשכוללים את היקף ההרשאה בפקודות לפרסום או להתקנה של חבילות, npm משתמש במאגר שלכם. כשמפרסמים או מתקינים חבילות בלי היקף, נעשה שימוש במאגר ברירת המחדל שהגדרתם. מידע נוסף מופיע בסקירה הכללית בנושא Node.js.

      הפלט של הפקודה gcloud אמור להיראות כמו בדוגמה הבאה. ‫PROJECT הוא Google Cloud מזהה הפרויקט.

      @quickstart:registry=https://us-central1-npm.pkg.dev/PROJECT/quickstart-nodejs-repo/
      //us-central1-npm.pkg.dev/PROJECT/quickstart-nodejs-repo/:always-auth=true
      
    2. מוסיפים את הגדרות התצורה מהשלב הקודם לקובץ .npmrc בכל פרויקט npm. הקובץ באותה ספרייה כמו קובץ package.json.

      למידע נוסף על הקובץ .npmrc, קראו את הסקירה הכללית של Node.js.

      כל Package Repository של חבילות Node.js ב-Artifact Registry משויך לנקודת קצה של מאגר npm https://LOCATION-npm.pkg.dev/PROJECT/REPOSITORY. אם יוצרים עוד מאגר חבילות Node.js ב-Artifact Registry, ‏ npm מתקשר איתו כמאגר נפרד עם היקף משלו.

  5. עורכים את package.json בפרויקטים emoji-regex ו-npm-package2.

    1. מוודאים שהערך של name כולל את ההיקף quickstart.

      • עבור emoji-regex:
      "name": "@quickstart/emoji-regex"
      
      • עבור npm-package2:
      "name": "@quickstart/npm-package2"
      
    2. בקטע scripts, מוסיפים סקריפט ל-google-artifactregistry-auth, ספריית לקוח שמעדכנת את פרטי הכניסה למאגרי Artifact Registry.

      "scripts": {
        "artifactregistry-login": "npx google-artifactregistry-auth --repo-config=./.npmrc --credential-config=./.npmrc"
      }
      

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

  1. מרעננים את טוקן הגישה כדי להתחבר למאגר.

    npm run artifactregistry-login
    
  2. מוסיפים את החבילה למאגר. אפשר להשתמש בפקודה npm או yarn.

    npm publish
    
    yarn publish
    

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

כדי לוודא שהחבילה נוספה למאגר:

המסוף

  1. פותחים את הדף Repositories במסוף Google Cloud .

    פתיחת הדף Repositories

  2. ברשימת המאגרים, לוחצים על המאגר quickstart-nodejs-repo.

    בדף Packages מופיעות החבילות במאגר.

  3. לוחצים על חבילה כדי לראות את הגרסאות שלה.

gcloud

כדי להציג את רשימת קובצי האימג' במאגר ברירת המחדל של quickstart-nodejs-repo, מריצים את הפקודה הבאה:

 gcloud artifacts packages list

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

gcloud artifacts versions list --package=PACKAGE

כאשר PACKAGE הוא מזהה החבילה.

התקנת חבילות

מתקינים את החבילה emoji-regex ממאגר Artifact Registry ב-npm-package2.

בספרייה npm-package2, מריצים את הפקודה npm install או yarn add:

npm install @quickstart/emoji-regex
yarn add @quickstart/emoji-regex

הפקודה מתקינה את החבילה emoji-regex בפרויקט npm-package2. הוא גם מעדכן את package.json כדי להגדיר את emoji-regex כתלות.

"dependencies": {
  "@quickstart/emoji-regex": "^10.1.0"
}

הסרת המשאבים

כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה:

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

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

המסוף

  1. פותחים את הדף Repositories במסוף Google Cloud .

    פתיחת הדף Repositories

  2. ברשימת המאגרים, בוחרים במאגר quickstart-nodejs-repo.

  3. לוחצים על Delete.

gcloud

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

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

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

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