Skip to content

Commit

Permalink
CI: package backend as nuget library
Browse files Browse the repository at this point in the history
We use this debug package to test our watchtower software on
RegTest.

This commit copies nugetPush.fsx from
88abce82fefee4aa7f1fb451bee5fd3235f8fa82 of nblockchain/fsx
repo.

The .nuspec file was generated from packages.config using "nuget
pack GWallet.Backend.fsproj" and opening the resulting nupkg
file and extracting the generated nuspec file in it.
  • Loading branch information
aarani authored and knocte committed Oct 16, 2022
1 parent 308c0c2 commit e3c14b3
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 9 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ jobs:
- name: Validate current commit (last commit) with commitlint
run: npx commitlint --from HEAD~1 --to HEAD --verbose


snap_pkg:

needs:
Expand Down Expand Up @@ -370,3 +371,40 @@ jobs:
sudo apt update
sudo apt install --yes fsharp
./scripts/snap_release.sh
nuget_pkg:

needs:
# in theory this could run in parallel to snap_pkg, but then we would need to duplicate
# all the deps of snap_pkg below, so we prefer better DRY than faster pipelines
- snap_pkg

runs-on: ubuntu-20.04
container:
image: "ubuntu:20.04"

env:
DEFAULT_REF_TO_NUGET_PUSH: "*lightning*"
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

steps:
- uses: actions/checkout@v1
- name: install sudo
run: apt update && apt install --yes sudo
- name: install dependencies
run: |
sudo apt install -y make git
# needed to download NuGet
sudo apt install -y curl
./scripts/install_mono_from_microsoft_deb_packages.sh
# workaround for https://github.com/actions/runner/issues/2033
- name: ownership workaround
run: git config --global --add safe.directory '*'

- name: build
run: |
./configure.sh
make
- name: generate & publish nuget package
run: ./scripts/nugetPush.fsx 0.3.325.0 $NUGET_API_KEY
63 changes: 63 additions & 0 deletions GWallet.Backend.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>GWallet.Backend</id>
<!-- nugetPush will not use this version number -->
<version>0.0.0.0</version>
<title>GWallet.Backend</title>
<authors>knocte</authors>
<owners>NodeEffect</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>geewallet is a minimalistic and pragmatist crossplatform lightweight opensource brainwallet for people that want to hold the most important cryptocurrencies in the same application with ease and peace of mind.</description>
<dependencies>
<dependency id="DotNetLightning.Kiss" version="1.1.2-date20210528-0835-git-0c83757" />
<dependency id="FSharp.Core" version="4.7.0" />
<dependency id="FSharp.Data" version="3.0.0" />
<dependency id="HtmlAgilityPack" version="1.11.24" />
<dependency id="Microsoft.Bcl.AsyncInterfaces" version="5.0.0" />
<dependency id="Microsoft.Extensions.Logging.Abstractions" version="1.0.2" />
<dependency id="Microsoft.VisualStudio.Threading" version="16.9.60" />
<dependency id="Microsoft.VisualStudio.Validation" version="16.8.33" />
<dependency id="NBitcoin" version="5.0.75" />
<dependency id="NBitcoin.Altcoins" version="2.0.5.1" />
<dependency id="Nethereum" version="0.95.0--date20210125-0551.git-05a29e9" />
<dependency id="Newtonsoft.Json" version="12.0.2" />
<dependency id="Portable.BouncyCastle" version="1.8.6.7" />
<dependency id="SharpRaven" version="2.4.0" />
<dependency id="StreamJsonRpc" version="2.8.21" />
<dependency id="System.Buffers" version="4.5.1" />
<dependency id="System.Collections" version="4.3.0" />
<dependency id="System.Collections.Concurrent" version="4.3.0" />
<dependency id="System.Collections.Immutable" version="5.0.0" />
<dependency id="System.Diagnostics.Debug" version="4.3.0" />
<dependency id="System.Diagnostics.DiagnosticSource" version="5.0.1" />
<dependency id="System.Globalization" version="4.3.0" />
<dependency id="System.IO.Pipelines" version="5.0.1" />
<dependency id="System.IO.Pipes" version="4.3.0" />
<dependency id="System.Linq" version="4.3.0" />
<dependency id="System.Linq.Expressions" version="4.3.0" />
<dependency id="System.Memory" version="4.5.4" />
<dependency id="System.Net.Http" version="4.3.4" />
<dependency id="System.Net.WebSockets.Client" version="4.3.2" />
<dependency id="System.Numerics.Vectors" version="4.5.0" />
<dependency id="System.ObjectModel" version="4.3.0" />
<dependency id="System.Reflection" version="4.3.0" />
<dependency id="System.Reflection.Emit" version="4.7.0" />
<dependency id="System.Reflection.Emit.Lightweight" version="4.6.0" />
<dependency id="System.Reflection.Extensions" version="4.3.0" />
<dependency id="System.Reflection.Primitives" version="4.3.0" />
<dependency id="System.Resources.ResourceManager" version="4.3.0" />
<dependency id="System.Runtime" version="4.3.0" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" />
<dependency id="System.Runtime.Extensions" version="4.3.0" />
<dependency id="System.Runtime.InteropServices" version="4.3.0" />
<dependency id="System.Threading" version="4.3.0" />
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="Xamarin.Essentials" version="1.0.0" />
</dependencies>
</metadata>
<files>
<file src="src\GWallet.Backend\bin\Debug\GWallet.Backend.dll" target="lib\net472" />
<file src="src\GWallet.Backend\bin\Debug\GWallet.Backend.xml" target="lib\net472" />
</files>
</package>
11 changes: 11 additions & 0 deletions scripts/.disabled-gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ newmono_test_end2end:

- time (./configure.sh && make && make check-end2end)

newmono_nuget:
image: $CI_REGISTRY_IMAGE/new_env:$CI_COMMIT_SHA
stage: package
variables:
DEFAULT_REF_TO_NUGET_PUSH: "*lightning*"
script:
- ./scripts/install_mono_from_microsoft_deb_packages.sh
- ./configure.sh
- make
- ./scripts/nugetPush.fsx 0.3.325.0 $NUGET_API_KEY

#
# Disabled snap generation because of https://bugs.launchpad.net/snappy/+bug/1660957/comments/2
#
Expand Down
22 changes: 13 additions & 9 deletions scripts/bump.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,26 @@ let filesToBumpMiniVersion: seq<string> =
[
] :> seq<string>

let isGitLabCiDisabled = true
let gitLabCiYml =
if isGitLabCiDisabled then
"scripts/.disabled-gitlab-ci.yml"
else
".gitlab-ci.yml"

let filesToBumpFullVersion: seq<string> =
Seq.append filesToBumpMiniVersion [
"src/GWallet.Backend/Properties/CommonAssemblyInfo.fs"
"snap/snapcraft.yaml"
".github/workflows/CI.yml"
gitLabCiYml
]

let isGitLabCiDisabled = true

let gitLabCiYml = ".gitlab-ci.yml"
// filesToGitAdd used to be different from filesToBumpFullVersion because we
// changed the expiry date of some artifacts while we didn't bump any version
// in the gitlab pipeline YML file, now we change a version too so they're same
let filesToGitAdd: seq<string> =
if not isGitLabCiDisabled then
Seq.append filesToBumpFullVersion [
gitLabCiYml
]
else
filesToBumpFullVersion
filesToBumpFullVersion

let replaceScript =
Path.Combine(rootDir.FullName, "scripts", "fsx", "Tools", "replace.fsx")
Expand Down
209 changes: 209 additions & 0 deletions scripts/nugetPush.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#!/usr/bin/env fsharpi

open System
open System.IO
open System.Linq

#r "System.Configuration"
open System.Configuration
#load "./InfraLib/Misc.fs"
#load "./InfraLib/Process.fs"
#load "./InfraLib/Network.fs"
#load "./InfraLib/Git.fs"
open FSX.Infrastructure
open Process

let PrintUsage() =
Console.Error.WriteLine "Usage: nugetPush.fsx [--output-version] [baseVersion] <nugetApiKey>"
Environment.Exit 1

let args = Misc.FsxArguments()
if args.Length > 3 then
PrintUsage ()
if args.Length > 2 && args.[0] <> "--output-version" then
PrintUsage ()

let rootDir =
DirectoryInfo (
Path.Combine (
__SOURCE_DIRECTORY__, // scripts/
".." // root
)
)

// this is a translation of doing this in unix (assuming initialVersion="0.1.0"):
// 0.1.0--date`date +%Y%m%d-%H%M`.git-`git rev-parse --short=7 HEAD`
let GetIdealNugetVersion (inputVersion: string) =
let initialVersion =
let versionSplit =
inputVersion.Split '.'

if versionSplit.Length = 4 && versionSplit.[3] = "0" then
String.Join(".", versionSplit.Take 3)
else
inputVersion

let dateSegment = sprintf "date%s" (DateTime.UtcNow.ToString "yyyyMMdd-hhmm")

let gitHash = Git.GetLastCommit()
if null = gitHash then
Console.Error.WriteLine "Not in a git repository?"
Environment.Exit 2

let gitHashDefaultShortLength = 7
let gitShortHash = gitHash.Substring(0, gitHashDefaultShortLength)
let gitSegment = sprintf "git-%s" gitShortHash
let finalVersion = sprintf "%s--%s.%s"
initialVersion dateSegment gitSegment
finalVersion

let IsDotNetSdkInstalled() =
try
let dotnetVersionCmd =
{
Command = "dotnet"
Arguments = "--version"
}
Process.SafeExecute (dotnetVersionCmd, Echo.All) |> ignore
true
with
:? ProcessCouldNotStart -> false

let EnsureNugetExists() =
let nugetTargetDir = Path.Combine(rootDir.FullName, ".nuget") |> DirectoryInfo
if not nugetTargetDir.Exists then
nugetTargetDir.Create()
let prevCurrentDir = Directory.GetCurrentDirectory()
Directory.SetCurrentDirectory nugetTargetDir.FullName
let nugetDownloadUri = Uri "https://dist.nuget.org/win-x86-commandline/v4.5.1/nuget.exe"
Network.DownloadFile nugetDownloadUri |> ignore
let nugetExe = Path.Combine(nugetTargetDir.FullName, "nuget.exe") |> FileInfo
Directory.SetCurrentDirectory prevCurrentDir

nugetExe

let FindOrGenerateNugetPackages (): seq<FileInfo> =
let nuspecFiles = rootDir.EnumerateFiles "*.nuspec"
if nuspecFiles.Any() then
if args.Length < 1 then
Console.Error.WriteLine "Usage: nugetPush.fsx [baseVersion] <nugetApiKey>"
Environment.Exit 1
let baseVersion = args.First()

seq {
for nuspecFile in nuspecFiles do
let packageName = Path.GetFileNameWithoutExtension nuspecFile.FullName

let nugetVersion = GetIdealNugetVersion baseVersion

// we need to download nuget.exe here because `dotnet pack` doesn't support using standalone (i.e.
// without a project association) .nuspec files, see https://github.com/NuGet/Home/issues/4254

let nugetPackCmd =
{
Command = EnsureNugetExists().FullName
Arguments = sprintf "pack %s -Version %s"
nuspecFile.FullName nugetVersion
}

Process.SafeExecute (nugetPackCmd, Echo.All) |> ignore
yield FileInfo (sprintf "%s.%s.nupkg" packageName nugetVersion)
}
else
let FindNugetPackages() =
rootDir.Refresh()
rootDir.EnumerateFiles("*.nupkg", SearchOption.AllDirectories)

if not (FindNugetPackages().Any()) then
if args.Length < 1 then
Console.Error.WriteLine "Usage: nugetPush.fsx [baseVersion] <nugetApiKey>"
Environment.Exit 1
let baseVersion = args.First()
let nugetVersion = GetIdealNugetVersion baseVersion

if IsDotNetSdkInstalled() then
let dotnetPackCmd =
{
Command = "dotnet"
Arguments = sprintf "pack -c Release -p:Version=%s"
nugetVersion
}
Process.SafeExecute (dotnetPackCmd, Echo.All) |> ignore
else
failwith "Please install .NET SDK to build nuget packages without nuspec file"

FindNugetPackages()


let NugetUpload (packageFile: FileInfo) (nugetApiKey: string) =

let defaultNugetFeedUrl = "https://api.nuget.org/v3/index.json"
if IsDotNetSdkInstalled() then
let nugetPushCmd =
{
Command = "dotnet"
Arguments = sprintf "nuget push %s -k %s -s %s"
packageFile.FullName nugetApiKey defaultNugetFeedUrl
}
Process.SafeExecute (nugetPushCmd, Echo.All) |> ignore
else
let nugetPushCmd =
{
Command = EnsureNugetExists().FullName
Arguments = sprintf "push %s -ApiKey %s -Source %s"
packageFile.FullName nugetApiKey defaultNugetFeedUrl
}
Process.SafeExecute (nugetPushCmd, Echo.All) |> ignore

if args.Length > 0 && args.[0] = "--output-version" then
if args.Length < 2 then
Console.Error.WriteLine "When using --output-version, pass the base version as the second argument"
Environment.Exit 4
let baseVersion = args.[1]
Console.WriteLine (GetIdealNugetVersion baseVersion)
Environment.Exit 0

let nugetPkgs = FindOrGenerateNugetPackages () |> List.ofSeq
if not (nugetPkgs.Any()) then
Console.Error.WriteLine "No nuget packages found or generated"
Environment.Exit 3

if args.Length < 1 then
Console.Error.WriteLine "nugetApiKey argument was not passed to the script (running in a fork?), skipping upload..."
Environment.Exit 0
let nugetApiKey = args.Last()

let GetCurrentRef(): string =
let githubRef = Environment.GetEnvironmentVariable "GITHUB_REF"
// https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
let gitlabRef = Environment.GetEnvironmentVariable "CI_COMMIT_REF_NAME"

if githubRef <> null then
githubRef
elif gitlabRef <> null then
gitlabRef
else
Git.GetCurrentBranch()

let IsMasterBranch(): bool =
let branch = GetCurrentRef()
branch = "master" || branch = "refs/heads/master"

let IsDefaultRefToPush(): bool =
let defaultRefToPushOpt = Environment.GetEnvironmentVariable "DEFAULT_REF_TO_NUGET_PUSH" |> Option.ofObj
match defaultRefToPushOpt with
| Some defaultRefToPush ->
if (defaultRefToPush.StartsWith "*") && (defaultRefToPush.EndsWith "*") then
let defaultRefWithoutWildCard =
defaultRefToPush.Substring(1, defaultRefToPush.Count() - 2)
GetCurrentRef().Contains defaultRefWithoutWildCard
else
GetCurrentRef() = defaultRefToPush
| None -> IsMasterBranch()

if not (IsDefaultRefToPush()) then
Console.WriteLine "Branch is not default branch to push, skipping upload..."
Environment.Exit 0

for nugetPkg in nugetPkgs do
NugetUpload nugetPkg nugetApiKey

0 comments on commit e3c14b3

Please sign in to comment.