Skip to content

Commit

Permalink
Add package publish automation
Browse files Browse the repository at this point in the history
- Extend the build workflow to output the NuGet package name and version and then dispatch a `nuget_packages_published` event when the NuGet package is published to NuGet.org.
- Pin the version of dotnet-validate used.
  • Loading branch information
martincostello committed May 25, 2024
1 parent e8c6d26 commit 6f8aea3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-validate": {
"version": "0.0.1-preview.304",
"commands": [
"dotnet-validate"
]
}
}
}
31 changes: 30 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:

outputs:
dotnet-sdk-version: ${{ steps.setup-dotnet.outputs.dotnet-version }}
dotnet-validate-version: ${{ steps.get-dotnet-validate-version.outputs.dotnet-validate-version }}
package-names: ${{ steps.build.outputs.package-names }}
package-version: ${{ steps.build.outputs.package-version }}

permissions:
attestations: write
Expand Down Expand Up @@ -54,6 +57,7 @@ jobs:
id: setup-dotnet

- name: Build, Test and Package
id: build
shell: pwsh
run: ./build.ps1

Expand Down Expand Up @@ -84,6 +88,16 @@ jobs:
path: ./artifacts/package/release
if-no-files-found: error

- name: Get dotnet-validate version
id: get-dotnet-validate-version
shell: pwsh
run: |
$dotnetValidateVersion = (Get-Content "./.config/dotnet-tools.json" | Out-String | ConvertFrom-Json).tools.'dotnet-validate'.version
"dotnet-validate-version=${dotnetValidateVersion}" >> $env:GITHUB_OUTPUT
# TODO Remover after testing
Write-Output "package-names=${{ steps.build.outputs.package-names }}"
Write-Output "package-version=${{ steps.build.outputs.package-version }}"
validate-packages:
needs: build
runs-on: ubuntu-latest
Expand All @@ -101,8 +115,10 @@ jobs:

- name: Validate NuGet packages
shell: pwsh
env:
DOTNET_VALIDATE_VERSION: ${{ needs.build.outputs.dotnet-validate-version }}
run: |
dotnet tool install --global dotnet-validate --version 0.0.1-preview.304
dotnet tool install --global dotnet-validate --version ${env:DOTNET_VALIDATE_VERSION}
$packages = Get-ChildItem -Filter "*.nupkg" | ForEach-Object { $_.FullName }
$invalidPackages = 0
foreach ($package in $packages) {
Expand Down Expand Up @@ -173,3 +189,16 @@ jobs:
API_KEY: ${{ secrets.NUGET_TOKEN }}
SOURCE: https://api.nuget.org/v3/index.json
run: dotnet nuget push "*.nupkg" --api-key "${API_KEY}" --skip-duplicate --source "${SOURCE}"

- name: Publish nuget_packages_published
uses: peter-evans/repository-dispatch@ff45666b9427631e3450c54a1bcbee4d9ff4d7c0 # v3.0.0
with:
event-type: nuget_packages_published
repository: ${{ github.repository_owner }}/github-automation
token: ${{ secrets.COSTELLOBOT_TOKEN }}
client-payload: |-
{
"repository": "${{ github.repository }}",
"packages": "${{ needs.build.outputs.package-names }}",
"version": "${{ needs.build.outputs.package-version }}"
}
19 changes: 19 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project>
<Target Name="SetNuGetPackageOutputs" AfterTargets="Pack" Condition=" '$(GITHUB_OUTPUT)' != '' ">
<PropertyGroup>
<_PackageNamesPath>$(ArtifactsPath)\package-names.txt</_PackageNamesPath>
</PropertyGroup>
<ReadLinesFromFile File="$(_PackageNamesPath)">
<Output TaskParameter="Lines" ItemName="_PackageNames" />
</ReadLinesFromFile>
<ItemGroup>
<_PackageNames Include="$(PackageId)" />
</ItemGroup>
<RemoveDuplicates Inputs="@(_PackageNames)">
<Output TaskParameter="Filtered" ItemName="_UniquePackageNames" />
</RemoveDuplicates>
<WriteLinesToFile File="$(_PackageNamesPath)" Lines="@(_UniquePackageNames)" Overwrite="true" WriteOnlyWhenDifferent="true" />
<WriteLinesToFile File="$(GITHUB_OUTPUT)" Lines="package-names=@(_UniquePackageNames->'%(Identity)', ',')" />
<WriteLinesToFile File="$(GITHUB_OUTPUT)" Lines="package-version=$(Version)" />
</Target>
</Project>
1 change: 1 addition & 0 deletions PseudoLocalizer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.vsconfig = .vsconfig
build.ps1 = build.ps1
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
Directory.Packages.props = Directory.Packages.props
global.json = global.json
LICENSE = LICENSE
Expand Down
8 changes: 5 additions & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ ForEach ($packageProject in $packageProjects) {
DotNetPack $packageProject
}

Write-Host "Running tests..." -ForegroundColor Green
ForEach ($testProject in $testProjects) {
DotNetTest $testProject
if (-Not $SkipTests) {
Write-Host "Running tests..." -ForegroundColor Green
ForEach ($testProject in $testProjects) {
DotNetTest $testProject
}
}

0 comments on commit 6f8aea3

Please sign in to comment.