问题
When doing my CI on Azure DevOps with the YAML file format, I'm trying to read my csproj files to extract informations about version to create NuGet packages that will follow the csproj version.
I'm using NuGetCommand@2
for packaging (because of versioningScheme: byPrereleaseNumber
)
Is there a built-in task or a way to extract this info from csproj file as a regex and pass them to NuGetCommand@2
?
回答1:
I'm fairly sure there is nothing built-in for this, but you can use any scripting language you like to parse the file and "spit out" whatever you need as a build variable and consume it later on. here's what I've been doing:
- script: echo "##vso[task.setvariable variable=dp]$(cat $(Build.Repository.LocalPath)/deployment/dp)"
- script: az group delete -n $(dp)-k8s -y --no-wait
you can obviously do any custom thing in the script step and consume the result in anyway you like to.
https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md
回答2:
You can add this as a first step to extract the tag of a csproj :
- script: echo "##vso[task.setvariable variable=version]$(cat 'ProjectDirectory/MyProject.csproj' | grep -oP '(?<=<Version>).*?(?=<\/Version>)')"
You will then be able to use it by calling $(version)
anywhere in your yaml.
Explanation :
echo "##vso[task.setvariable variable=version]
means "set a variable that I can use after"cat 'ProjectDirectory/MyProject.csproj' | grep
gets your csproj content then pass it to grep so we can exec a regular expression on it(?<=<Version>).*?(?=<\/Version>)')
is a regular expression that finds the<Version>x.x.x</Version>
tag without consuming the tags so it only prints what is inside
来源:https://stackoverflow.com/questions/54541654/is-there-a-way-to-read-file-from-azure-devops-yaml