From e3c14b33ae0f39fdb1b99e761118b29a9ca9e7d3 Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Sun, 16 Oct 2022 12:03:40 +0800 Subject: [PATCH] CI: package backend as nuget library 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. --- .github/workflows/CI.yml | 38 ++++++ GWallet.Backend.nuspec | 63 ++++++++++ scripts/.disabled-gitlab-ci.yml | 11 ++ scripts/bump.fsx | 22 ++-- scripts/nugetPush.fsx | 209 ++++++++++++++++++++++++++++++++ 5 files changed, 334 insertions(+), 9 deletions(-) create mode 100644 GWallet.Backend.nuspec create mode 100755 scripts/nugetPush.fsx diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index f912b62d0..889fb6941 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -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: @@ -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 diff --git a/GWallet.Backend.nuspec b/GWallet.Backend.nuspec new file mode 100644 index 000000000..c98cfe4d8 --- /dev/null +++ b/GWallet.Backend.nuspec @@ -0,0 +1,63 @@ + + + + GWallet.Backend + + 0.0.0.0 + GWallet.Backend + knocte + NodeEffect + false + 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. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/scripts/.disabled-gitlab-ci.yml b/scripts/.disabled-gitlab-ci.yml index 7a86f4fd6..43bcbab2d 100644 --- a/scripts/.disabled-gitlab-ci.yml +++ b/scripts/.disabled-gitlab-ci.yml @@ -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 # diff --git a/scripts/bump.fsx b/scripts/bump.fsx index 7363276f2..8b2b2cfe9 100755 --- a/scripts/bump.fsx +++ b/scripts/bump.fsx @@ -42,22 +42,26 @@ let filesToBumpMiniVersion: seq = [ ] :> seq +let isGitLabCiDisabled = true +let gitLabCiYml = + if isGitLabCiDisabled then + "scripts/.disabled-gitlab-ci.yml" + else + ".gitlab-ci.yml" + let filesToBumpFullVersion: seq = 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 = - if not isGitLabCiDisabled then - Seq.append filesToBumpFullVersion [ - gitLabCiYml - ] - else - filesToBumpFullVersion + filesToBumpFullVersion let replaceScript = Path.Combine(rootDir.FullName, "scripts", "fsx", "Tools", "replace.fsx") diff --git a/scripts/nugetPush.fsx b/scripts/nugetPush.fsx new file mode 100755 index 000000000..051166315 --- /dev/null +++ b/scripts/nugetPush.fsx @@ -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] " + 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 = + let nuspecFiles = rootDir.EnumerateFiles "*.nuspec" + if nuspecFiles.Any() then + if args.Length < 1 then + Console.Error.WriteLine "Usage: nugetPush.fsx [baseVersion] " + 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] " + 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