אתם יכולים להגדיר משתני סביבה שיקבלו ערך במהלך ה-build של קובץ אימג' של קונטיינר שלכם.
אתם גם יכולים להתאים אישית את קובץ אימג' של קונטיינר באמצעות הרחבה של פיתוח גרסת ה-build והרצה של קובצי אימג'.
גרסאות build מקומיות
כדי לעבוד עם גרסאות build מקומיות צריך להתקין את Pack CLI ואת Docker.
לפני שמתחילים
- מתקינים את Docker Community Edition (CE) בתחנת העבודה. Docker משמש את
packליצירת קובצי אימג' של OCI. - מתקינים את Pack CLI.
- מתקינים את הכלי Git source control כדי שתהיה אפשרות לטעון אפליקציות לדוגמה מ-GitHub.
הגדרה של משתני סביבה
כדי להגדיר משתני סביבה לגרסאות build מקומיות, מוסיפים את הדגל --env לפקודה pack לכל משתנה סביבה.
תוכלו להשתמש באחד או יותר ממשתני הסביבה שנתמכים על ידי סביבת זמן הריצה.
pack build SERVICE_NAME \
--env ENVIRONMENT_VARIABLE
מחליפים את:
SERVICE_NAMEבשם של השירות שאתם רוצים לתת לאפליקציה או לפונקציה.ENVIRONMENT_VARIABLEבמשתנה הסביבה שרוצים להגדיר במשך הזמן של תהליך ה-build.
- דוגמה
כדי להגדיר את משתנה הסביבה
GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app"בקובץ אימג' של קונטיינר, מריצים את הפקודה הבאה:pack build my-app \ --builder gcr.io/buildpacks/builder:v1 \ --env GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app" --env MY-LOCAL-ENV-VARIABLE
פיתוח גרסאות build מרחוק
כדי להגדיר משתני סביבה לגרסאות build מרחוק, משתמשים במתאר הפרויקט project.toml.
בזמן הבניה של קובץ האימג' בקונטיינר שלכם, Cloud Build משתמש במתאר הפרויקט project.toml.
לפני שמתחילים
- נכנסים לחשבון 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 Cloud Build and Artifact Registry APIs.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build and Artifact Registry APIs.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init - מוודאים שלפרויקט Google Cloud יש גישה למאגר קובצי אימג' של קונטיינר.
כדי להגדיר גישה למאגר Docker ב-Artifact Registry:
- יוצרים מאגר Docker חדש באותו מיקום של הפרויקט Google Cloud .
מחליפים את:gcloud artifacts repositories create REPO_NAME \ --repository-format=docker \ --location=REGION --description="DESCRIPTION"
REPO_NAMEבשם שבחרתם למאגר ב-Docker.-
REGIONעם המיקום בפרויקט או במיקום הקרוב ביותר למיקום הפרויקט. Google Cloud DESCRIPTIONבתיאור כלשהו.
לדוגמה, כדי ליצור מאגר
dockerב-us-west2שהתיאור שלו הוא 'Docker repository', מריצים את הפקודה:gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \ --location=us-west2 --description="Docker repository"
- מוודאים שהמאגר נוצר:
gcloud artifacts repositories list
השם שבחרתם למאגר ב-Docker אמור להופיע ברשימה.
- יוצרים מאגר Docker חדש באותו מיקום של הפרויקט Google Cloud .
אופציונלי: הורדה של אפליקציה לדוגמה
- משכפלים את המאגר לדוגמה ומעבירים אותו למכונה המקומית:
git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
- עוברים לספרייה שמכילה את הקוד לדוגמה של האפליקציה:
Go
cd buildpack-samples/sample-go
Java
cd buildpack-samples/sample-java-gradle
Node.js
cd buildpack-samples/sample-node
PHP
cd buildpack-samples/sample-php
Python
cd buildpack-samples/sample-python
Ruby
cd buildpack-samples/sample-ruby
.NET
cd buildpack-samples/sample-dotnet
פיתוח אפליקציות באמצעות משתני סביבה
- בתיקיית השורש של השירות, יוצרים או מעדכנים את מתאר הפרויקט
project.tomlכך שיכלול את הקטע[[build.env]]וכל אחד ממשתני הסביבה הנתמכים:[[build.env]] name = "ENVIRONMENT_VARIABLE_NAME" value = "ENVIRONMENT_VARIABLE_VALUE"
מחליפים את:
ENVIRONMENT_VARIABLE_NAMEבשם של משתנה סביבה נתמך כלשהו.ENVIRONMENT_VARIABLE_VALUEבערך שמתאים למשתנה הסביבה שצוין.
דוגמה
[[build.env]] name = "GOOGLE_ENTRYPOINT" value = "gunicorn -p :8080 main:app"
- שולחים את קוד המקור של האפליקציה ל-Cloud Build באמצעות
gcloud:gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
מחליפים את:
LOCATIONבשם האזור של מאגר הקונטיינרים. לדוגמה:us-west2-docker.pkg.devPROJECT_IDבמזהה הפרויקט. Google CloudREPO_NAMEבשם המאגר ב-Docker.IMAGE_NAMEבשם של קובץ האימג' בקונטיינר.
דוגמאות: אם הורדתם אפליקציה לדוגמה, מריצים את הפקודה המתאימה:
Go
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-go
Java
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-java-gradle
Node.js
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-node
PHP
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-php
Python
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-python
Ruby
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-ruby
.NET
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-dotnet
-
מוודאים שהאפליקציה לדוגמה פורסמה ב-
REPO_NAME:gcloud artifacts docker images list LOCATION-docker.pkg.dev/project-id/REPO_NAME
מחליפים את:
LOCATIONבשם האזור של מאגר הקונטיינרים. לדוגמה:us-west2-docker.pkg.devPROJECT_IDבמזהה הפרויקט. Google CloudREPO_NAMEבשם המאגר ב-Docker.