Blog Post

Set a Runtime Variable in an Azure DevOps Pipeline

,

Note: I DO NOT recommend this. Any changes to a pipeline should be in code and through a PR.

That being said, I know this information is out there and some people need it. The question from a friend was how can they set a variable in an Azure DevOps Pipeline at runtime. This was for testing, and they wanted to change the pipeline behavior to test things when they ran them.

This post will show how to do this in classic and YAML pipelines. As a scenario, I’m just going to get a directory listing of a folder, and change that at runtime.

Classic Pipelines

I know the trend is everything in code. For experimenting and learning, I find this slightly annoying, so I like classic pipelines. I know others do.

In a classic pipeline, I can set variables. I’ll add a new one and call it myLocation. Over on the far right, there is a checkbox for “settable at runtime”. Check that.

2024-06-27 14_42_03-Dir Project-CI - Azure DevOps Services

Now, I’ll add a task to this pipeline that runs a dir, using this variable.

2024-06-27 14_54_13-Dir Project-CI - Azure DevOps Services

I can save and run this, and I see the results of c:Users from a hosted agent.

2024-06-27 14_50_22-Pipelines - Run 1346 logs

That’s the default behavior.

Now, let’s alter this at runtime. When I click “run pipeline, I see this on the right side as a blade. Note the “variables” section below.

2024-06-27 14_50_50-Pipelines - Runs for Dir Project-CI

I can click this and see my variables. System.debug is set at runtime by default, but I see my other one.

2024-06-27 14_53_35-Media Player

If I click this, I can change the location. I’ll set this to c:.

2024-06-27 14_50_40-

When I let this run, note I get different results.

2024-06-27 14_53_10-Pipelines - Run 1347 logs

I’ve changed behavior at runtime.

YAML Pipelines

In a YAML pipeline, I don’t have tabs or variables. Instead, I just get a script of sections, like this.

2024-06-27 14_58_28-Dir Project - Pipelines

I can alter this to add a variable by looking in the upper right, where I see a “variables” button. Click this.

2024-06-27 14_59_05-Dir Project - Pipelines

I get a list of variables, which is none in this case. I’ll click “New variable”.

2024-06-27 14_59_08-Media Player

This gives me a dialog where I can enter the information. Note I can set a default as well as let users override this with a checkbox.

2024-06-27 14_59_19-Dir Project - Pipelines

When I save this, I see my variable.

2024-06-27 14_59_24-Dir Project - Pipelines

Now, I can alter my script. I’ll add this as $(myLocation), where I surround the variable name with a $ and ().

2024-06-27 14_59_53-Dir Project - Pipelines

I can validate and save this, which I do.

2024-06-27 14_59_57-GPT-5 delayed , TikTok kill switch , inside Netflix's video encoding ‍ -

It’s valid, because I typed well, but this really should go through a PR. Since I’m testing, and I’d approve the PR, I’m doing it in main. I shouldn’t do this in any org.

2024-06-27 15_00_02-Dir Project - Pipelines

Now when I run the pipeline, I have the variables item where I can change the variable.

2024-06-27 15_00_17-Dir Project - Pipelines

I can also set this variable in YAML, like this:

2024-06-27 15_06_50-azure-pipelines.yml - Repos

However, if I set that value, I can’t change this at runtime. Here’s the runtime screen.

2024-06-27 15_07_56-Window

I can use a parameter instead. I’ll use this structure:

2024-06-27 15_23_21-Dir Project - Pipelines

When I run this, I see a new box:

2024-06-27 15_18_50-Dir Project - Pipelines

I can override this “Dir location”. When I set this to c:, I see these results:

2024-06-27 15_25_19-Pipelines - Run 20240627.17 logs

Summary

I’ve shown how to configure a variable to be set at runtime, both in classic and YAML pipelines.

Note, this a place an administrator can make a mistake, or run rogue, without review. This is not recommended. Put all pipeline changes through a PR.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating