ניהול חבילות Node.js

בדף הזה מפורטות המשימות הבאות:

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

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

  1. אם מאגר היעד לא קיים, צריך ליצור מאגר חדש.
  2. מוודאים שיש לכם את ההרשאות הנדרשות למאגר.
  3. הגדרת אימות ל-npm
  4. (אופציונלי) הגדרת ברירות מחדל לפקודות gcloud.
  5. אם אתם משתמשים ב-npm credential helper לאימות, צריך לקבל אסימון גישה לפני שמתחברים למאגר באמצעות npm.

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

כדי לקבל את ההרשאות שדרושות לניהול חבילות, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים במאגר:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

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

קבלת טוקן גישה

אסימוני הגישה תקפים למשך 60 דקות. יוצרים אסימון גישה זמן קצר לפני שמריצים פקודות שיוצרות אינטראקציה עם מאגרי מידע.

כדי לקבל טוקן, אפשר להשתמש באחת מהאפשרויות הבאות:

  • משתמשים בפקודה npx כדי לרענן את אסימון הגישה.

    1. מוודאים שפרטי הכניסה להתחברות למאגר npm הציבורי נמצאים בקובץ התצורה של npm של המשתמש, ~/.npmrc.

    2. מריצים את הפקודה הבאה בספריית הפרויקט של Node.js.

      npx google-artifactregistry-auth
      

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

      npm_config_registry=https://registry.npmjs.org npx google-artifactregistry-auth
      
  • מוסיפים סקריפט לקובץ package.json בפרויקט.

    "scripts": {
     "artifactregistry-login": "npx google-artifactregistry-auth"
    }
    

    מריצים את הסקריפט בספריית הפרויקט של Node.js.

    npm run artifactregistry-login
    

‫Artifact Registry קורא את הגדרות המאגר של Artifact Registry בקובץ .npmrc של הפרויקט ומשתמש בהן כדי להוסיף פרטי כניסה של אסימון לקובץ .npmrc של המשתמש. אחסון האסימון בקובץ .npmrc של המשתמש מבודד את פרטי הכניסה מקוד המקור וממערכת בקרת המקור.

הוספת חבילות

מצבי מאגר: רגיל

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

  • החלפת גרסת חבילה על ידי פרסום שלה מחדש במאגר
  • להסיר חבילה או את הגרסה שלה מהמאגר, ואז לפרסם חבילה עם אותו שם ומספר גרסה

אם לא מציינים תג כשמפרסמים חבילה, npm מוסיף את התג latest. כדי לפשט את ההתקנה של החבילות בשלב פיתוח ספציפי, כדאי לפרסם את החבילות עם תג, כמו beta או dev.

ב-Artifact Registry, שמות החבילות ב-npm צריכים להיות אלפאנומריים ובאותיות קטנות.

כדי להוסיף חבילה:

  1. מוודאים ששם החבילה ב-package.json כולל את ההיקף שהוגדר למאגר. בדוגמה הבאה מוצגת חבילה עם ההיקף dev-repo.

    "name": "@dev-repo/my-package"
    
  2. אם אתם משתמשים בכלי העזר לפרטי הכניסה כדי לבצע אימות באמצעות אסימון גישה, עליכם לקבל אסימון חדש.

  3. מוסיפים חבילות למאגר. אפשר להשתמש בפקודה npm או yarn.

    כדי לתייג את החבילה, כוללים את הדגל --tag ומחליפים את TAG בתג שרוצים להשתמש בו. אם לא כוללים את הדגל --tag, npm מגדיר את התג באופן אוטומטי ל-latest.

    npm publish --tag=TAG
    
    yarn publish --tag TAG
    

צפייה בחבילות ובגרסאות

מצבי מאגר: רגיל, מרוחק, וירטואלי

כדי לקבל מידע על חבילה באמצעות npm או yarn:

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

  2. מריצים את הפקודה המתאימה:

    npm view
    
    yarn info
    

כדי לראות את החבילות ואת גרסאות החבילות באמצעות Google Cloud המסוף או gcloud:

המסוף

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

    פתיחת הדף Repositories

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

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

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

gcloud

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

gcloud artifacts packages list [--repository=REPOSITORY] [--location=LOCATION]

מחליפים את מה שכתוב בשדות הבאים:

  • REPOSITORY הוא שם המאגר. אם הגדרתם מאגר ברירת מחדל, אתם יכולים להשמיט את הדגל הזה כדי להשתמש בברירת המחדל.
  • LOCATION הוא המיקום האזורי או המיקום במספר אזורים של המאגר. ‫ אם הגדרתם מיקום ברירת מחדל, תוכלו להשמיט את הדגל הזה כדי להשתמש בברירת המחדל.

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

gcloud artifacts versions list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

מחליפים את מה שכתוב בשדות הבאים:

  • PACKAGE הוא המזהה של החבילה או המזהה המלא של החבילה.
  • REPOSITORY הוא שם המאגר. אם הגדרתם מאגר ברירת מחדל, אתם יכולים להשמיט את הדגל הזה כדי להשתמש בברירת המחדל.
  • LOCATION הוא המיקום האזורי או המיקום במספר אזורים של המאגר. ‫ אפשר להשתמש בדגל הזה כדי להציג מאגרי מידע במיקום ספציפי. אם הגדרתם מיקום ברירת מחדל, אתם יכולים להשמיט את הדגל הזה כדי להשתמש בברירת המחדל.

הצגת חבילות וגרסאות מ Google Cloud מסוף או מ-CLI של gcloud זמינה רק למאגרים רגילים ומרוחקים.

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

מרכיב רשימת קבצים

מצבי מאגר: רגיל, מרוחק

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

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

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

gcloud artifacts files list

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

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION

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

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE

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

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --version=VERSION
כדי להציג רשימה של קבצים לתג מסוים:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --tag=TAG

מחליפים את הערכים הבאים:

  • LOCATION: המיקום האזורי או הרב-אזורי של המאגר.
  • PROJECT: Google Cloud מזהה הפרויקט. אם מזהה הפרויקט מכיל נקודתיים (:), כדאי לעיין במאמר בנושא פרויקטים בהיקף הדומיין.
  • REPOSITORY: השם של המאגר שבו התמונה מאוחסנת.
  • PACKAGE: שם החבילה.
  • VERSION: הגרסה של החבילה.
  • TAG: התג שמשויך לחבילה.

דוגמאות

כדאי לעיין בפרטי החבילה הבאים:

  • פרויקט: my-project
  • מאגר: my-repo
  • מיקום המאגר: us-west1
  • חבילה: my-app

הפקודה הבאה מציגה רשימה של כל הקבצים במאגר my-repo במיקום us-west1 בתוך פרויקט ברירת המחדל:

gcloud artifacts files list \
    --location=us-west1 \
    --repository=my-repo
הפקודה הבאה מציגה רשימה של קבצים בגרסה 1.0 של החבילה.

gcloud artifacts files list \
    --project=my-project \
    --location=us-west1 \
    --repository=my-repo \
    --package=my-app \
    --version=1.0
הפקודה הבאה מציגה רשימה של קבצים בגרסה של החבילה עם התג 1.0-dev

gcloud artifacts files list \
    --project=my-project \
    --location=us-west1 \
    --repository=my-repo \
    --package=my-app \
    --tag=1.0-dev

תיוג חבילות

מצבי מאגר: רגיל

You can view, add, update, and delete tags. Tags can help you manage semantic versions of your packages and streamline installation of packages at a specific stage of development.

For example, you can tag the current release candidate build with rc. Your team can then install the correct version based on the tag instead of a version specifier, and unpublishing unused pre-release versions won't break your dependencies on the release candidate package.

Viewing tags

To view tags for a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. Click the package to view versions and the associated tags.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the command:

gcloud artifacts tags list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

Where

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.

For example, to view tags for the package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags list --package=my-pkg --repository=my-repo

Creating tags

You can create a tag for a specific version of a package.

To tag an existing image in a repository:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. Click the package to view versions of the package.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags create TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • VERSION is version of the package that you want to tag.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to create the tag release-candidate for version 1.0.0 of package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags create release-candidate --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Updating tags

You can change a tag associated with a package version.

To change an existing tag:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. Click the package to view versions of the package.

  3. Select the package version with the tag to change.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Edit the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags update TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • VERSION is version of the package that you want to tag.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to change the tag for version 1.0.0 of package my-package to production in the repository my-repo in the default location, run the command:

gcloud artifacts tags update production --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Untagging package versions

You can remove an existing tag from a package version.

To remove a tag:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. Click the image to view versions of the image.

  3. Select the image version to untag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Delete the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags delete TAG --package=PACKAGE \
    [--location=<LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to remove the tag release-candidate from package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags delete release-candidate --package=my-pkg \
    --repository=my-repo

התקנת חבילות

מצבי מאגר: רגיל, מרוחק, וירטואלי

כדי להתקין חבילה ממאגר החבילות של Node.js:

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

  2. משתמשים בפקודה npm install או yarn add.

    npm

    כדי להתקין את הגרסה עם התג latest:

    npm install @SCOPE/PACKAGE
    

    כדי להתקין גרסה עם תג אחר:

    npm install @SCOPE/PACKAGE@TAG
    

    כדי להתקין גרסה ספציפית:

    npm install @SCOPE/PACKAGE@VERSION
    

    חוטי סריגה

    כדי להתקין את הגרסה עם התג latest:

    yarn add @SCOPE/PACKAGE
    

    כדי להתקין גרסה עם תג אחר:

    yarn add @SCOPE/PACKAGE@TAG
    

    כדי להתקין גרסה ספציפית:

    yarn add @SCOPE/PACKAGE@VERSION
    

    מחליפים את הערכים הבאים:

    • SCOPE הוא ההיקף שמשויך למאגר. אם מאגר החבילות של Node.js לא מוגדר עם היקף, משמיטים את @SCOPE/ מהפקודה.
    • PACKAGE הוא שם החבילה במאגר.
    • TAG הוא תג לגרסה שרוצים להתקין.
    • VERSION הוא מספר הגרסה שרוצים להתקין.

כשמציינים חבילה כתלות ב-package.json, חשוב לכלול את ההיקף של המאגר. בדוגמה הבאה מוצג היקף @dev-repo לחבילה בשם my-package.

"dependencies": {
  "@dev-repo/my-package": ">=1.0.0"
}

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

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

במאגר וירטואלי, מערכת Artifact Registry מחפשת את החבילה המבוקשת במאגרים במעלה הזרם.

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

לדוגמה, נניח שיש מאגר וירטואלי עם הגדרות העדיפות הבאות למאגרי upstream:

  • main-repo: העדיפות מוגדרת ל-100
  • secondary-repo1: העדיפות מוגדרת ל-80.
  • secondary-repo2: העדיפות מוגדרת ל-80.
  • test-repo: העדיפות מוגדרת ל-20.

ל-main-repo יש את ערך העדיפות הגבוה ביותר, ולכן המאגר הווירטואלי תמיד מחפש בו קודם.

העדיפות של secondary-repo1 ושל secondary-repo2 מוגדרת כ80. אם חבילה מבוקשת לא זמינה ב-main-repo, ‏ Artifact Registry מחפש אותה במאגרים האלה. מכיוון שלשני המאגרים יש אותו ערך עדיפות, אם הגרסה זמינה בשניהם, Artifact Registry יכול לבחור להציג חבילה מכל אחד מהם.

ל-test-repo יש את ערך העדיפות הנמוך ביותר, והוא יציג פריט מאוחסן אם הוא לא נמצא באף אחד ממאגרי המידע האחרים במעלה הזרם.

מחיקת חבילות

מצבי מאגר: רגיל, מרוחק

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

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

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

אם רוצים לעודד משתמשים להתקין גרסה מעודכנת של חבילה, אפשר להשתמש בפקודה npm deprecate כדי לסמן את הגרסה הישנה של החבילה כגרסה שהוצאה משימוש. כשמשתמש מנסה להתקין את החבילה שהוצאה משימוש, Artifact Registry מחזיר אזהרה על הוצאה משימוש.

Before you delete a package or package version, verify that any you have communicated or addressed any important dependencies on it.

To delete a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Select the package that you want to delete.

  4. Click DELETE.

  5. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts packages delete PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] [--async]

Replace the following:

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

To delete versions of a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    פתיחת הדף Repositories

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of that package.

  4. Select versions that you want to delete.

  5. Click DELETE.

  6. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts versions delete VERSION \
    --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] \
    [--async]

Replace the following:

  • VERSION is the name of the version to delete.
  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

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