Buildkite Pre-Command Environment Example

This repository is an example Buildkite pipeline that demonstrates how environment variables can be set through a repository hook.

See the full Getting Started Guide for step-by-step instructions on how to get this running.

Screenshot of example pipeline build page

How it works

This pipeline demonstrates how to use the pre-command hook to dynamically set environment variables at runtime for each job.

In this example:

  • .buildkite/hooks/pre-command sets a GIT_SHORT_HASH variable
  • check_environment_var.sh prints that variable
  • The hook runs once per job and is scoped to that job context only

This can be helpful for injecting metadata into your builds without hardcoding values into your pipeline configuration.

Notes

  • Environment vars are scoped to a job context and cleaned up to prevent polluting other job contexts.
  • The environment var exported by pre-command in this example is calculated every time a job in this pipeline is run.
  • If the environment variable was expensive to calculate or non-deterministic, you might want to consider only calculating it during the pipeline upload step and setting the env inside the pipeline.yml

License

See LICENSE (MIT)

More examples

Start turning complexity into an advantage

Create an account to get started with a 30-day free trial. No credit card required.

Buildkite Pipelines

Platform

  1. Pipelines
  2. Public pipelines
  3. Test Engine
  4. Package Registries
  5. Mobile Delivery Cloud
  6. Pricing

Hosting options

  1. Self-hosted agents
  2. Mac hosted agents
  3. Linux hosted agents

Resources

  1. Docs
  2. Blog
  3. Changelog
  4. Example pipelines
  5. Plugins
  6. Webinars
  7. Case studies
  8. Events
  9. Migration Services
  10. Comparisons

Company

  1. About
  2. Careers
  3. Press
  4. Security
  5. Brand assets
  6. Contact

Solutions

  1. Replace Jenkins
  2. Workflows for AI/ML
  3. Testing at scale
  4. Monorepo mojo
  5. Bazel orchestration

Legal

  1. Terms of Service
  2. Acceptable Use Policy
  3. Privacy Policy
  4. Subprocessors
  5. Service Level Agreement

Support

  1. System status
  2. Forum
© Buildkite Pty Ltd 2025