问题
I would like to develop an Azure DevOps build that needs to read some data and after the build update that data. My options currently are:
- Associate the data with the build as an artifact. The next build will know to look at the previous build for that artifact and thus will get the required data. Relies on the presence of the previous build, i.e. if anyone accidentally deletes it - poof. Also, if anyone screws the retention policy - poof.
- Store the data as file in the Git repo, either the one containing the build or a dedicated one. - the build will have to push the changes to the repo. In case of a dedicated repo, one will have to fetch the data manually also.
- A dedicated storage somewhere.
I am not particularly keen on hacking my way through with the items (1) and (2). Item (3) seems the most straightforward way, but then I have to maintain this separate storage. Even if it is the simplest blob storage in Azure.
Is there a better way?
EDIT 1
By scanning the Azure DevOps REST API documentation I came across this - https://docs.microsoft.com/en-us/rest/api/azure/devops/build/properties?view=azure-devops-rest-5.1
Never used it before, but it does seem to match my needs. I do not need to store a lot of data. Wonder if anyone has any experience with the build properties and if there are any gotchas there.
EDIT 2
I need to store less than 1K of textual information. I could store it in a file on a shared drive, but then there is too much maintenance headache - one need to make sure the permissions are right and that nobody deletes this file.
At the end I decided to store the information in the definition of the build I develop using the following powershell code:
$DefaultStateKey = "3edac6eed15745eb960e20b53e2bba68"
function ExtractValue($res, $StateKey)
{
$res = $res.value.$StateKey.'$value'
if ($res)
{
$res = $res | ConvertFrom-Json
}
$res
}
function Get-State(
[Parameter(Mandatory)]$TfsProject,
[Parameter(Mandatory)]$BuildDefId,
[ValidateNotNullOrEmpty()]$StateKey = $DefaultStateKey)
{
$url = "$TfsInstanceUrl/$TfsProject/_apis/build/definitions/$BuildDefId/properties?$TfsApiVersion-preview.1"
ExtractValue $(Invoke-RestMethod $url -UseDefaultCredentials) $StateKey
}
function Set-State(
[Parameter(Mandatory)]$TfsProject,
[Parameter(Mandatory)]$BuildDefId,
[Parameter(Mandatory)][AllowNull()]$value,
[ValidateNotNullOrEmpty()]$StateKey = $DefaultStateKey)
{
$url = "$TfsInstanceUrl/$TfsProject/_apis/build/definitions/$BuildDefId/properties?$TfsApiVersion-preview.1"
$Body = @{
path = "/$StateKey"
}
if ($null -eq $value)
{
$Body.op = 'Remove'
}
else
{
$Body.op = 'Add'
$Body.value = $value | ConvertTo-Json
}
$Body = $Body | ConvertTo-Json -Compress
ExtractValue $(Invoke-RestMethod $url `
-UseDefaultCredentials `
-Method Patch `
-ContentType "application/json-patch+json" `
-Body "[$Body]") $StateKey
}
来源:https://stackoverflow.com/questions/58220841/does-azure-devops-provide-persistent-storage-for-its-builds