Multiple Value to Variable

HI Team,

We have a need to set the multiple variable’s value to a single Variable with different scope. I have tried below two options but somehow both are not working. Can you please help me with this?

“Variables”: [
{
“name”: “User”,
“value”: "$DEV_User,
“scope”: “Dev”,
“value”: “$DEV2_User”,
“scope”: “Dev2”
}

Second way:

“Variables”: [
{
“name”: “User”,
“value”: “$DEV_User”,
“scope”: “Dev”
}
{
“name”: “User”,
“value”: “$DEV2_User”,
“scope”: “Dev2”
}
]

Hello,

Can you tell me a bit more about your use case? With what you have listed above, is there a reason why a variable setup like this wouldn’t work?

Well I have an automation process that creates octopus project from Jenkins, those variable values will be a user input from a form.

$Dev_User will be resolved as per input value and same will be set as variable value in octopus.

So far I used to set single value to a variable but this time I have need to set multiple variable values with different environment scope.

In the json format we provide variable details like name and value that works but when I am using scope along with name and value then it’s not working.

Okay, thank you for the added clarification!

So, from a Jenkins job, you’re calling the Octopus API and creating variables for your new projects, correct? And you’re wanting to create multiple scopes for the variables, and you’re looking for the correct format for the JSON? Confirming so I can provide the correct resources for your use case.

Yeah correct, I tried different different formats, I have mentioned couple of in this article. The value is also getting set but even passing different value for each env scope it’s taking only the last value and setting it up for all env.

FYI, I have one template that we use to create a octopus setup from Jenkins and in that template default values are set for that variable with different scope.

Expecting those values to get replaced by the provided values.

I did some testing with the API to confirm the correct format for variables in JSON with separate scopes.

I used an existing PowerShell example we have published and attempted to set the two values for UserName I showed above.

The endpoint it reached out to was [OCTOPUS-URL]/api/[SPACE-ID]/variables/[VARIABLESET-ID], and the relevant JSON was formatted like this:

{
    "Id": "variableset-Projects-85",
    "OwnerId": "Projects-85",
    "Version": 1,
    "Variables": [
      {
        "IsSensitive": false,
        "Value": "Dev1_User",
        "Scope": {
          "Environment": [
            "Environments-41"
          ],
          "Role": []
        },
        "Type": "String",
        "Name": "UserName"
      },
      {
        "IsSensitive": false,
        "Value": "Dev2_User",
        "Scope": {
          "Environment": [
            "Environments-42"
          ],
          "Role": []
        },
        "Type": "String",
        "Name": "UserName"
      }

// remainder of the JSON payload truncated for brevity

Hopefully between the PowerShell example and the JSON snippet above, that can help you get your multiple values updated. Please let me know if that doesn’t work or if you have additional questions.