Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: automate NuGet publishing #21

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions .github/workflows/nuget-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
name: Publish libraries

on:
workflow_dispatch:
inputs:
dry-run:
description: 'Dry run'
required: true
type: boolean
default: true
schedule:
- cron: '28 3 * * 1' # 3:28 AM UTC every Monday

jobs:
preflight:
name: Preflight
runs-on: ubuntu-latest
outputs:
dry-run: ${{ steps.get-dry-run.outputs.dry-run }}

steps:
- name: Get dry run
id: get-dry-run
shell: pwsh
run: |
Set-PSDebug -Trace 1

$IsDryRun = '${{ github.event.inputs.dry-run }}' -Eq 'true' -Or '${{ github.event_name }}' -Eq 'schedule'

if ($IsDryRun) {
echo "dry-run=true" >> $Env:GITHUB_OUTPUT
} else {
echo "dry-run=false" >> $Env:GITHUB_OUTPUT
}

build:
name: NuGet package build [${{matrix.library}}]
runs-on: windows-2022

strategy:
fail-fast: false
matrix:
library: [ NowAgent ]
include:
- library: NowAgent
libpath: ./dotnet/Devolutions.NowClient

steps:
- name: Check out ${{ github.repository }}
uses: actions/checkout@v4

- name: Build
shell: pwsh
run: |
Set-PSDebug -Trace 1

$Path = '${{matrix.libpath}}'

$csprojPath = Get-ChildItem -Path "$Path" -Filter '*.csproj' | Select -First 1
dotnet build --configuration Release "$csprojPath"
dotnet pack --configuration Release "$csprojPath"

New-Item -ItemType "directory" -Path . -Name "nuget-packages"
Get-ChildItem -Path $Path -Recurse -Include '*.nupkg' | ForEach { Copy-Item $_ "./nuget-packages" }
Get-ChildItem -Path $Path -Recurse -Include '*.snupkg' | ForEach { Copy-Item $_ "./nuget-packages" }

- name: Upload packages
uses: actions/upload-artifact@v4
with:
name: nupkg-${{matrix.library}}
path: |
nuget-packages/*.nupkg
nuget-packages/*.snupkg

merge:
name: NuGet merge artifacts
runs-on: ubuntu-latest
needs: build

steps:
- name: Merge Artifacts
uses: actions/upload-artifact/merge@v4
with:
name: nupkg
pattern: nupkg-*
delete-merged: true

publish:
name: Publish NuGet packages
runs-on: ubuntu-latest
environment: publish-prod
if: needs.preflight.outputs.dry-run == 'false'
needs:
- preflight
- merge

steps:
- name: Download NuGet packages artifact
uses: actions/download-artifact@v4
with:
name: nupkg
path: nuget-packages

- name: Publish to nuget.org
shell: pwsh
run: |
Set-PSDebug -Trace 1

$Files = Get-ChildItem -Recurse nuget-packages/*.nupkg

foreach ($File in $Files) {
$PushCmd = @(
'dotnet',
'nuget',
'push',
"$File",
'--api-key',
'${{ secrets.NUGET_API_KEY }}',
'--source',
'https://api.nuget.org/v3/index.json',
'--skip-duplicate'
)

Write-Host "Publishing $($File.Name)..."
$PushCmd = $PushCmd -Join ' '
Invoke-Expression $PushCmd
}

notify:
name: Notify failure
runs-on: ubuntu-latest
if: ${{ always() && contains(needs.*.result, 'failure') && github.event_name == 'schedule' }}
needs:
- preflight
- build
- merge
- publish
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_ARCHITECTURE }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
steps:
- name: Send slack notification
id: slack
uses: slackapi/slack-github-action@v1.24.0
with:
payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*${{ github.repository }}* :fire::fire::fire::fire::fire: \n The scheduled build for *${{ github.repository }}* is <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|broken>"
}
}
]
}
Loading