I was thinking that I need to see the date of last CD – who done what. For this,I need 2 things: to have a controller/gui to show the info and the CD process,via GitHub/AzureDevOps,to take care of that.
For the part with code,the problem was pretty simple:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace InfoValutarWebAPI.Controllers
{
/// <summary>
/// info about commit
/// </summary>
public class LastCommitInfo
{
/// <summary>
/// comment latest commit
/// </summary>
public string LatestCommit { get; set; }
/// <summary>
/// last date of commit
/// </summary>
public DateTime DateCommit { get; set; }
/// <summary>
/// last author of commit
/// </summary>
public string LastAuthor { get; set; }
}
/// <summary>
/// controller about info the application
/// </summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/rates")]
public class InfoController
{
/// <summary>
/// info about latest commit
/// </summary>
/// <returns></returns>
public LastCommitInfo GetLatestCommit()
{
return
new LastCommitInfo()
{
LatestCommit = "{LatestCommit}",
DateCommit = DateTime.ParseExact("{DateCommit}","yyyyMMdd:HHmmss",null),
LastAuthor = "{LastAuthor}"
}
;
}
}
}
What about the CD process ?
Well,this was cumbersome. To see ALL the environment variables,I used cmd /K set ( in command ) or Get-ChildItem Env: ( in powershell).
And I come with this:
A bash script to take the version
– bash: |
git log –format=’%s’ -1
git log –pretty=oneline | head -1
gitMessage=$(git log –format=’%s’ -1)
echo “##vso[task.setvariable variable=commitMessage;isOutput=true]$gitMessage”
displayName: Store commit message in variable
– powershell: .\modifyinfo.ps1
displayName: modify info
And a .ps1 powershell
$file = “.\InfoValutar\InfoValutarWebAPI\Controllers\InfoController.cs”
$date = Get-Date -Format “yyyyMMdd:HHmmss”
Get-ChildItem Env:
$author= $Env:BUILD_SOURCEVERSIONAUTHOR
$commitText = $env:BASH_COMMITMESSAGE
((Get-Content -path $file -Raw) -replace ‘{LatestCommit}’,$commitText -replace ‘{LastAuthor}’,$author -replace ‘{DateCommit}’,$date ) | Set-Content -Path $file
(Get-Content -path $file -Raw)
The result can be seen at https://infovalutar.azurewebsites.net/api/v1.0/info
Leave a Reply