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.
Now, I’ll add a task to this pipeline that runs a dir, using this variable.
I can save and run this, and I see the results of c:Users from a hosted agent.
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.
I can click this and see my variables. System.debug is set at runtime by default, but I see my other one.
If I click this, I can change the location. I’ll set this to c:.
When I let this run, note I get different results.
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.
I can alter this to add a variable by looking in the upper right, where I see a “variables” button. Click this.
I get a list of variables, which is none in this case. I’ll click “New variable”.
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.
When I save this, I see my variable.
Now, I can alter my script. I’ll add this as $(myLocation), where I surround the variable name with a $ and ().
I can validate and save this, which I do.
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.
Now when I run the pipeline, I have the variables item where I can change the variable.
I can also set this variable in YAML, like this:
However, if I set that value, I can’t change this at runtime. Here’s the runtime screen.
I can use a parameter instead. I’ll use this structure:
When I run this, I see a new box:
I can override this “Dir location”. When I set this to c:, I see these results:
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.