liveBook · Manning

Environment variable

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

An environment variable is a named value supplied by the execution environment to configure a program without changing its source code.

An environment variable is a named value supplied when a program runs. It lets the same microservice start with different settings without editing its code. A Node.js HTTP service can read `process.env.PORT` to choose its listening port. section 2.6.6

Set a port before starting the service

Set `PORT=3000` in the terminal, then run `node src/index.js`. Use `export PORT=3000` in a Unix-style shell or `set PORT=3000` in Windows Command Prompt. If `PORT` is missing, the service throws an error naming the required variable instead of starting with an unintended configuration. section 2.6.6

A service may need several values before it can start. A metadata service, for example, requires `PORT` for its web server, `DBHOST` and `DBNAME` for MongoDB, and `RABBIT` for RabbitMQ. Set the required values before running `npm start` or the development script `npm run start:dev`. section 10.5.1

env:
  VERSION: ${{ github.sha }}                             #1
  CONTAINER_REGISTRY: ${{ secrets.CONTAINER_REGISTRY }}  #2
  REGISTRY_UN: ${{ secrets.REGISTRY_UN }}                #3
  REGISTRY_PW: ${{ secrets.REGISTRY_PW  }}               #3

The workflow `env` mapping shows two sources for values passed to deployment scripts: `github.sha` supplies `VERSION`, while GitHub secrets supply registry settings and credentials. section 8.9.7 section 8.9.8

Keep sensitive deployment values in GitHub secrets rather than hardcoding them in workflow YAML or shell scripts. This also makes the deployment code easier to reuse. In a production deployment, `CONTAINER_REGISTRY` supplies a registry address to the shell script; `envsubst` inserts it into templated Kubernetes configuration before `kubectl` applies that configuration. section 8.9.7 section 10.9.2

When a deployment fails, check the GitHub Actions workflow history and read its error messages. Verify that every required variable is set, then compare local and production values if the process works locally but fails after deployment. Testing the deployment process locally can shorten this investigation. section 8.9.10

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Shows shell assignment, Node.js access, required-value errors, and workflow mappings; does not explore environment-variable internals.
Examples
Uses `PORT=3000`, metadata-service connection settings, and a workflow that maps a commit hash and registry secrets into variables.
What it assumes
Assumes basic terminal and Node.js project use while showing how to read `process.env` and set `PORT` explicitly.
What you can do after
Operate/configure a microservice with required settings and diagnose deployment failures caused by missing or differing variables.
How it connects
Links environment variables to HTTP server ports, MongoDB and RabbitMQ connections, GitHub secrets, and Kubernetes configuration templates.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.