问题描述
我想创建一个Azure任务,该任务每5分钟向API发出一次HTTP GET请求。由于此请求而返回的文件将存储在存储容器中。但是,我不知道什么是最好的方法。
我尝试过的事情:
问题是我是Azure的新手,还不了解如何解决所有问题的体系结构,因此我什至不知道Google应该使用哪些关键字。
执行此Azure的最佳方法是什么?这是在Azure devops或Azure门户中完成的吗?哪个更好的选择?最后,最重要的是,您能否提供一个简单的示例HTTP GET任务,该任务将在Azure环境中工作?
解决方法
您应该可以使用scheduled triggers
处理此问题# YAML file in the master branch
schedules:
- cron: "*/5 * * * *"
displayName: Run every 5 mins
branches:
include:
- master
您可以使用此task或通过powershell调用rest API
Invoke-RestMethod -Uri 'https://cat-fact.herokuapp.com/facts' -Headers @{ 'Authentication' = 'Bearer xxxxxxxxxxxxxxxx' }
但是,我建议您为此使用Azure功能。您可以在那里使用timer trigger,然后可以使用各种语言进行REST API调用,并且还可以与Azure Blob 集成在一起,从而非常轻松地保存响应。
假设您具有如下定义的绑定:
{
"bindings": [
{
"authLevel": "function","type": "httpTrigger","direction": "in","name": "req","methods": [
"get","post"
]
},{
"type": "blob","name": "inBlob","dataType": "binary","path": "samples-input/text.zip"
},"name": "outBlob","direction": "out","path": "samples-output/string.txt"
}
]
}
然后
push-outputbinding -name outBlob -value $someValue
如果您的功能无法保存到存储帐户中,则会失败。因此,您不需要任何特定的代码即可测试内容是否已保存。
,整个过程:
1。按照 Krzysztof Madej 的建议,您可以将yaml管道与- cron: "*/5 * * * *"
一起使用,每五分钟运行一次管道。但是根据您的描述,您还应该启用always: true
,以便即使源存储库中没有任何更改,管道也可以运行。
2。此外,要以这种格式使用计划,您需要禁用任何CI触发器或PR触发器。
如果只想使用计划的触发器来运行管道,则必须通过在YAML文件中指定pr:none和trigger:none来禁用PR和持续集成触发器。
3。除了计划(每隔5分钟),您还需要一个Powershell任务来调用其余的api。获取响应,然后将响应写入新创建的json文件。(或文本文件)
4。之后,您可以使用Azure File Copy task将包含响应的文件上载到Azure存储容器。
这是我的基本工作示例:
pool:
vmImage: 'windows-latest'
schedules:
- cron: "*/5 * * * *"
displayName: Run every 5 mins
branches:
include:
- master
always: 'true'
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
#Call the rest api.
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)_apis/projects/$env:SYSTEM_TEAMPROJECTID/teams?api-version=5.1"
$response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
#Get the response and then pass it into one spcific file.
write-host $($response | ConvertTo-Json -Depth 100)
$response | ConvertTo-Json -depth 100 | Out-File "$(System.DefaultWorkingDirectory)\Backup-$(Build.BuildId).json"
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
- task: AzureFileCopy@4
inputs:
SourcePath: '$(System.DefaultWorkingDirectory)\Backup-$(Build.BuildId).json'
azureSubscription: 'xxx'
Destination: 'AzureBlob'
storage: 'xxx'
blobPrefix: 'xxx'
ContainerName: 'xxx'
此外:
这里是similar topic,有关如何使用AzureFileCopy
进行备份。如果遇到AzureFileCopy
任务的权限问题,可以参考this link。