Skip to content

Commit

Permalink
Release to NuGet
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed May 28, 2024
1 parent 0918f94 commit fda0eb6
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
25 changes: 23 additions & 2 deletions .github/workflows/_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ name: Build

on:
workflow_call:
inputs:
packShipCandidate:
required: false
type: boolean
default: false

jobs:
build:
Expand All @@ -28,12 +33,28 @@ jobs:

- name: Restore dependencies
run: dotnet restore

- name: Create build number
run: |
now=$(date +'%Y%m%d')
buildNumber=$now.$GITHUB_RUN_NUMBER
echo "BUILD_NUMBER=$buildNumber" >> $GITHUB_ENV
echo "$buildNumber"
- name: Build
run: dotnet build --no-restore --configuration Release -p:SourceRevisionId=$GITHUB_SHA
run: dotnet build --no-restore --configuration Release -p:BuildNumber=$BUILD_NUMBER -p:SourceRevisionId=$GITHUB_SHA -p:ContinuousIntegrationBuild=true

- name: Pack (ci)
run: dotnet pack --configuration Release --output ./artifacts/ci --verbosity normal -p:SourceRevisionId=$GITHUB_SHA
run: dotnet pack --configuration Release --output ./artifacts/ci --verbosity normal -p:BuildNumber=$BUILD_NUMBER -p:SourceRevisionId=$GITHUB_SHA -p:ContinuousIntegrationBuild=true

- name: Pack (ship candidate)
if: ${{ inputs.packShipCandidate }}
run: >-
dotnet pack --configuration Release --output ./artifacts/ship --verbosity normal
-p:BuildNumber=$BUILD_NUMBER
-p:SourceRevisionId=$GITHUB_SHA
-p:ContinuousIntegrationBuild=true
-p:IsShipCandidate=true
- name: Upload artifacts (packages)
uses: actions/upload-artifact@v4
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
build:
name: Build
uses: ./.github/workflows/_build.yml
with:
packShipCandidate: true

deploy:
name: Deploy
Expand Down
94 changes: 94 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Release

on:
workflow_dispatch:
inputs:
runId:
description: The run ID of the CI workflow to release NuGet artifacts from
required: true
type: string

jobs:
push-package:
name: Release
runs-on: ubuntu-latest
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_NOLOGO: true
DOTNET_ADD_GLOBAL_TOOLS_TO_PATH: false
DOTNET_MULTILEVEL_LOOKUP: 0
DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION: true

permissions:
packages: write

steps:
- name: Download workflow run details
run: |
workflowUrl="https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ inputs.runId }}"
curl -s -H "Accept: application/json" "${workflowUrl}" > workflow_details.json
- name: Extract workflow run commit SHA
uses: sergeysova/jq-action@v2
id: workflowsha
with:
cmd: 'jq .head_sha workflow_details.json -r'

- name: Download workflow run artifacts
uses: dawidd6/action-download-artifact@v3
with:
run_id: ${{ inputs.runId }}
workflow_conclusion: success
name: nupkg

- name: Setup .NET SDK
uses: actions/setup-dotnet@v4

- name: Add nuget.org source
run: dotnet nuget add source --name NUGET https://www.nuget.org

- name: Push packages to nuget.org
run: |
_file=$(find ./ship -iname "*.nupkg" -print -quit)
_releaseVersion=""
echo "Found package ${_file}"
# strip dir path from file path
_filename="${_file##*/}"
# strip file extension from file name
_filenamewithoutext="${_filename%.*}"
# get version from file name
_packageVersion=$( \
echo "$_filenamewithoutext" | \
# Regex to match semantic version at end of string
grep -oP '(?<=\.)[0-9]+\.[0-9]+\.[0-9]+((-[a-zA-Z]+)?(\.[0-9]+)*)*$' \
)
# get package id by truncating the version from the file name
_packageId="${_filenamewithoutext%%.$_packageVersion}"
echo "Got version '${_packageVersion}' for '${_packageId}'."
_releaseVersion=$_packageVersion
# verify package doesn't already exist
_packageUrl="https://api.nuget.org/v3/registration5-semver1/${_packageId}/${_packageVersion}.json"
echo "Checking for existing package at ${_packageUrl}"
_statusCode=$(curl -s -o /dev/null -I -w '%{http_code}' "${_packageUrl}")
echo "Status code ${_statusCode}"
if [ "${_statusCode}" -eq "200" ]; then
echo "The package ${_packageId} with version ${_packageVersion} already exists on nuget.org"; exit 1
elif [ "${_statusCode}" -eq "404" ]; then
echo "Confirmed package ${_packageId} with version ${_packageVersion} does not already exist on nuget.org"
else
echo "Unexpected status code ${_statusCode} received from nuget.org"; exit 1
fi
# push package to nuget.org
echo "Pushing package ${_file} to nuget.org"
dotnet nuget push "${_file}" -s "NUGET" -k ${{ secrets.NUGET_API_KEY }}
echo "PACKAGE_VERSION=${_releaseVersion}" >> $GITHUB_ENV
- name: Create release
uses: ncipollo/release-action@v1
with:
tag: v${{ env.PACKAGE_VERSION }}
commit: ${{ steps.workflowsha.outputs.value }}
generateReleaseNotes: true
draft: true
prerelease: ${{ contains(env.PACKAGE_VERSION, '-') }}
1 change: 1 addition & 0 deletions Aspire.Hosting.WireMock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
ProjectSection(SolutionItems) = preProject
.github\workflows\ci.yml = .github\workflows\ci.yml
.github\workflows\pr.yml = .github\workflows\pr.yml
.github\workflows\release.yml = .github\workflows\release.yml
.github\workflows\_build.yml = .github\workflows\_build.yml
EndProjectSection
EndProject
Expand Down
5 changes: 5 additions & 0 deletions src/Aspire.Hosting.WireMock/Aspire.Hosting.WireMock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<Description>API mocking support for .NET Aspire using WireMock.Net.</Description>
<PackageTags>aspire hosting WireMock.Net mock api</PackageTags>
<VersionPrefix Condition=" '$(VersionPrefix)' == '' ">0.0.1</VersionPrefix>
<!-- VersionSuffix used for local builds -->
<VersionSuffix>dev</VersionSuffix>
<!-- VersionSuffix to be used for CI builds -->
<VersionSuffix Condition=" '$(ContinuousIntegrationBuild)' == 'true' And '$(BuildNumber)' != '' ">ci.$(BuildNumber)</VersionSuffix>
<VersionSuffix Condition=" '$(ContinuousIntegrationBuild)' == 'true' And '$(IsShipCandidate)' == 'true' "></VersionSuffix>
<Authors>Fredi Machado</Authors>
<Copyright>Copyright © Fredi Machado</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down

0 comments on commit fda0eb6

Please sign in to comment.