Skip to content

Commit b31ca4e

Browse files
committed
Use pipeline to set version
1 parent b19f71e commit b31ca4e

File tree

7 files changed

+159
-5
lines changed

7 files changed

+159
-5
lines changed

Steeltoe.NetCoreToolService.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2828
stylecop.json = stylecop.json
2929
Version.props = Version.props
3030
azure-pipelines.yaml = azure-pipelines.yaml
31+
setversion.sh = setversion.sh
3132
EndProjectSection
3233
EndProject
3334
Global

Version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<SteeltoeNetCoreToolServiceVersion>0.1.0</SteeltoeNetCoreToolServiceVersion>
4+
<SteeltoeNetCoreToolServiceVersion>0.0.0</SteeltoeNetCoreToolServiceVersion>
55
</PropertyGroup>
66

77
<PropertyGroup>

azure-pipelines.yaml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@ stages:
1919
- stage: assemble
2020
displayName: Assemble
2121
jobs:
22-
- job: test
23-
displayName: Test
22+
- job: build
23+
displayName: Build
2424
pool:
2525
vmImage: ubuntu-latest
2626
steps:
27+
- task: CmdLine@2
28+
displayName: 'Set Version'
29+
inputs:
30+
script: ./setversion.sh $(Build.BuildNumber)
2731
- task: UseDotNet@2
2832
displayName: 'Use .NET Core SDK 5.0'
2933
inputs:
3034
packageType: sdk
3135
version: 5.0.x
3236
- task: DotNetCoreCLI@2
33-
displayName: Unit Test
37+
displayName: dotnet build
38+
inputs:
39+
command: build
40+
arguments: --no-restore /p:TreatWarningsAsErrors=True
41+
- task: DotNetCoreCLI@2
42+
displayName: dotnet test
3443
inputs:
3544
command: test
45+
arguments: --no-build
3646
- job: kubernetes
3747
displayName: Kubernetes
3848
pool:

setversion.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
prog=$(basename $0)
6+
base_dir=$(cd $(dirname $0) && pwd)
7+
8+
if command -v gsed >/dev/null; then
9+
sed=gsed
10+
else
11+
sed=sed
12+
fi
13+
14+
usage() {
15+
cat <<EOF
16+
USAGE
17+
$prog [-h] version
18+
WHERE
19+
version
20+
Template version in <major>.<minor>.<patch> format.
21+
OPTIONS
22+
-h print this message
23+
DESCRIPTION
24+
Updates the NuGet spec and DevOps pipeline with the specified version.
25+
EOF
26+
}
27+
28+
die() {
29+
echo $* 2>&1
30+
exit 1
31+
}
32+
33+
while getopts ":h" opt ; do
34+
case $opt in
35+
h)
36+
usage
37+
exit
38+
;;
39+
\?)
40+
die "invalid option -$OPTARG; run with -h for help"
41+
;;
42+
:)
43+
die "option -$OPTARG requires an argument; run with -h for help"
44+
;;
45+
esac
46+
done
47+
shift $(($OPTIND - 1))
48+
49+
if [ $# -eq 0 ]; then
50+
die "version not specified; run with -h for help"
51+
fi
52+
53+
version=$(echo $1 | cut -d- -f1)
54+
shift
55+
56+
if [ $# -gt 0 ]; then
57+
die "too many args; run with -h for help"
58+
fi
59+
60+
major=$(echo $version | cut -d. -f1)
61+
minor=$(echo $version | cut -d. -f2)
62+
patch=$(echo $version | cut -d. -f3)
63+
64+
if [[ "$major.$minor.$patch" != "$version" ]]; then
65+
die "version not in major.minor.patch format"
66+
fi
67+
[[ $major =~ ^-?[0-9]+$ ]] || die "invalid major value: $major"
68+
[[ $minor =~ ^-?[0-9]+$ ]] || die "invalid minor value: $minor"
69+
[[ $patch =~ ^-?[0-9]+$ ]] || die "invalid patch value: $patch"
70+
71+
if ! $sed --version 2>/dev/null | grep 'GNU sed' >/dev/null; then
72+
die "This script requires GNU sed"
73+
fi
74+
75+
echo "Setting version to $major.$minor.$patch"
76+
77+
$sed -i 's:<SteeltoeNetCoreToolServiceVersion>.*</SteeltoeNetCoreToolServiceVersion>:<SteeltoeNetCoreToolServiceVersion>'$major.$minor.$patch'</SteeltoeNetCoreToolServiceVersion>:' $base_dir/Version.props
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Steeltoe.NetCoreToolService.Models
6+
{
7+
/// <summary>
8+
/// Application information, such as version.
9+
/// </summary>
10+
public sealed class About
11+
{
12+
/* ----------------------------------------------------------------- *
13+
* properties *
14+
* ----------------------------------------------------------------- */
15+
16+
/// <summary>
17+
/// Gets or sets the application name.
18+
/// </summary>
19+
public string Name { get; set; }
20+
21+
/// <summary>
22+
/// Gets or sets the application version.
23+
/// </summary>
24+
public string Version { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the application build source control commit ID.
28+
/// </summary>
29+
public string Commit { get; set; }
30+
}
31+
}

src/NetCoreToolService/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.Extensions.Hosting;
77
using Steeltoe.Extensions.Logging;
8+
using Steeltoe.NetCoreToolService.Models;
9+
using System.Reflection;
810

911
namespace Steeltoe.NetCoreToolService
1012
{
@@ -13,6 +15,34 @@ namespace Steeltoe.NetCoreToolService
1315
/// </summary>
1416
public class Program
1517
{
18+
static Program()
19+
{
20+
var versionAttr =
21+
typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
22+
var fields = versionAttr?.InformationalVersion.Split('+');
23+
if (fields is null)
24+
{
25+
fields = new[] { "unknown" };
26+
}
27+
28+
if (fields.Length == 1)
29+
{
30+
fields = new[] { fields[0], "unknown" };
31+
}
32+
33+
About = new About
34+
{
35+
Name = typeof(Program).Namespace ?? "unknown",
36+
Version = fields[0],
37+
Commit = fields[1],
38+
};
39+
}
40+
41+
/// <summary>
42+
/// Gets or sets "About" details, such as version.
43+
/// </summary>
44+
public static About About { get; set; }
45+
1646
/// <summary>
1747
/// Program entrypoint.
1848
/// </summary>

src/NetCoreToolService/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Hosting;
10+
using Microsoft.Extensions.Logging;
1011
using Microsoft.OpenApi.Models;
1112
using Steeltoe.Common.Utils.Diagnostics;
1213
using Steeltoe.Management.Endpoint;
@@ -56,8 +57,12 @@ public void ConfigureServices(IServiceCollection services)
5657
/// </summary>
5758
/// <param name="app">Injected IApplicationBuilder.</param>
5859
/// <param name="env">Injected IWebHostEnvironment.</param>
59-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
60+
/// <param name="logger">Injected ILogger.</param>
61+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
6062
{
63+
var about = Program.About;
64+
logger.LogInformation("{Program}, version {Version} [{Commit}]", about.Name, about.Version, about.Commit);
65+
6166
if (env.IsDevelopment())
6267
{
6368
app.UseDeveloperExceptionPage();

0 commit comments

Comments
 (0)