Skip to content

Commit b7df595

Browse files
committed
ci: fix retrieving version for publishing to GitHub
also extract helpers to a separate file
1 parent 7476c7e commit b7df595

File tree

4 files changed

+75
-64
lines changed

4 files changed

+75
-64
lines changed

build/Changelog.fs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Changelog
33
open System
44
open Fake.Core
55
open Fake.IO
6+
open Helpers
67

78
let isEmptyChange =
89
function
@@ -62,10 +63,9 @@ let mkReleaseNotes changelog (latestEntry : Changelog.ChangelogEntry) gitHubRepo
6263
{ latestEntry with Description = Some description }.ToString ()
6364

6465
let getVersionNumber envVarName ctx =
65-
let args = ctx.Context.Arguments
6666

6767
let verArg =
68-
args
68+
ctx.Context.Arguments
6969
|> List.tryHead
7070
|> Option.defaultWith (fun () -> Environment.environVarOrDefault envVarName "")
7171

@@ -100,21 +100,24 @@ let mutable changelogBackupFilename = ""
100100

101101
let updateChangelog changelogPath (changelog : Fake.Core.Changelog.Changelog) gitHubRepoUrl ctx =
102102

103-
let verStr = ctx |> getVersionNumber "RELEASE_VERSION"
103+
let newVersion =
104+
if isPublishToGitHub ctx then
105+
changelog.LatestEntry.SemVer
106+
else
107+
ctx |> getVersionNumber "RELEASE_VERSION" |> SemVer.parse
108+
104109

105110
let description, unreleasedChanges =
106111
match changelog.Unreleased with
107112
| None -> None, []
108113
| Some u -> u.Description, u.Changes
109114

110-
let newVersion = SemVer.parse verStr
111-
112115
changelog.Entries
113116
|> List.tryFind (fun entry -> entry.SemVer = newVersion)
114117
|> Option.iter (fun entry ->
115118
Trace.traceErrorfn
116119
"Version %s already exists in %s, released on %s"
117-
verStr
120+
newVersion.AsString
118121
changelogPath
119122
(if entry.Date.IsSome then
120123
entry.Date.Value.ToString ("yyyy-MM-dd")
@@ -128,7 +131,7 @@ let updateChangelog changelogPath (changelog : Fake.Core.Changelog.Changelog) gi
128131
|> Option.iter (fun entry ->
129132
Trace.traceErrorfn
130133
"You're trying to release version %s, but a later version %s already exists, released on %s"
131-
verStr
134+
newVersion.AsString
132135
entry.SemVer.AsString
133136
(if entry.Date.IsSome then
134137
entry.Date.Value.ToString ("yyyy-MM-dd")

build/Helpers.fs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module Helpers
2+
3+
open System
4+
open Fake.Core
5+
open Fake.DotNet
6+
open Fake.Tools
7+
8+
let releaseBranch = "main"
9+
10+
let environVarAsBoolOrDefault varName defaultValue =
11+
let truthyConsts = [ "1"; "Y"; "YES"; "T"; "TRUE" ]
12+
Environment.environVar varName
13+
|> ValueOption.ofObj
14+
|> ValueOption.map (fun envvar ->
15+
truthyConsts
16+
|> List.exists (fun ``const`` -> String.Equals (``const``, envvar, StringComparison.InvariantCultureIgnoreCase)))
17+
|> ValueOption.defaultValue defaultValue
18+
19+
let isRelease (targets : Target list) =
20+
targets
21+
|> Seq.map (fun t -> t.Name)
22+
|> Seq.exists ((=) "PublishToNuGet")
23+
24+
let invokeAsync f = async { f () }
25+
26+
let configuration (targets : Target list) =
27+
let defaultVal = if isRelease targets then "Release" else "Debug"
28+
29+
match Environment.environVarOrDefault "CONFIGURATION" defaultVal with
30+
| "Debug" -> DotNet.BuildConfiguration.Debug
31+
| "Release" -> DotNet.BuildConfiguration.Release
32+
| config -> DotNet.BuildConfiguration.Custom config
33+
34+
let failOnBadExitAndPrint (p : ProcessResult) =
35+
if p.ExitCode <> 0 then
36+
p.Errors |> Seq.iter Trace.traceError
37+
38+
failwithf "failed with exitcode %d" p.ExitCode
39+
40+
let isPublishToGitHub ctx = ctx.Context.FinalTarget = "PublishToGitHub"
41+
42+
type TargetParameter with
43+
44+
member ctx.IsPublishToGitHub = isPublishToGitHub ctx
45+
46+
let isCI = lazy environVarAsBoolOrDefault "CI" false
47+
48+
// CI Servers can have bizarre failures that have nothing to do with your code
49+
let rec retryIfInCI times fn =
50+
match isCI.Value with
51+
| true ->
52+
if times > 1 then
53+
try
54+
fn ()
55+
with _ ->
56+
retryIfInCI (times - 1) fn
57+
else
58+
fn ()
59+
| _ -> fn ()
60+
61+
let failOnWrongBranch () =
62+
if Git.Information.getBranchName "" <> releaseBranch then
63+
failwithf "Not on %s. If you want to release please switch to this branch." releaseBranch

build/build.fs

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ open Fake.Core.TargetOperators
99
open Fake.Api
1010
open Fake.BuildServer
1111
open Argu
12-
13-
let environVarAsBoolOrDefault varName defaultValue =
14-
let truthyConsts = [ "1"; "Y"; "YES"; "T"; "TRUE" ]
15-
Environment.environVar varName
16-
|> ValueOption.ofObj
17-
|> ValueOption.map (fun envvar ->
18-
truthyConsts
19-
|> List.exists (fun ``const`` -> String.Equals (``const``, envvar, StringComparison.InvariantCultureIgnoreCase)))
20-
|> ValueOption.defaultValue defaultValue
12+
open Helpers
2113

2214
//-----------------------------------------------------------------------------
2315
// Metadata and Configuration
@@ -69,7 +61,6 @@ let gitHubRepoUrl = $"https://github.com/%s{gitOwner}/%s{gitRepoName}"
6961

7062
let documentationRootUrl = $"https://%s{gitOwner}.github.io/%s{gitRepoName}"
7163

72-
let releaseBranch = "main"
7364
let readme = "README.md"
7465
let changelogFile = "CHANGELOG.md"
7566

@@ -96,53 +87,6 @@ let githubToken = Environment.environVarOrNone "GITHUB_TOKEN"
9687

9788
let nugetToken = Environment.environVarOrNone "NUGET_TOKEN"
9889

99-
//-----------------------------------------------------------------------------
100-
// Helpers
101-
//-----------------------------------------------------------------------------
102-
103-
104-
let isRelease (targets : Target list) =
105-
targets
106-
|> Seq.map (fun t -> t.Name)
107-
|> Seq.exists ((=) "PublishToNuGet")
108-
109-
let invokeAsync f = async { f () }
110-
111-
let configuration (targets : Target list) =
112-
let defaultVal = if isRelease targets then "Release" else "Debug"
113-
114-
match Environment.environVarOrDefault "CONFIGURATION" defaultVal with
115-
| "Debug" -> DotNet.BuildConfiguration.Debug
116-
| "Release" -> DotNet.BuildConfiguration.Release
117-
| config -> DotNet.BuildConfiguration.Custom config
118-
119-
let failOnBadExitAndPrint (p : ProcessResult) =
120-
if p.ExitCode <> 0 then
121-
p.Errors |> Seq.iter Trace.traceError
122-
123-
failwithf "failed with exitcode %d" p.ExitCode
124-
125-
126-
let isCI = lazy environVarAsBoolOrDefault "CI" false
127-
128-
// CI Servers can have bizarre failures that have nothing to do with your code
129-
let rec retryIfInCI times fn =
130-
match isCI.Value with
131-
| true ->
132-
if times > 1 then
133-
try
134-
fn ()
135-
with _ ->
136-
retryIfInCI (times - 1) fn
137-
else
138-
fn ()
139-
| _ -> fn ()
140-
141-
let failOnWrongBranch () =
142-
if Git.Information.getBranchName "" <> releaseBranch then
143-
failwithf "Not on %s. If you want to release please switch to this branch." releaseBranch
144-
145-
14690
module dotnet =
14791
let watch cmdParam program args = DotNet.exec cmdParam (sprintf "watch %s" program) args
14892

build/build.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<Compile Include="Helpers.fs" />
1415
<Compile Include="Changelog.fs" />
1516
<Compile Include="FsDocs.fs" />
1617
<Compile Include="build.fs" />

0 commit comments

Comments
 (0)