Azure Devops-Python$(Pipeline.Workspace)-如何存储文件



我试图在Azure Devops上使用Python Yaml将证书响应存储为文本。我现在在下面的代码中得到一个语法错误。但我也不知道如何将文件存储在azure devops的$(Pipeline.Workspace(文件夹中。在谷歌上搜索,但找不到任何东西。有什么想法吗?THanks

response    = requests.post(f"{GATEWAY_URL}/certificate/download/format?gwsource={GATEWAY_SOURCE}", headers=headers, json=payload, verify="$(API_GATEWAY_CERT)")
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
filename = $(Pipeline.Workspace) + '/' + COMMONNAME + '_' + timestamp + '.cer'

with open(filename,  "w") as f:
f.write(response.text)

要在python中使用任何Azure DevOps变量,您需要使用os.environment["Name"]。根据文档,预定义的变量将转换为大写和任意"被替换为"_">

因此,为了在python中访问该值,您将使用以下内容:

import os
os.environ['PIPELINE_WORKSPACE']

您的用法有问题,请直接参考我的示例:

trigger:
- none
pool:
vmImage: ubuntu-latest
steps:
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
import requests
import datetime

#Get the repo of DevOps via REST API(zip format data, please ignore this step, this step only for get data.) 

url = "https://dev.azure.com/<Organization Name>/<Project Name>/_apis/git/repositories/<Repository Name>/items/items?path=/&versionDescriptor[versionOptions]=0&versionDescriptor[versionType]=0&versionDescriptor[version]=<Branch Name>&resolveLfs=true&$format=zip&api-version=5.0&download=true"

payload={}
headers = {
'Authorization': 'Basic <Personal Access Token>',
}

response = requests.request("GET", url, headers=headers, data=payload)

#############################################################################
#Save the zip file to current directory(The below logic is what you want.)
COMMONNAME = "TestResults"
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
filename = "$(Pipeline.Workspace)" + '/' + COMMONNAME + '_' + timestamp + '.zip'
with open(filename, "wb") as f:
f.write(response.content)
#Check whether the file exists.
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Pipeline.Workspace)'
artifact: 'drop'
publishLocation: 'pipeline'

最新更新