Overwrite default environment variables (e.g., BUILDKITE_REPO) via pipeline upload

Is it possible to override the value of a default environment variable? For example, I am passing the following YAML into buildkite-agent pipeline upload. My assumption was that the resulting job would accept the new BUILDKITE_REPO value, but it does not. I understand this is a bit of an edge case, but is this even possible?

steps:
  - env:
      BUILDKITE_REPO: https://github.com/some-other/repo.git
    command:
      - command specific to that repo

Hey @tom!

By default we don’t allow step specifications to override internal variables so they can be trusted by agent hook scripts, etc.

But you can pretty easily work around that by having an agent hook script that selectively copies a value from a non-protected variable (e.g. OVERRIDE_BUILDKITE_REPO) on top of a protected one – while we guarantee officially-defined variables will be set “correctly” at the start of execution, hooks are still free to change them as they see fit.

(This also allows you to decide whether you fully trust pipeline definitions to select any repo they like, or enforce rules on the permitted override values – only allowing repos matching your org, say.)

Sorry that’s a bit more involved than just setting a variable… does it sound workable to get you what you want?

Cheers,

Matthew

It does! Here is a little snippet I came up with that I put in my agent environment hook.

while IFS= read -r env_var; do
  key="${env_var%=*}"
  if [[ "$key" == "OVERRIDE_BUILDKITE_"* ]]; then
    export "${key#OVERRIDE_}"="${env_var#*=}"
  fi
done < <(env)

Worked like a charm!