אחסון חבילות Node.js ב-Artifact Registry
במדריך למתחילים הזה נסביר איך להגדיר מאגר חבילות פרטי של Node.js ב-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.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
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.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init - אם רוצים להתחבר ל-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:
נכנסים למסוף Google Cloud .
בסרגל הכלים של המסוף Google Cloud , לוחצים על Activate Cloud Shell:

בחלק התחתון של המסוף ייפתח סשן של Cloud Shell בתוך מסגרת.
משתמשים בשורת הפקודה הזו כדי להריץ פקודות gcloud.
יצירת מאגר חבילות Node.js
יוצרים את המאגר של פריטי המידע שנוצרו בתהליך הפיתוח (Artifact) ב-Node.js.
יוצרים את המאגר.
המסוף
פותחים את הדף Repositories במסוף Google Cloud .
לוחצים על Create Repository.
מציינים את
quickstart-nodejs-repoכשם המאגר.בוחרים בפורמט npm ובמצב רגיל.
בקטע Location Type, בוחרים באפשרות Region ואז בוחרים את המיקום
us-central1.לוחצים על יצירה.
המאגר נוסף לרשימת המאגרים.
gcloud
מריצים את הפקודה הבאה כדי ליצור מאגר חבילות חדש של 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"מריצים את הפקודה הבאה כדי לוודא שהמאגר נוצר.
gcloud artifacts repositories list
כדי לקבל מידע נוסף על פקודות Artifact Registry, מריצים את הפקודה
gcloud artifacts.כדי לפשט את הפקודות של
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 עם הגדרות לאימות מול המאגר שיצרתם. במדריך למתחילים הזה, תעלו חבילה ואז תתקינו אותה בחבילה שנייה כתלות.
מורידים את חבילת emoji-regex, חבילה שמספקת ביטוי רגולרי להתאמה של כל סמלי האימוג'י ורצפים שלהם.
npm pack emoji-regexהפקודה מורידה ארכיון של החבילה.
מחולצים את הארכיון לספרייה
emoji-regex. הפקודה הבאה מיועדת לארכיון שלemoji-regexבגרסה 10.1.0:mkdir emoji-regex && tar xvf emoji-regex-10.1.0.tgz -C emoji-regex --strip-components 1יוצרים חבילת npm שנייה שבה תשתמשו כדי להתקין את החבילה
emoji-regexמ-Artifact Registry. כדי ליצור חבילה בסיסית, משתמשים בפקודהnpm init.mkdir npm-package2 cd npm-package2 npm init -yכשמוצגת בקשה, מאשרים את ערכי ברירת המחדל.
מגדירים את פרויקטים
emoji-regexו-npm-package2כך שיאומתו באמצעות מאגר Artifact Registry שיצרתם.מריצים את הפקודה הבאה. הפקודה מחזירה את הגדרות התצורה שצריך להוסיף לקובץ התצורה של 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מוסיפים את הגדרות התצורה מהשלב הקודם לקובץ
.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 מתקשר איתו כמאגר נפרד עם היקף משלו.
עורכים את
package.jsonבפרויקטיםemoji-regexו-npm-package2.מוודאים שהערך של
nameכולל את ההיקףquickstart.- עבור
emoji-regex:
"name": "@quickstart/emoji-regex"- עבור
npm-package2:
"name": "@quickstart/npm-package2"- עבור
בקטע
scripts, מוסיפים סקריפט ל-google-artifactregistry-auth, ספריית לקוח שמעדכנת את פרטי הכניסה למאגרי Artifact Registry."scripts": { "artifactregistry-login": "npx google-artifactregistry-auth --repo-config=./.npmrc --credential-config=./.npmrc" }
העלאת חבילה למאגר
מרעננים את טוקן הגישה כדי להתחבר למאגר.
npm run artifactregistry-loginמוסיפים את החבילה למאגר. אפשר להשתמש בפקודה
npmאוyarn.npm publishyarn publish
הצגת החבילה במאגר
כדי לוודא שהחבילה נוספה למאגר:
המסוף
פותחים את הדף Repositories במסוף Google Cloud .
ברשימת המאגרים, לוחצים על המאגר
quickstart-nodejs-repo.בדף Packages מופיעות החבילות במאגר.
לוחצים על חבילה כדי לראות את הגרסאות שלה.
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 על המשאבים שבהם השתמשתם בדף הזה:
לפני שמסירים מאגר, צריך לוודא שכל החבילות שרוצים לשמור זמינות במיקום אחר.
כדי למחוק את המאגר:
המסוף
פותחים את הדף Repositories במסוף Google Cloud .
ברשימת המאגרים, בוחרים במאגר
quickstart-nodejs-repo.לוחצים על Delete.
gcloud
כדי למחוק את מאגר
quickstart-nodejs-repo, מריצים את הפקודה הבאה:gcloud artifacts repositories delete quickstart-nodejs-repoאם רוצים להסיר את הגדרות ברירת המחדל של המאגר והמיקום שהגדרתם עבור ההגדרה הפעילה
gcloud, מריצים את הפקודות הבאות:gcloud config unset artifacts/repository gcloud config unset artifacts/location
המאמרים הבאים
- מידע נוסף על הגדרת אימות
- מידע נוסף על ניהול מאגרי מידע
- מידע נוסף על ניהול חבילות
- אפשר גם לקרוא את המשאבים שלנו בנושא DevOps ולעיין בתוכנית המחקר שלנו.