Skip to content

Commit

Permalink
Add GitLab pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
David Nelson committed Jun 17, 2024
1 parent 8af0c71 commit 905d526
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ _ReSharper.Caches/

/artifacts
*.binlog

# GitLab
.idea/
.gitlab/packages
**/TestResults.xml
39 changes: 39 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variables:
BASE_IMAGE_VERSION: ${CI_COMMIT_REF_SLUG}-${CI_PIPELINE_ID}
DOCKER_VERSION: 24.0.5

workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_TAG
- when: never

build-base:
image: docker:${DOCKER_VERSION}
services:
- docker:${DOCKER_VERSION}-dind
script:
- echo "$CI_REGISTRY_PASSWORD" | docker login $CI_REGISTRY -u $CI_REGISTRY_USER --password-stdin
- docker buildx build -f .gitlab/Dockerfile-build -t ${CI_REGISTRY_IMAGE}/base:${BASE_IMAGE_VERSION} --push .gitlab

build:
needs:
- build-base
image: ${CI_REGISTRY_IMAGE}/base:${BASE_IMAGE_VERSION}
script:
- ./build.sh Compile Test Pack --configuration Release
artifacts:
paths:
- artifacts/*.nupkg
- artifacts/*.snupkg
reports:
junit: "**/TestResults.xml"

publish:
image: ${CI_REGISTRY_IMAGE}/base:${BASE_IMAGE_VERSION}
needs:
- build
script:
- dotnet nuget add source "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/nuget/index.json" --name gitlab
- dotnet nuget push artifacts/*.nupkg --source gitlab --api-key ${CI_DEPLOY_PASSWORD}
when: manual
8 changes: 8 additions & 0 deletions .gitlab/Dockerfile-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine

COPY --from=mcr.microsoft.com/dotnet/sdk:7.0-alpine /usr/share/dotnet/shared /usr/share/dotnet/shared
COPY --from=mcr.microsoft.com/dotnet/sdk:7.0-alpine /usr/share/dotnet/sdk /usr/share/dotnet/sdk
COPY --from=mcr.microsoft.com/dotnet/sdk:6.0-alpine /usr/share/dotnet/shared /usr/share/dotnet/shared
COPY --from=mcr.microsoft.com/dotnet/sdk:6.0-alpine /usr/share/dotnet/sdk /usr/share/dotnet/sdk

RUN apk add --no-cache bash npm
12 changes: 12 additions & 0 deletions .gitlab/testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<!-- If this version changes, the adapter path in the pipeline needs to be updated -->
<PackageReference Include="JunitXml.TestLogger" Version="3.1.12" />
</ItemGroup>

</Project>
55 changes: 6 additions & 49 deletions build/Build.Pack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public partial class Build
Target Pack => _ => _
.DependsOn(Compile)
.After(Test)
.OnlyWhenDynamic(() => IsRunningOnWindows)
.Executes(() =>
{
if (Configuration != Configuration.Release)
Expand Down Expand Up @@ -59,59 +58,20 @@ public partial class Build
.SetFileVersion(VersionPrefix)
.SetInformationalVersion(VersionPrefix)
.SetVersion(nugetVersion)
.SetConfiguration(Configuration)
// Some part of the pack (possibly the nuspec) does not lower-case the configuration when finding
// artifact directories like build does
.SetConfiguration(Configuration.ToString().ToLowerInvariant())
.SetOutputDirectory(ArtifactsDirectory)
.SetDeterministic(IsServerBuild)
.SetContinuousIntegrationBuild(IsServerBuild)
);
}
Serilog.Log.Information("Build WiX installer");
(SourceDirectory / "NSwagStudio.Installer" / "bin").CreateOrCleanDirectory();
MSBuild(x => x
.SetTargetPath(GetProject("NSwagStudio.Installer"))
.SetTargets("Rebuild")
.SetAssemblyVersion(VersionPrefix)
.SetFileVersion(VersionPrefix)
.SetInformationalVersion(VersionPrefix)
.SetConfiguration(Configuration)
.SetMaxCpuCount(Environment.ProcessorCount)
.SetNodeReuse(IsLocalBuild)
.SetVerbosity(MSBuildVerbosity.Minimal)
.SetProperty("Deterministic", IsServerBuild)
.SetProperty("ContinuousIntegrationBuild", IsServerBuild)
);
// gather relevant artifacts
Serilog.Log.Information("Package nuspecs");
var apiDescriptionClientNuSpec = SourceDirectory / "NSwag.ApiDescription.Client" / "NSwag.ApiDescription.Client.nuspec";
var content = apiDescriptionClientNuSpec.ReadAllText();
content = content.Replace("<dependency id=\"NSwag.MSBuild\" version=\"1.0.0\" />", "<dependency id=\"NSwag.MSBuild\" version=\"" + nugetVersion + "\" />");
apiDescriptionClientNuSpec.WriteAllText(content);
var nuspecs = new[]
{
apiDescriptionClientNuSpec,
SourceDirectory / "NSwag.MSBuild" / "NSwag.MSBuild.nuspec",
SourceDirectory / "NSwagStudio.Chocolatey" / "NSwagStudio.nuspec"
};
foreach (var nuspec in nuspecs)
{
NuGetPack(x => x
.SetOutputDirectory(ArtifactsDirectory)
.SetConfiguration(Configuration)
.SetVersion(nugetVersion)
.SetTargetPath(nuspec)
);
}
// GitLab: Removed windows-only and other problematic and unnecessary packages
var artifacts = Array.Empty<AbsolutePath>()
.Concat(RootDirectory.GlobFiles("**/Release/**/NSwag*.nupkg"))
.Concat(SourceDirectory.GlobFiles("**/Release/**/NSwagStudio.msi"));
;
foreach (var artifact in artifacts)
{
Expand All @@ -120,16 +80,13 @@ public partial class Build
// patch npm version
var npmPackagesFile = SourceDirectory / "NSwag.Npm" / "package.json";
content = npmPackagesFile.ReadAllText();
var content = npmPackagesFile.ReadAllText();
content = Regex.Replace(content, @"""version"": "".*""", @"""version"": """ + nugetVersion + @"""");
npmPackagesFile.WriteAllText(content);
// ZIP directories
ZipFile.CreateFromDirectory(NSwagNpmBinaries, ArtifactsDirectory / "NSwag.Npm.zip");
ZipFile.CreateFromDirectory(NSwagStudioBinaries, ArtifactsDirectory / "NSwag.zip");
// NSwagStudio.msi
CopyFileToDirectory(ArtifactsDirectory / "bin" / "NSwagStudio.Installer" / Configuration / "NSwagStudio.msi", ArtifactsDirectory);
});
}

8 changes: 8 additions & 0 deletions build/Build.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;
Expand Down Expand Up @@ -145,6 +146,11 @@ protected override void OnBuildInitialized()
.SetProcessWorkingDirectory(SourceDirectory / "NSwag.Npm")
);
DotNetRestore(x => x
.SetProjectFile(".gitlab")
.SetPackageDirectory(".gitlab/packages")
);
DotNetRestore(x => x
.SetProjectFile(SolutionFile)
.SetVerbosity(DotNetVerbosity.minimal)
Expand Down Expand Up @@ -230,6 +236,8 @@ protected override void OnBuildInitialized()
.EnableNoRestore()
.EnableNoBuild()
.SetConfiguration(Configuration)
.SetLoggers("junit;MethodFormat=Full;FailureBodyFormat=Verbose")
.SetTestAdapterPath(".gitlab/packages/junitxml.testlogger/3.1.12/build/_common/")
);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
Expand Down Expand Up @@ -59,6 +60,8 @@ public async Task Should_generate_openapi_for_project(string projectName, string
finally
{
process.Kill();
// Process is not guaranteed to have ended when Kill returns
process.WaitForExit();
}

// Assert
Expand Down

0 comments on commit 905d526

Please sign in to comment.