Skip to content

Commit

Permalink
Add project descriptor to env var tutorials
Browse files Browse the repository at this point in the history
Signed-off-by: David Freilich <dfreilich@vmware.com>
  • Loading branch information
dfreilich committed Aug 18, 2020
1 parent 3878976 commit 4b4d6fa
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions content/docs/app-developer-guide/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ summary="Environment variables are a common way to configure various buildpacks

Environment variables are a common way to configure various buildpacks at build-time.

Here are a few ways you can do so.
Below are a few ways you can do so. All of them will use our [samples][samples] repo for simplicity.

### Using CLI arguments (`--env`)

Expand All @@ -16,9 +16,6 @@ The `--env` parameter must be one of the following:
- `VARIABLE`, where the value of `VARIABLE` will be taken from the local environment

##### Example:

For this example we will use our [samples][samples] repo for simplicity.

```bash
# clone the repo
git clone https://github.com/buildpacks/samples
Expand Down Expand Up @@ -47,17 +44,14 @@ The following environment variables were set and available to buildpacks at buil
| `FOO` | `BAR` | _current environment_ |


### Using files (`--env-file`)
### Using env files (`--env-file`)

The `--env-file` parameter must be a path to a file where each line is one of the following:

- `VARIABLE=VALUE`
- `VARIABLE`, where the value of `VARIABLE` will be taken from the current environment

##### Example:

For this example we will use our [samples][samples] repo for simplicity.

```bash
# clone the repo
git clone https://github.com/buildpacks/samples
Expand Down Expand Up @@ -91,4 +85,41 @@ The following environment variables were set and available to buildpacks at buil

> **NOTE:** Variables defined using `--env` take precedence over variables defined in `--env-file`.
### Using Project Descriptor (`project.toml`, or `--descriptor`)
The `--descriptor` parameter must be a path to a file which follows the project.toml [schema][descriptor-schema].
You can define environment variables in an `env` table in the file, and pass those into the application.

##### Example:
```bash
# clone the repo
git clone https://github.com/buildpacks/samples

# Add an environment variable to the project.toml
printf "[[build.env]]\nname='HELLO'\nvalue='WORLD'" >> samples/apps/bash-script/project.toml

# build the app
pack build sample-app \
--builder cnbs/sample-builder:bionic \
--buildpack samples/buildpacks/hello-world/ \
--buildpack samples/apps/bash-script/bash-script-buildpack/ \
--path samples/apps/bash-script/

# run the app
docker run sample-app
```

The following environment variables were set and available to buildpacks at build-time:

| Name | Value | _Source_ |
|---------|---------|----------------------------|
| `HELLO` | `WORLD` | _hard-coded value in file_ |

<p class="spacer"></p>

> **NOTE:** Variables defined using `--env` or `--env-file` take precedence over variables defined in the `project.toml`.
> **NOTE:** `project.toml` can't detect environment variables (so, for instance, if one ran `export FOO=BAR` and added
>`name=FOO` to the `project.toml`, it wouldn't detect any value set for `FOO`).
[descriptor-schema]: /docs/reference/project-descriptor/
[samples]: https://github.com/buildpacks/samples

0 comments on commit 4b4d6fa

Please sign in to comment.