Skip to content

Commit ae1c4a7

Browse files
committed
Build and release action improvements
* Let Build.ps1 and release.yml run on an ubuntu agent * Full automatic release: use last commit message (usually merge commit from PR) as release description
1 parent 52ac86a commit ae1c4a7

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

.github/workflows/release.yml

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,56 @@ name: Release
22

33
on:
44
push:
5-
branches: [ dev, main ]
5+
branches:
6+
- main
7+
- dev
68

79
# Allows you to run this workflow manually from the Actions tab
810
workflow_dispatch:
911

1012
jobs:
1113
build-and-release:
12-
runs-on: windows-latest
14+
runs-on: ubuntu-latest
1315
steps:
14-
- uses: actions/checkout@v3
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Read version from csproj
21+
id: extract_version
22+
if: github.ref == 'refs/heads/main'
23+
run: |
24+
VERSION=$(grep '<VersionPrefix>' src/Serilog.Sinks.MSSqlServer/Serilog.Sinks.MSSqlServer.csproj | sed 's/.*<VersionPrefix>\(.*\)<\/VersionPrefix>.*/\1/')
25+
echo "VERSION=$VERSION" >> $GITHUB_ENV
26+
if git rev-parse "v$VERSION" >/dev/null 2>&1; then
27+
echo "Tag v$VERSION already exists"
28+
exit 1
29+
fi
1530
1631
- name: Run build
1732
run: ./Build.ps1 -SkipTests
33+
shell: pwsh
1834

19-
- name: Read version from csproj
35+
- name: Get last commit message
36+
id: last_commit
37+
if: success() && github.ref == 'refs/heads/main'
2038
run: |
21-
$selectResult = Select-String -Path ".\src\Serilog.Sinks.MSSqlServer\Serilog.Sinks.MSSqlServer.csproj" -Pattern '<VersionPrefix>(.+)</VersionPrefix>'
22-
$versionMatch = $selectResult.Matches[0].Groups[1].Value
23-
echo ("VERSION=" + $versionMatch) >> $env:GITHUB_ENV
24-
echo "Found version $versionMatch"
25-
26-
- name: Create GitHub release
27-
if: ${{ github.ref_name == 'main' }}
28-
uses: "marvinpinto/action-automatic-releases@latest"
29-
with:
30-
repo_token: "${{ secrets.GITHUB_TOKEN }}"
31-
prerelease: false
32-
automatic_release_tag: "v${{ env.VERSION }}"
33-
title: "v${{ env.VERSION }}"
34-
files: |
35-
artifacts/*.nupkg
36-
artifacts/*.snupkg
39+
git log -1 --pretty=%B > last_commit_message.txt
40+
41+
# Create GitHub release only on main branch (latest release)
42+
- name: Create Release
43+
if: github.ref == 'refs/heads/main' && success()
44+
env:
45+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46+
run: |
47+
gh release create v${{ env.VERSION }} \
48+
--title "v${{ env.VERSION }}" \
49+
--notes "$(cat last_commit_message.txt)" \
50+
artifacts/*.nupkg artifacts/*.snupkg
3751
3852
- name: Publish to nuget.org
3953
env:
4054
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
41-
run: nuget push artifacts\*.nupkg -Source 'https://api.nuget.org/v3/index.json' -ApiKey ${{ env.NUGET_API_KEY }}
55+
run: |
56+
nuget push artifacts/*.nupkg -Source https://api.nuget.org/v3/index.json -ApiKey ${{ secrets.NUGET_API_KEY }}
57+
nuget push artifacts/*.snupkg -Source https://api.nuget.org/v3/index.json -ApiKey ${{ secrets.NUGET_API_KEY }}

Build.ps1

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,55 @@ param (
77

88
echo "build: Build started"
99

10-
Push-Location $PSScriptRoot
10+
Push-Location "$PSScriptRoot"
1111

12-
if(Test-Path .\artifacts) {
13-
echo "build: Cleaning .\artifacts"
14-
Remove-Item .\artifacts -Force -Recurse
12+
if (Test-Path .\artifacts) {
13+
echo "build: Cleaning .\artifacts"
14+
Remove-Item .\artifacts -Force -Recurse
1515
}
1616

1717
& dotnet restore --no-cache
1818

19-
$branch = @{ $true = $env:GITHUB_REF_NAME; $false = $(git symbolic-ref --short -q HEAD) }[$env:GITHUB_REF_NAME -ne $NULL];
20-
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:GITHUB_RUN_NUMBER, 10); $false = "local" }[$env:GITHUB_RUN_NUMBER -ne $NULL];
21-
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -ne "dev" -and $revision -ne "local"]
19+
$branch = @{ $true = $env:GITHUB_REF_NAME; $false = $(git symbolic-ref --short -q HEAD) }[$env:GITHUB_REF_NAME -ne $NULL]
20+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:GITHUB_RUN_NUMBER, 10); $false = "local" }[$env:GITHUB_RUN_NUMBER -ne $NULL]
21+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10, $branch.Length)))-$revision" }[$branch -ne "dev" -and $revision -ne "local"]
2222

2323
echo "build: Version suffix is $suffix"
2424

25-
foreach ($src in ls src/*) {
26-
Push-Location $src
25+
foreach ($src in Get-ChildItem "$PSScriptRoot/src" -Directory) {
26+
Push-Location $src.FullName
2727

28-
echo "build: Packaging project in $src"
28+
echo "build: Packaging project in $($src.FullName)"
2929

3030
if ($suffix) {
3131
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
3232
} else {
3333
& dotnet pack -c Release -o ..\..\artifacts
3434
}
35-
if($LASTEXITCODE -ne 0) { exit 1 }
35+
if ($LASTEXITCODE -ne 0) { exit 1 }
3636

3737
Pop-Location
3838
}
3939

4040
if ($SkipTests -eq $false) {
41-
foreach ($test in ls test/*.PerformanceTests) {
42-
Push-Location $test
41+
foreach ($test in Get-ChildItem "$PSScriptRoot/test" -Filter "*.PerformanceTests" -Directory) {
42+
Push-Location $test.FullName
4343

44-
echo "build: Building performance test project in $test"
44+
echo "build: Building performance test project in $($test.FullName)"
4545

4646
& dotnet build -c Release
47-
if($LASTEXITCODE -ne 0) { exit 2 }
47+
if ($LASTEXITCODE -ne 0) { exit 2 }
4848

4949
Pop-Location
5050
}
5151

52-
foreach ($test in ls test/*.Tests) {
53-
Push-Location $test
52+
foreach ($test in Get-ChildItem "$PSScriptRoot/test" -Filter "*.Tests" -Directory) {
53+
Push-Location $test.FullName
5454

55-
echo "build: Testing project in $test"
55+
echo "build: Testing project in $($test.FullName)"
5656

5757
& dotnet test -c Release --collect "XPlat Code Coverage"
58-
if($LASTEXITCODE -ne 0) { exit 3 }
58+
if ($LASTEXITCODE -ne 0) { exit 3 }
5959

6060
Pop-Location
6161
}

0 commit comments

Comments
 (0)