Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploying new releases using octokit #905 #933

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Octokit.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#I __SOURCE_DIRECTORY__
#r "./Octokit/bin/Release/Net45/Octokit.dll"

open Octokit
open System
open System.IO

type Draft =
{ Client : GitHubClient
Owner : string
Project : string
DraftRelease : Release }

let private isRunningOnMono = System.Type.GetType ("Mono.Runtime") <> null

let rec private retry count asyncF =
// This retry logic causes an exception on Mono:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be working on Mono master - is there a specific build we should be using here to ensure this works on Mono, or is it too early to rely on this fix being present?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isRunningOnMono is used for SourceLink . Sourcelink is excluded on mono https://github.com/fsharp/FAKE/blob/master/build.fsx#L304

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

// https://github.com/fsharp/fsharp/issues/440
if isRunningOnMono then
asyncF
else
async {
try
return! asyncF
with _ when count > 0 -> return! retry (count - 1) asyncF
}

let createClient user password =
async {
let github = new GitHubClient(new ProductHeaderValue("Octokit"))
github.Credentials <- Credentials(user, password)
return github
}

let createClientWithToken token =
async {
let github = new GitHubClient(new ProductHeaderValue("Octokit"))
github.Credentials <- Credentials(token)
return github
}

let private makeRelease draft owner project version prerelease (notes:seq<string>) (client : Async<GitHubClient>) =
async {
let data = new NewRelease(version)
data.Name <- version
data.Body <- String.Join(Environment.NewLine, notes)
data.Draft <- draft
data.Prerelease <- prerelease
let! client' = client
let! draft = Async.AwaitTask <| client'.Release.Create(owner, project, data)
let draftWord = if data.Draft then " draft" else ""
printfn "Created%s release id %d" draftWord draft.Id
return { Client = client'
Owner = owner
Project = project
DraftRelease = draft }
} |> retry 5

let createDraft owner project version prerelease notes client = makeRelease true owner project version prerelease notes client
let createRelease owner project version prerelease notes client = makeRelease false owner project version prerelease notes client

let uploadFile fileName (draft : Async<Draft>) =
async {
let fi = FileInfo(fileName)
let archiveContents = File.OpenRead(fi.FullName)
let assetUpload = new ReleaseAssetUpload(fi.Name,"application/octet-stream",archiveContents,Nullable<TimeSpan>())
let! draft' = draft
let! asset = Async.AwaitTask <| draft'.Client.Release.UploadAsset(draft'.DraftRelease, assetUpload)
printfn "Uploaded %s" asset.Name
return draft'
} |> retry 5

let releaseDraft (draft : Async<Draft>) =
async {
let! draft' = draft
let update = draft'.DraftRelease.ToUpdate()
update.Draft <- Nullable<bool>(false)
let! released = Async.AwaitTask <| draft'.Client.Release.Edit(draft'.Owner, draft'.Project, draft'.DraftRelease.Id, update)
printfn "Released %d on github" released.Id
} |> retry 5
15 changes: 15 additions & 0 deletions deploy.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off

"tools\nuget\nuget.exe" "install" "FAKE.Core" "-OutputDirectory" "tools" "-ExcludeVersion" "-version" "4.4.2"

:Build
cls

SET TARGET="ReleaseGitHub"
SET GITOWNER="octokit"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be named GITHUBOWNER to indicate this is specific to the GitHub API (rather than the Git repository)?

And octokit is an organization, so this is just going to fail miserably with the default. Not sure what to put here, so let's leave it empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see it's also used to find the right remote (I have both my fork and the upstream octokit repos tracked as remotes, so perhaps this needs to be two variables?

IF NOT [%1]==[] (set GITOWNER="%1")

"tools\FAKE.Core\tools\Fake.exe" "deploy.fsx" "target=%TARGET%" "gitOwner=%GITOWNER%"

:Quit
exit /b %errorlevel%
55 changes: 55 additions & 0 deletions deploy.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#r @"tools/FAKE.Core/tools/FakeLib.dll"
#load "Octokit.fsx"
open Fake
open Fake.Git
open System
open Octokit

let authors = ["GitHub"]

// project name
let gitOwner = getBuildParam "gitOwner"
let gitHome = "https://github.com/" + gitOwner
let projectName = "Octokit"

// The name of the project on GitHub
let gitName = "octokit.net"

// directories
let packagingRoot = "./packaging/"

let releaseNotes =
ReadFile "ReleaseNotes.md"
|> ReleaseNotesHelper.parseReleaseNotes


Target "ReleaseGitHub" (fun _ ->
let user = getBuildParam "gitOwner"
traceFAKE "The git owner is %s" user
let pw =
match getBuildParam "github-pw" with
| s when not (String.IsNullOrWhiteSpace s) -> s
| _ -> getUserPassword "Password: "
let remote =
Git.CommandHelper.getGitResult "" "remote -v"
|> Seq.filter (fun (s: string) -> s.EndsWith("(push)"))
|> Seq.tryFind (fun (s: string) -> s.Contains(gitOwner + "/" + gitName))
|> function None -> gitHome + "/" + gitName | Some (s: string) -> s.Split().[0]

StageAll ""
Git.Commit.Commit "" (sprintf "Bump version to %s" releaseNotes.NugetVersion)
Branches.pushBranch "" remote (Information.getBranchName "")

Branches.tag "" releaseNotes.NugetVersion
Branches.pushTag "" remote releaseNotes.NugetVersion

// release on github
createClient user pw
|> createDraft gitOwner gitName releaseNotes.NugetVersion (releaseNotes.SemVer.PreRelease <> None) releaseNotes.Notes
|> uploadFile (sprintf "./packaging/Octokit.%s.nupkg" releaseNotes.AssemblyVersion)
|> uploadFile (sprintf "./packaging/Octokit.Reactive.%s.nupkg" releaseNotes.AssemblyVersion)
|> releaseDraft
|> Async.RunSynchronously
)

RunTargetOrDefault "ReleaseGitHub"