Impostare le variabili di ambiente per configurare le build

Puoi configurare le variabili di ambiente impostate durante la creazione dell'immagine container.

Puoi anche personalizzare l'immagine container estendendo le immagini di build ed esecuzione.

Build locali

Per le build locali, devi aver installato Pack CLI e Docker.

Prima di iniziare

  1. Installa Docker Community Edition (CE) sulla tua workstation. Docker viene utilizzato da pack come generatore di immagini OCI.
  2. Installa Pack CLI.
  3. Installa lo strumento di controllo del codice sorgente Git per recuperare l'applicazione di esempio da GitHub.

Imposta le variabili di ambiente

Per impostare le variabili di ambiente per le build locali, aggiungi il flag --env al comando pack per ogni variabile di ambiente.

Puoi utilizzare una o più delle variabili di ambiente supportate dal runtime.

pack build SERVICE_NAME \
    --env ENVIRONMENT_VARIABLE

Sostituisci:

  • SERVICE_NAME con il nome del servizio per la tua applicazione o funzione.
  • ENVIRONMENT_VARIABLE con la variabile di ambiente che vuoi impostare durante il tempo di compilazione.
Esempio

Per impostare la variabile di ambiente GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app" nell'immagine container, esegui questo comando:

pack build my-app \
    --builder gcr.io/buildpacks/builder:v1 \
    --env GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app"
    --env MY-LOCAL-ENV-VARIABLE

Build remoti

Per impostare le variabili di ambiente per le build remote, utilizza il descrittore del progetto project.toml. Il descrittore del progetto project.toml viene utilizzato da Cloud Build durante la creazione dell'immagine container.

Prima di iniziare

  1. Accedi al tuo account Google Cloud . Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti senza costi per l'esecuzione, il test e il deployment dei carichi di lavoro.
  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 Cloud Build and Artifact Registry APIs, if any are 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 APIs

  5. Install the Google Cloud CLI.

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

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

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

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

  10. Enable the Cloud Build and Artifact Registry APIs, if any are 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 APIs

  11. Install the Google Cloud CLI.

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

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

    gcloud init
  14. Assicurati che il tuo progetto Google Cloud abbia accesso a un repository di immagini container.

    Per configurare l'accesso a un repository Docker in Artifact Registry:

    1. Crea un nuovo repository Docker nella stessa località del tuo progetto Google Cloud .
      gcloud artifacts repositories create REPO_NAME \
      --repository-format=docker \
      --location=REGION --description="DESCRIPTION"
      Sostituisci:
      • REPO_NAME con il nome che scegli per il repository Docker.
      • REGION con la posizione in o più vicina alla posizione del tuo progetto Google Cloud .
      • DESCRIPTION con una descrizione a tua scelta.

      Ad esempio, per creare un repository docker in us-west2 con la descrizione "Docker repository", esegui:

      gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \
      --location=us-west2 --description="Docker repository"
    2. Verifica che il repository sia stato creato:
      gcloud artifacts repositories list

      Dovresti vedere il nome scelto per il repository Docker nell'elenco.

(Facoltativo) Scaricare un'applicazione di esempio

  1. Clona il repository di esempio sulla tua macchina locale:
    git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
  2. Passa alla directory che contiene il codice campione dell'applicazione:

    Vai

    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

Crea l'applicazione con le variabili di ambiente

  1. Nella directory radice del servizio, crea o aggiorna il descrittore del progetto project.toml in modo che includa la sezione [[build.env]] e una delle variabili di ambiente supportate:
    [[build.env]]
        name = "ENVIRONMENT_VARIABLE_NAME"
        value = "ENVIRONMENT_VARIABLE_VALUE"

    Sostituisci:

    • ENVIRONMENT_VARIABLE_NAME con il nome di una variabile di ambiente supportata.
    • ENVIRONMENT_VARIABLE_VALUE con il valore corrispondente per la variabile di ambiente specificata.

    Esempio

    [[build.env]]
        name = "GOOGLE_ENTRYPOINT"
        value = "gunicorn -p :8080 main:app"
  2. Utilizza gcloud per inviare il codice sorgente dell'applicazione a Cloud Build:
    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

    Sostituisci:

    • LOCATION con il nome della regione del tuo repository di container. Esempio: us-west2-docker.pkg.dev
    • PROJECT_ID: l'ID del tuo progetto Google Cloud .
    • REPO_NAME con il nome del tuo repository Docker.
    • IMAGE_NAME con il nome dell'immagine container.

    Esempi: se hai scaricato un'applicazione di esempio, esegui il comando corrispondente:

    Vai

    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
  3. Verifica che l'applicazione di esempio sia stata pubblicata correttamente su REPO_NAME:
    gcloud artifacts docker images list LOCATION-docker.pkg.dev/project-id/REPO_NAME

    Sostituisci:

    • LOCATION con il nome della regione del repository del container. Esempio: us-west2-docker.pkg.dev
    • PROJECT_ID: l'ID del tuo progetto Google Cloud .
    • REPO_NAME con il nome del tuo repository Docker.