# Conditional based on custom environment variable

**URL:** <https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383>\
**Category:** Pipelines\
**Created:** [May 22, 2025, 8:47am UTC](https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383 "2025-05-22T08:47:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MishaLT](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MishaLT](https://forum.buildkite.community/u/MishaLT)\
**Post date:** [May 22, 2025, 8:47am UTC](https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383/1 "2025-05-22T08:47:42Z")

</div>

Use-case:  
I run the same pipeline in 2 situations: on git push and triggered via API.  
I need to distinguish those 2 cases and be able to run different steps for them.  
So I assumed it would be logical to pass the env variable that will exist only in the case of API call.  
Like `API="true"` or something similar.  
And then in the pipeline have

```auto
if: build.env('API') == null

```

and

```auto
if: build.env('API') != null

```

But this does not seem to work. If I don’t define the custom env var, it just gets ignored.  
If I define the custom variable for the pipeline, the value that I set via API does not seem to affect the pipeline upload step. Though it gets propagated inside the steps.

Is there any suggestion whether it should work?  
What would be the alternative way to use in conditionals something that I can pass in the API trigger call?  
Thanks

---

<div class="post-metadata">

**Author:** ![MishaLT](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MishaLT](https://forum.buildkite.community/u/MishaLT)\
**Post date:** [May 22, 2025, 8:53am UTC](https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383/2 "2025-05-22T08:53:18Z")

</div>

~~I can see that I can potentially use `BUILDKITE_SOURCE="api"`~~  
~~but it is a bit generic, I’d prefer to have some custom env var if possible~~

OK, update: I cannot use BUILDKITE\_SOURCE  
buildkite-agent: fatal: Failed to upload and process pipeline: Pipeline upload rejected: One of the steps you provided was invalid: Error parsing `if`  
2025-05-22 19:13:31 AEST  
expression: Interpolation of “BUILDKITE\_SOURCE” is not supported (see [Environment variables | Buildkite Documentation](https://buildkite.com/docs/pipelines/environment-variables#variable-interpolation)).  
2025-05-22 19:13:31 AEST  
You can instead interpolate this variable at runtime (see [Environment variables | Buildkite Documentation](https://buildkite.com/docs/pipelines/environment-variables#runtime-variable-interpolation)).

---

<div class="post-metadata">

**Author:** ![MishaLT](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MishaLT](https://forum.buildkite.community/u/MishaLT)\
**Post date:** [May 22, 2025, 9:18am UTC](https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383/3 "2025-05-22T09:18:26Z")

</div>

more specific example:  
I’m trying to achieve something like that

```auto
- if: build.env('TEST') == null
  label: ENV VAR STEP
  agents:
    queue: main
  command: exit 1

```

and be able to pass this env var TEST via API and have it not set when triggered via git push.

---

<div class="post-metadata">

**Author:** ![Owen](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/owen/32/1616_2.png) [@Owen](https://forum.buildkite.community/u/Owen)\
**Post date:** [May 22, 2025, 10:29am UTC](https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383/4 "2025-05-22T10:29:10Z")

</div>

Hi @MishaLT, welcome to the Buildkite community :) I see that you’ve explored a couple of possible solutions here. It’s true that if you want to use a custom env var, it has to be defined in the pipeline config; you can’t pass in arbitrary ones and have them be instantiated in the build.

The easiest path forward for you might be to use the `build.source` variable (see [Using conditionals | Buildkite Documentation](https://buildkite.com/docs/pipelines/configure/conditionals#variable-and-syntax-reference-variables)), rather than the BUILDKITE\_SOURCE environment variable. You mentioned wanting to be able to use the conditional around the pipeline upload step; this would enable you to do that, and it also avoids the interpolation issue that you ran into. Here’s a simple example:

```auto
steps:
  - command: buildkite-agent pipeline upload
    if: build.source == 'webhook'
  - command: buildkite-agent annotate "This build was triggered by the REST API!"
    if: build.source == 'api'

```

Here, we will call the pipeline upload command if the job is triggered by a webhook, but we will do something different if the REST API was used.

If that doesn’t help get you going, it might be useful if you could contact us by email at [support@buildkite.com](mailto:support@buildkite.com), and share the details of your build privately, so we can get a better idea of what you are trying to do and craft a solution that will work for you.

---

<div class="post-metadata">

**Author:** ![MishaLT](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MishaLT](https://forum.buildkite.community/u/MishaLT)\
**Post date:** [May 26, 2025, 3:08am UTC](https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383/5 "2025-05-26T03:08:01Z")

</div>

ok, thanks for the suggestion.  
But i’m still wondering if there is any way to have a conditional based on the env var.  
I’m not sure if there are any echnical limitations, but it looks like pretty naturally expected behavior TBH.  
Thanks

---

<div class="post-metadata">

**Author:** ![lizette](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/lizette/32/1161_2.png) [@lizette](https://forum.buildkite.community/u/lizette)\
**Post date:** [May 26, 2025, 4:07am UTC](https://forum.buildkite.community/t/conditional-based-on-custom-environment-variable/4383/6 "2025-05-26T04:07:02Z")

</div>

Hi @MishaLT ,

As previously suggested, to use env vars in conditionals, you can use the function `build.env()` to return the value of the environment variable you defined and use it in the conditional, as mentioned in our docs - [Using conditionals | Buildkite Documentation](https://buildkite.com/docs/pipelines/configure/conditionals#variable-and-syntax-reference-variables).

Cheers!
