Skip to content

Commit

Permalink
ci: fix retrieving version for publishing to GitHub
Browse files Browse the repository at this point in the history
also extract helpers to a separate file
  • Loading branch information
xperiandri committed Nov 22, 2024
1 parent 7476c7e commit b7df595
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 64 deletions.
17 changes: 10 additions & 7 deletions build/Changelog.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Changelog
open System
open Fake.Core
open Fake.IO
open Helpers

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

let getVersionNumber envVarName ctx =
let args = ctx.Context.Arguments

let verArg =
args
ctx.Context.Arguments
|> List.tryHead
|> Option.defaultWith (fun () -> Environment.environVarOrDefault envVarName "")

Expand Down Expand Up @@ -100,21 +100,24 @@ let mutable changelogBackupFilename = ""

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

let verStr = ctx |> getVersionNumber "RELEASE_VERSION"
let newVersion =
if isPublishToGitHub ctx then
changelog.LatestEntry.SemVer
else
ctx |> getVersionNumber "RELEASE_VERSION" |> SemVer.parse


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

let newVersion = SemVer.parse verStr

changelog.Entries
|> List.tryFind (fun entry -> entry.SemVer = newVersion)
|> Option.iter (fun entry ->
Trace.traceErrorfn
"Version %s already exists in %s, released on %s"
verStr
newVersion.AsString
changelogPath
(if entry.Date.IsSome then
entry.Date.Value.ToString ("yyyy-MM-dd")
Expand All @@ -128,7 +131,7 @@ let updateChangelog changelogPath (changelog : Fake.Core.Changelog.Changelog) gi
|> Option.iter (fun entry ->
Trace.traceErrorfn
"You're trying to release version %s, but a later version %s already exists, released on %s"
verStr
newVersion.AsString
entry.SemVer.AsString
(if entry.Date.IsSome then
entry.Date.Value.ToString ("yyyy-MM-dd")
Expand Down
63 changes: 63 additions & 0 deletions build/Helpers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Helpers

open System
open Fake.Core
open Fake.DotNet
open Fake.Tools

let releaseBranch = "main"

let environVarAsBoolOrDefault varName defaultValue =
let truthyConsts = [ "1"; "Y"; "YES"; "T"; "TRUE" ]
Environment.environVar varName
|> ValueOption.ofObj
|> ValueOption.map (fun envvar ->
truthyConsts
|> List.exists (fun ``const`` -> String.Equals (``const``, envvar, StringComparison.InvariantCultureIgnoreCase)))
|> ValueOption.defaultValue defaultValue

let isRelease (targets : Target list) =
targets
|> Seq.map (fun t -> t.Name)
|> Seq.exists ((=) "PublishToNuGet")

let invokeAsync f = async { f () }

let configuration (targets : Target list) =
let defaultVal = if isRelease targets then "Release" else "Debug"

match Environment.environVarOrDefault "CONFIGURATION" defaultVal with
| "Debug" -> DotNet.BuildConfiguration.Debug
| "Release" -> DotNet.BuildConfiguration.Release
| config -> DotNet.BuildConfiguration.Custom config

let failOnBadExitAndPrint (p : ProcessResult) =
if p.ExitCode <> 0 then
p.Errors |> Seq.iter Trace.traceError

failwithf "failed with exitcode %d" p.ExitCode

let isPublishToGitHub ctx = ctx.Context.FinalTarget = "PublishToGitHub"

type TargetParameter with

member ctx.IsPublishToGitHub = isPublishToGitHub ctx

let isCI = lazy environVarAsBoolOrDefault "CI" false

// CI Servers can have bizarre failures that have nothing to do with your code
let rec retryIfInCI times fn =
match isCI.Value with
| true ->
if times > 1 then
try
fn ()
with _ ->
retryIfInCI (times - 1) fn
else
fn ()
| _ -> fn ()

let failOnWrongBranch () =
if Git.Information.getBranchName "" <> releaseBranch then
failwithf "Not on %s. If you want to release please switch to this branch." releaseBranch
58 changes: 1 addition & 57 deletions build/build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ open Fake.Core.TargetOperators
open Fake.Api
open Fake.BuildServer
open Argu

let environVarAsBoolOrDefault varName defaultValue =
let truthyConsts = [ "1"; "Y"; "YES"; "T"; "TRUE" ]
Environment.environVar varName
|> ValueOption.ofObj
|> ValueOption.map (fun envvar ->
truthyConsts
|> List.exists (fun ``const`` -> String.Equals (``const``, envvar, StringComparison.InvariantCultureIgnoreCase)))
|> ValueOption.defaultValue defaultValue
open Helpers

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

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

let releaseBranch = "main"
let readme = "README.md"
let changelogFile = "CHANGELOG.md"

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

let nugetToken = Environment.environVarOrNone "NUGET_TOKEN"

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------


let isRelease (targets : Target list) =
targets
|> Seq.map (fun t -> t.Name)
|> Seq.exists ((=) "PublishToNuGet")

let invokeAsync f = async { f () }

let configuration (targets : Target list) =
let defaultVal = if isRelease targets then "Release" else "Debug"

match Environment.environVarOrDefault "CONFIGURATION" defaultVal with
| "Debug" -> DotNet.BuildConfiguration.Debug
| "Release" -> DotNet.BuildConfiguration.Release
| config -> DotNet.BuildConfiguration.Custom config

let failOnBadExitAndPrint (p : ProcessResult) =
if p.ExitCode <> 0 then
p.Errors |> Seq.iter Trace.traceError

failwithf "failed with exitcode %d" p.ExitCode


let isCI = lazy environVarAsBoolOrDefault "CI" false

// CI Servers can have bizarre failures that have nothing to do with your code
let rec retryIfInCI times fn =
match isCI.Value with
| true ->
if times > 1 then
try
fn ()
with _ ->
retryIfInCI (times - 1) fn
else
fn ()
| _ -> fn ()

let failOnWrongBranch () =
if Git.Information.getBranchName "" <> releaseBranch then
failwithf "Not on %s. If you want to release please switch to this branch." releaseBranch


module dotnet =
let watch cmdParam program args = DotNet.exec cmdParam (sprintf "watch %s" program) args

Expand Down
1 change: 1 addition & 0 deletions build/build.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="Helpers.fs" />
<Compile Include="Changelog.fs" />
<Compile Include="FsDocs.fs" />
<Compile Include="build.fs" />
Expand Down

0 comments on commit b7df595

Please sign in to comment.