Display Azure DevOps pipeline variables inside my code - variables

Before, I was displaying my product version with :
#string.Format("v{0}", ViewContext.Controller.GetType().Assembly.GetName().Version)
where the version was stored in the AssemblyInfo.cs like so :
[assembly: AssemblyVersion("2.2.1.0")]
and the result was "v2.2.1"
Now, I'm using CI/CD with Azure DevOps and I'd like to display the $(Release.ReleaseName) instead of the AssemblyVersion.
How can I do that? Is it possible?

tag\update the assemblyinfo.cs with the $(release.releasename) variable value during the build. there is a special task for that, as far as I recall, something like this: https://marketplace.visualstudio.com/items?itemName=richardfennellBM.BM-VSTS-Versioning-Task
or just create a script that would do that for you

Display Azure DevOps pipeline variables inside my code
AFAIK, there is no out of box to way to update the AssemblyVersion by the value the $(Release.ReleaseName).
As workaround, just like 4c74356b41 pointed out, you could use the Assembly Info task to update the AssemblyInfo.cs:
Note:
Since you need use this task in the release pipeline, you need change the default value in the Source folder to $(System.DefaultWorkingDirectory).
The Release name format need match the Semantic Versioning Specification (SemVer)
Hope this helps.

Related

Azure Pipelines Set variable in script with string value that contains newline

I'm trying to build aspnetcore artifact for a solution with multiple web projects inside. Default netcore publish task has checkbox "Publish Web Projects" that finds all projects containing web.config or wwwroot folders, but my WebApi projects doesn't need them at all.
You can disable this checkbox and provide paths to projects you want to publish. Each project must be provided in separate line.
So, if I edit this setting in task properties window, everything is ok, cause there is multiline editor that behaves as expected. But I want to move list of projects to variables, were all other configuration is stored. However variable's editor is singleline.
I've decided to put project names in variable with ";" delimiter and write powershell task that properly constructs paths for publish task:
$projects = '$(PublishProjects)'
$result = ( $projects.Split(#(';'), [System.StringSplitOptions]::RemoveEmptyEntries) | %{ ('**\' + $_.Trim() + '.csproj') } ) -join "`n"
Write-Host "##vso[task.setvariable variable=PublishProjectsPath]$result"
Unfortunately this results in setting PublishProjectsPath`s value to only first project, cause all project paths except first one are printed on their own line without ##vso... and so azure ignores them effectively.
Is there any way to set pipeline variable from script correctly?
I cannot use azure-pipelines.yml cause azure doesn't support gitlab :(
Set variable in script with string value that contains newline
For this issue , I am afraid this is currently not support in azure devops. Although dotnet publish task has the textboxes that support multiple line content , but currently does not support multi-line string as the value of a variable.
Until now, in our official feature suggestion for Azure Devops forum, there has been a such suggestion exist in it: https://developercommunity.visualstudio.com/idea/365667/multiple-lines-variable-in-build-and-release.html.
You could vote that suggestion ticket and share your comment there,the product team would provide the updates if they view it. Anyone interested in this can vote for it and track it.
Yous set the variable as job-scoped.
You need to set a multi job output variable with ##vso[task.setvariable variable=myOutputVar;isOutput=true]

Increment Version number when checking in to TFS

I have a folder in TFS which has SQL Scripts. At the moment I am manually adding a comment and updating a version number inside the comment every time i make a change and check it back it. This works however was hoping there might be a better way. Is there a way to automate this in TFS?
I have read the following article
Version control project files
do i have to go through such a process for simple .sql files? Are there any other simple ways.
There are a few ways you can do this:
Create an automated build in TFS and write a custom build step / PowerShell script to parse the appropriate SQL scripts, read the version, increment it, and store the new version by either checking in the updated file or a local store
Use a database project (part of SQL Server Data Tools) which will output a DACPAC. Inside the database project, you can set the version as specified here. This stores the version in the project file. If you update your TFS build number to be digits only, you can then update the project file to set that value to match the build using a custom build task. For example, if your build number was yyyy.m.d.R where R is the number of times that build was run today (TFS manages that - it's the revision variable). Or, you could set the the <DacVersion> tag to something like 2.1.0.0 and your build replaces the last digit with yyyymmddr.
I'd recommend using a database project. It's pretty easy to create a new database project off an existing database.
The first way mentioned by Jacob above can achieve that if you just want to incremental the version number of the script/folder, just create a CI build definition.
Actually you can just enable Label sources and set the Label format with predefined environment variables such as $(build.buildNumber), and set without publish any artifacts during build process.
Thus, it will automatically trigger the CI build when you check in files, and the source (SQL Script /folder) will be labeled with the incremental number.
Then you can find the specific versions with the label.

How to permanently save a build variable as a build step?

I have the following variables defined:
Now once a build is complete (the last step in the build process), I want to update the VersionRevision variable, basically increment it.
So I'm looking for an API I can call from C# and create a console application or a powershell script to edit the build definition (if I have to do this)?
You can use VSTS Rest API to update the variable value in Build Definition. Both Console Application and Powershell Script is OK for this.
If I understand correctly, you want to get these build variables and them assignment them as your version number.
After the build completes, update and increment the VersionRevision. It's not a good way and seems not available to achieve it.
In TFS build there is a $(Rev:.r) which means
Use $(Rev:.rr) to ensure that every completed build has a unique name.
When a build is completed, if nothing else in the build number has
changed, the Rev integer value is incremented by one.
Source: Specify general build definition settings
To version your assemblies you could just add an powershell script in your build definition, detail ways to achieve please follow this link from MSDN: Version your assemblies
And usually we only define and assignment variables with the Major and Minor version. If you want to change the value of them. You may need manually edit the build definition.
More related link about how to manage version numbers as part of your vNext builds.
vNext Build Awesomeness – Managing Version Numbers
Generate custom build numbers in TFS Build vNext

How to skip Ivy publish without causing error?

I would like to skip publishing an artifact if it already exists in the repository, but as far as I can see from the documentation there isn't a way to do this. There is an overwrite attribute, but if set to false that causes the publish to fail if the artifact exists. I definitely don't want to overwrite the artifact, either.
I've looked into using <ivy:info> and <ivy:findrevision> to see if the artifact exists and set a property I can use on my publish target (as an unless attribute, for example), but neither of these tasks allows me to specify the repository to check.
I'd rather not resort to using an external taskdef, like antcontrib's try/catch tasks.
Does anyone have any other suggestions?
Info and findrevision allow the settingsRef-attribute. So you could use an extra settings-file that only references the resolver you need (via ivy:settings and ivy:configure) and use that settingsRef in your task.
Why would you run the "publish" task if you don't intend saving what you built?
I use the buildnumber task to ensure that my version number is incremented automatically based on what was previously published.

CruiseControl.NET Set Variable to a dynamic value

is there any plug-in or other possibility to set an environment variable in CC.NET 1.4.2 to some generated value. I would like to pass to MSBuild some random value (can be a time stamp where to put some build reports). Afterwords all the generated report files from the randomly named dir will be merged to cc.net report.
The problem here is that I can't use the CCNetBuildDate + CCNetBuildTime environment variables, due to the format of CCNetBuildTime (HH:mm:ss), because : is not a valid character for directory name. I could use them if CC.NET supports ':' replacement by some other char (e.g. '-').
I can use MSBuild community task to create the output directory with the help of <Time>-task, the problem is that I don't know how to return to CCNet in which random dir the reports were produced.
I can't use the labeller either, because we have rewritten the labeller and it always returns the dummy label (I know that is very bad and changes ccnet logic, but currently I have no choice).
I can write a plug-in, but I would like to use as much default technologies as possible.
Many thanks,
Ovanes
Can't you just produce the report files in the normal project working directory and merge them from there? Every other external reporting tool works this way.