You can configure environment variables that are set during the build of your
container image.

You can also customize your container image by
[extending the build and run images](https://docs.cloud.google.com/docs/buildpacks/build-run-image).

## Local builds

For local builds, you must have the Pack CLI and Docker installed.

### Before you begin

1. Install [Docker Community Edition (CE)](https://docs.docker.com/engine/installation/) on your workstation. Docker is used by `pack` as an OCI image builder.
2. Install [Pack CLI](https://buildpacks.io/docs/tools/pack/).
3. Install the [Git source control](https://git-scm.com/downloads) tool to fetch the sample application from GitHub.

### Set environment variables

To set environment variables for local builds, you append the `--env` flag to
the `pack` command for each environment variable.

You can use one or more of the
[environment variables that are supported by your runtime](https://docs.cloud.google.com/docs/buildpacks/service-specific-configs).

    pack build SERVICE_NAME \
        --env ENVIRONMENT_VARIABLE

Replace:

- `SERVICE_NAME` with the name of the service for your application or function.
- `ENVIRONMENT_VARIABLE` with the environment variable that you want to set during build time.

Example

:   To set the `GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app"` environment variable
    in your container image, you run the following command:

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

## Remote builds

To set environment variables for remote builds, you use the
[`project.toml` project descriptor](https://buildpacks.io/docs/app-developer-guide/using-project-descriptor/).
The `project.toml` project descriptor is used by Cloud Build when your
container image is built.

### Before you begin

> [!NOTE]
> If you've already installed Google Cloud CLI previously, make sure you have the
> latest available version by running `gcloud components update`.

1. Ensure that your Google Cloud project has access to a container image repository. To configure access to a
   [Docker repository in
   Artifact Registry](https://docs.cloud.google.com/artifact-registry/docs/docker/store-docker-container-images):

   1. Create a new Docker repository in the same location of your Google Cloud project.

      ```bash
      gcloud artifacts repositories create REPO_NAME \
      --repository-format=docker \
      --location=REGION --description="DESCRIPTION"
      ```
      Replace:
      - `REPO_NAME` with the name that you choose for your Docker repository.
      - `REGION` with the [location](https://docs.cloud.google.com/artifact-registry/docs/repositories/repo-locations) in or nearest to the location of your Google Cloud project.
      - `DESCRIPTION` with a description of your choice.

      For example, to create a `docker` repository in
      `us-west2` with the description "Docker repository", you run:

      ```bash
      gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \
      --location=us-west2 --description="Docker repository"
      ```
   2. Verify that your repository was created:

      ```bash
      gcloud artifacts repositories list
      ```

      You should see name that you choose for your Docker repository in the list.

### Optional: Download a sample application

1. Clone the sample repository to your local machine:

   ```bash
   git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
   ```
2. Change to the directory that contains the application sample code:

   ### Go

   ```sh
   cd buildpack-samples/sample-go
   ```

   ### Java

   ```sh
   cd buildpack-samples/sample-java-gradle
   ```

   ### Node.js

   ```sh
   cd buildpack-samples/sample-node
   ```

   ### PHP

   ```sh
   cd buildpack-samples/sample-php
   ```

   ### Python

   ```sh
   cd buildpack-samples/sample-python
   ```

   ### Ruby

   ```sh
   cd buildpack-samples/sample-ruby
   ```

   ### .NET

   ```sh
   cd buildpack-samples/sample-dotnet
   ```

### Build the application with environment variables

1. In the root directory of your service, create or update your `project.toml` project descriptor to include the `[[build.env]]` section and any of the [supported
   environment variable](https://docs.cloud.google.com/docs/buildpacks/service-specific-configs):

   ```bash
   [[build.env]]
       name = "ENVIRONMENT_VARIABLE_NAME"
       value = "ENVIRONMENT_VARIABLE_VALUE"
   ```

   Replace:
   - `ENVIRONMENT_VARIABLE_NAME` with the name of a [supported
     environment variable](https://docs.cloud.google.com/docs/buildpacks/service-specific-configs).
   - `ENVIRONMENT_VARIABLE_VALUE` with the corresponding value for the specified environment variable.

   **Example**

   ```bash
   [[build.env]]
       name = "GOOGLE_ENTRYPOINT"
       value = "gunicorn -p :8080 main:app"
   ```
2. Use `gcloud` to submit the application source code to Cloud Build:

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
   ```

   Replace:
   - `LOCATION` with the region name of your container repository. Example: `us-west2-docker.pkg.dev`
   - `PROJECT_ID` with the ID of your Google Cloud project.
   - `REPO_NAME` with the name of your Docker repository.
   - `IMAGE_NAME` with the name of your container image.

   <br />

   **Examples**: If you downloaded a sample application, you run the
   corresponding command:

   ### Go

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-go
   ```

   ### Java

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-java-gradle
   ```

   ### Node.js

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-node
   ```

   ### PHP

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-php
   ```

   ### Python

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-python
   ```

   ### Ruby

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-ruby
   ```

   ### .NET

   ```sh
   gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-dotnet
   ```
3. Verify that the sample application was successfully published to `REPO_NAME`:

   ```bash
   gcloud artifacts docker images list LOCATION-docker.pkg.dev/project-id/REPO_NAME
   ```

   Replace:
   - `LOCATION` with the region name of your container repository. Example: `us-west2-docker.pkg.dev`
   - `PROJECT_ID` with the ID of your Google Cloud project.
   - `REPO_NAME` with the name of your Docker repository.