From e31b85b8af163092448ded63662a70ea5cc682e0 Mon Sep 17 00:00:00 2001 From: "Per Christian B. Viken" Date: Sat, 2 Sep 2023 14:09:52 +0200 Subject: [PATCH] chore(workflow): Add CI/CD workflow Based on https://www.jamescroft.co.uk/how-to-build-publish-nuget-packages-with-github-actions/ last updated 2022-07-15. --- .github/workflows/release.yml | 55 +++++++++++++++++++ .../TypeContractor.Tool.csproj | 8 ++- build/GetBuildVersion.psm1 | 34 ++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 build/GetBuildVersion.psm1 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..0b13b46 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,55 @@ +name: CI + +on: + create: + branches: + - release/** + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + + env: + BUILD_CONFIG: 'Release' + SOLUTION: 'TypeContractor.sln' + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Get Build Version + run: | + Import-Module .\build\GetBuildVersion.psm1 + Write-Host $Env:GITHUB_REF + $version = GetBuildVersion -VersionString $Env:GITHUB_REF + echo "BUILD_VERSION=$version" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append + shell: pwsh + + - name: Setup NuGet + uses: NuGet/setup-nuget@v1 + with: + nuget-api-key: ${{secrets.NUGET_API_KEY}} + + - name: Restore dependencies + run: nuget restore $SOLUTION + + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 6.0.x + + - name: Build + run: dotnet build $SOLUTION --configuration $BUILD_CONFIG -p:Version=$BUILD_VERSION --no-restore + + - name: Run tests + run: dotnet test /p:Configuration=$env:BUILD_CONFIG --no-restore --no-build --verbosity normal + + - name: Publish + if: startsWith(github.ref, 'refs/heads/release') + run: nuget push **\*.nupkg -Source 'https://api.nuget.org/v3/index.json' -ApiKey ${{secrets.NUGET_API_KEY}} diff --git a/TypeContractor.Tool/TypeContractor.Tool.csproj b/TypeContractor.Tool/TypeContractor.Tool.csproj index 96ce55e..7c3574a 100644 --- a/TypeContractor.Tool/TypeContractor.Tool.csproj +++ b/TypeContractor.Tool/TypeContractor.Tool.csproj @@ -7,9 +7,13 @@ enable true + true TypeContractor - Hogia HR Systems - Provide TypeContractor as .NET tool + Per Christian B. Viken + + Provides a dotnet tool for generating TypeScript + definitions from an ASP.NET Core API. + dotnet-tool TypeContractor TypeScript generator typecontractor ./nupkg diff --git a/build/GetBuildVersion.psm1 b/build/GetBuildVersion.psm1 new file mode 100644 index 0000000..5a1b016 --- /dev/null +++ b/build/GetBuildVersion.psm1 @@ -0,0 +1,34 @@ +Function GetBuildVersion { + Param ( + [string]$VersionString + ) + + # Process through regex + $VersionString -match "(?\d+)(\.(?\d+))?(\.(?\d+))?(\-(?
[0-9A-Za-z\-\.]+))?(\+(?\d+))?" | Out-Null
+
+    if ($matches -eq $null) {
+        return "1.0.0-build"
+    }
+
+    # Extract the build metadata
+    $BuildRevision = [uint64]$matches['build']
+    # Extract the pre-release tag
+    $PreReleaseTag = [string]$matches['pre']
+    # Extract the patch
+    $Patch = [uint64]$matches['patch']
+    # Extract the minor
+    $Minor = [uint64]$matches['minor']
+    # Extract the major
+    $Major = [uint64]$matches['major']
+
+    $Version = [string]$Major + '.' + [string]$Minor + '.' + [string]$Patch;
+    if ($PreReleaseTag -ne [string]::Empty) {
+        $Version = $Version + '-' + $PreReleaseTag
+    }
+
+    if ($BuildRevision -ne 0) {
+        $Version = $Version + '.' + [string]$BuildRevision
+    }
+
+    return $Version
+}