I was wondering if there was a way to use the service connection to do that? Is there a way to get it in the script?
I am afraid that the service connection couldn't be used in your bash script. In azure devops, the service connection needs to be used in a specific task, and its stored content cannot be read directly by the bash script.
I suggest that you could save your username and token in Variable Group(Pipelines -> Library -> Variable Group
).
You could try to use the following PowerShell script to create variable groups for all Organization Projects: API: Variablegroups - Add
Projects - List
$token = "PAT"
$url="https://dev.azure.com/{OrganizationName}/_apis/projects?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach( $projectid in $response.value.id )
{
echo $projectid
$url2 = "https://dev.azure.com/{OrganizationName}/_apis/distributedtask/variablegroups?api-version=6.0-preview.2"
$body = "{
`"name`":`"Cocoapodsauth`",
`"type`":`"Vsts`",
`"variables`":`
{`"token`":{`"isSecret`":true,`"value`":`"PAT`"},`"username`":{`"isSecret`":true,`"value`":`"username`"}},
`"variableGroupProjectReferences`":[
{
`"name`":`"Cocoapodsauth`",
`"projectReference`":
{
`"id`":`"$projectid`"
}}]
}"
$response = Invoke-RestMethod -Uri $url2 -Headers @{Authorization = "Basic $token"} -Method Post -Body $body -ContentType application/json
}
The above script will traverse all the projects, and then it will create a variable group for each project.
In this case , you could directly use the variable via referring to the Variable Group.
