Search code examples
variablesazure-devopsazure-pipelines

How add a specific variable into Azure Devops Libraries?


In my pipeline (bash, ubuntu-latest) i want to add into already created library a variable (last git commit hash) is it possible ??

Documentation not telling too much about it


Solution

  • You could use this API to update the variable group: https://learn.microsoft.com/en-us/rest/api/azure/devops/distributedtask/variablegroups/update?view=azure-devops-rest-6.0

    Use this predefined variable "Build.SourceVersion" to get the latest commit ID: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

    enter image description here

    Bash script:

    curl --location --request PUT 'https://dev.azure.com/{OrgName}/{ProjName}t/_apis/distributedtask/variablegroups/{variablegroupsID}?api-version=7.0' \
    --header 'Authorization: Basic {Base64PAT}=' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "variables": {
            "test": {
                "value": "$(Build.SourceVersion)"
            }
        },
        "id": 5,
        "type": "Vsts",
        "name": "1013",
        "description": "",
        "createdBy": {
            "displayName": "{displayName}",
            "id": "{id}",
            "uniqueName": "{uniqueName}"
        },
        "createdOn": "2022-10-13T07:41:40.3066667Z",
        "modifiedBy": {
            "displayName": "{displayName}",
            "id": "{id}",
            "uniqueName": "{uniqueName}"
        },
        "modifiedOn": "2022-10-13T07:41:40.3066667Z",
        "isShared": false,
        "variableGroupProjectReferences": [
            {
                "projectReference": {
                    "id": "{Proj id}",
                    "name": "{Proj Name}"
                },
                "name": "1013",
                "description": ""
            }
        ]
    }'
    

    Variable group sample:

    enter image description here

    Running the script via Azure Pipeline (bash, ubuntu-latest)

    enter image description here

    Result:

    enter image description here

    enter image description here

    Update1

    enter image description here