# Custom Variable to decide next step execution

**URL:** <https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341>\
**Category:** General\
**Created:** [November 4, 2022, 3:01pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341 "2022-11-04T15:01:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sukurams](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@sukurams](https://forum.buildkite.community/u/sukurams)\
**Post date:** [November 4, 2022, 3:01pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/1 "2022-11-04T15:01:04Z")

</div>

Hello Team,

Looking to have Custom variable should have flag ( true / false) to decide next step if condition. Can someone please share the recommended way to achieve this?

For example

steps:

- label: ‘Get Delta files and deploy artifacts’  
commands:
  - chmod +x .buildkite/scripts/delta.sh
  - .buildkite/scripts/delta.sh $$BUILDKITE\_COMMIT $$BUILDKITE\_BRANCH
  - pwsh .buildkite/scripts/module-yaml.ps1
  - pwsh .buildkite/scripts/module-sailpointutils.ps1
  - “export CUSTOM\_VARIABLE=$$(pwsh .buildkite/scripts/deployartifact.ps1)”
  - "echo $$CUSTOM\_VARIABLE

- wait
- label: “SP Config Export Objects from Sandbox”  
commands:
  - pwsh .buildkite/scripts/module-sailpointutils.ps1
  - buildkite-agent artifact upload ./exportedfile.json  
key: “Export”  
if: “build.branch == ‘sv046x\_vault\_check && **CUSTOM\_VARIABLE** == true’”

---

<div class="post-metadata">

**Author:** ![jeremy](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/jeremy/32/1040_2.png) [@jeremy](https://forum.buildkite.community/u/jeremy)\
**Post date:** [November 4, 2022, 7:25pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/2 "2022-11-04T19:25:35Z")

</div>

Hey @sukurams 👋

You won’t be able to evaluate $CUSTOM\_VARIABLE in your next step, as exporting the variable in the command step will result in the value only being available in the same step, as each step runs in it’s own shell.

The recommendation would be to dynamically generate the `SP Config Export Objects from Sandbox`, evaluating `CUSTOM_VARIABLE` in a script that will upload the subsequent steps to the pipeline based on its value. We have some documentation around using conditionals in steps as well that gives some more examples!

> **[Using Conditionals](https://buildkite.com/docs/pipelines/conditionals#conditionals-in-steps)**
>
> Using conditionals, you can run builds or steps only when specific conditions are met. Define boolean conditions using C-like expressions.

---

<div class="post-metadata">

**Author:** ![sukurams](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@sukurams](https://forum.buildkite.community/u/sukurams)\
**Post date:** [November 11, 2022, 8:30pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/3 "2022-11-11T20:30:36Z")

</div>

@jeremy - Don’t we have a way to set custom environment variable from previous steps to refer upcoming steps ?.

---

<div class="post-metadata">

**Author:** ![paula](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/paula/32/954_2.png) [@paula](https://forum.buildkite.community/u/paula)\
**Post date:** [November 11, 2022, 9:43pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/4 "2022-11-11T21:43:15Z")

</div>

Hey @sukurams!

No, it’s not possible. If you need to share data between steps you need to use meta-data ([`buildkite-agent meta-data` v3 | Buildkite Documentation](https://buildkite.com/docs/agent/v3/cli-meta-data)), but it’s not possible with env vars.

Best,

---

<div class="post-metadata">

**Author:** ![sukurams](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@sukurams](https://forum.buildkite.community/u/sukurams)\
**Post date:** [November 17, 2022, 4:41pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/5 "2022-11-17T16:41:16Z")

</div>

@paula

Thank you. I was trying to do following way to check and limit the steps execution. I see error when I execute following pipeline.

Can you please guide me about this error and check if we can use as I trying below.

**022-11-17 11:36:43 WARN POST [https://xxxx.xxxx.com/v3/jobs/018d-325797978918/pipelines:](https://xxxx.xxxx.com/v3/jobs/018d-325797978918/pipelines:) 422 One of the steps you provided was invalid: Error parsing `if` expression: Unexpected `-`: expected `(`, comparison operator, `&&`, `||`, `?`, or EOF (line 2, column 11) (Attempt 0/60 Retrying in -2562047h47m11.854775807s)**  
**2022-11-17 11:36:43 ERROR Unrecoverable error, skipping retries**  
**2022-11-17 11:36:43 FATAL Failed to upload and process pipeline: POST [https://agent.buildkite.com/v3/jobs/01848671-8597-475f-8f99-325797978918/pipelines:](https://agent.buildkite.com/v3/jobs/01848671-8597-475f-8f99-325797978918/pipelines:) 422 One of the steps you provided was invalid: Error parsing `if` expression: Unexpected `-`: expected `(`, comparison operator, `&&`, `||`, `?`, or EOF (line 2, column 11)**  
**🚨 Error: The command exited with status 1**

```auto
steps:
  - label: 'Get Delta files and deploy artifacts'
    commands:
      - pwsh .buildkite/scripts/validatespconfig.ps1 | buildkite-agent meta-data set "SPCONFIG"
      - echo "$$(buildkite-agent meta-data get "SPCONFIG")"
  - wait
  - label: "SP Config Export Objects from Sandbox"
    commands:
      - pwsh .buildkite/scripts/module-sailpointutils.ps1
    key: "Export"
    if: |
        build.branch == 'sv046x_vault_check' && 
         buildkite-agent meta-data get "SPCONFIG" == "true"
  - wait
  - label: "SP Config Import Objects into Production"
    commands:
      - pwsh .buildkite/scripts/module-sailpointutils.ps1
    key: "Import"
    if: |
        build.branch == 'sv046x_vault_check' && 
         buildkite-agent meta-data get "SPCONFIG" == "true"
    depends_on: "Export"

```

---

<div class="post-metadata">

**Author:** ![jeremy](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/jeremy/32/1040_2.png) [@jeremy](https://forum.buildkite.community/u/jeremy)\
**Post date:** [November 17, 2022, 6:38pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/6 "2022-11-17T18:38:01Z")

</div>

Hi @sukurams 👋

The error is coming from your `if` statement in the YAML you shared:

> [@sukurams](#):
>
> ```auto
> if: |
> build.branch == 'sv046x_vault_check' && 
> buildkite-agent meta-data get "SPCONFIG" == "true"
> 
> ```

Unfortunately, It’s not possible to evaluate the results of the `buildkite-agent meta-data` command as part of a conditional in your pipeline steps. What you would need to do is run a command step that gets the meta-data and checks it, then uploads the conditional step as part of the `buildkite-agent pipeline upload` command, like this:

```auto
- command: |
      if [[$$(buildkite-agent meta-data get "SPCONFIG") == "true" && "$BUILDKITE_BRANCH_ID" == "sv046x_vault_check"]]; then
        cat <<- YAML | buildkite-agent pipeline upload
        steps:
          - label: "SP Config Export Objects from Sandbox"
            commands:
              - pwsh .buildkite/scripts/module-sailpointutils.ps1
      YAML
      fi

```

Cheers,  
Jeremy

---

<div class="post-metadata">

**Author:** ![sukurams](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@sukurams](https://forum.buildkite.community/u/sukurams)\
**Post date:** [November 17, 2022, 8:16pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/7 "2022-11-17T20:16:14Z")

</div>

@jeremy - Thank you. Do you know how to display the meta value after created. validatespconfig.ps1 will return true / false. I wanted to display the value to confirm about value.

```auto
- pwsh .buildkite/scripts/validatespconfig.ps1 | buildkite-agent meta-data set "SPCONFIG"
- echo "$$(buildkite-agent meta-data get "SPCONFIG")"

```

---

<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:** [November 17, 2022, 10:18pm UTC](https://forum.buildkite.community/t/custom-variable-to-decide-next-step-execution/2341/8 "2022-11-17T22:18:02Z")

</div>

Hi @sukurams 👋

We would want to set values to meta-data to make it available for the other steps, so the best way to confirm and display the value is to confirm it in another step.

Something like this:

```auto
steps:
 - label: "Set SPCONFIG"
   commands: pwsh .buildkite/scripts/validatespconfig.ps1 | buildkite-agent meta-data set "SPCONFIG" 
 - label: "Get SPCONFIG" 
   commands: echo "$$(buildkite-agent meta-data get "SPCONFIG")"

```

Hope this helps!

Cheers,  
Lizette
