Skip to content

Instantly share code, notes, and snippets.

@IlyaFinkelshteyn
Last active June 28, 2018 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IlyaFinkelshteyn/ce12c41c065b5703c4efd8ff7e38de0f to your computer and use it in GitHub Desktop.
Save IlyaFinkelshteyn/ce12c41c065b5703c4efd8ff7e38de0f to your computer and use it in GitHub Desktop.
$API_TOKEN = "<replace>" # from https://ci.appveyor.com/api-token
$APPVEYOR_ACCOUNT_NAME = "<replace>" # from AppVeyor project URL
$APPVEYOR_PROJECT_SLUG = "<replace>" # from AppVeyor project URL
$VAR_NAME = "<replace>" # name of variable to update
$VAR_NEW_VALUE = "<replace>" # new variable value
$API_URL = "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/settings/environment-variables"
$ENCRYPT_URL = "https://ci.appveyor.com/api/account/encrypt"
$headers = @{
"Authorization" = "Bearer $env:API_TOKEN"
"Content-type" = "application/json"
}
# get variables collection from server
$s = Invoke-RestMethod -Uri $API_URL -Headers $headers -Method Get
# check if variable exists
if (($s | ? {$_.name -eq $VAR_NAME}) -eq $null) {
Write-warning "Cannot find variale $VAR_NAME"
return
}
# if variable is encrypted - encrypt new value
if (($s | ? {$_.name -eq $VAR_NAME -and $_.value.isEncrypted -eq "True"}) -ne $null) {
$body = @{plainValue = $VAR_NEW_VALUE}
$VAR_NEW_VALUE = Invoke-RestMethod -Uri $ENCRYPT_URL -Headers $headers -Body ($body | ConvertTo-Json) -Method POST
}
# update variable value
$s | ? {$_.name -eq $VAR_NAME} | % {$_.value.value = $VAR_NEW_VALUE}
# update variables coillection on server
Invoke-RestMethod -Uri $API_URL -Headers $headers -Body ($s | ConvertTo-Json) -Method Put
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment