Create Release and Publish to NuGet #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create Release and Publish to NuGet | |
on: | |
workflow_dispatch: | |
inputs: | |
release_type: | |
description: 'Release type' | |
required: true | |
default: 'patch' | |
type: choice | |
options: | |
- patch | |
- minor | |
- major | |
jobs: | |
build-and-release: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v3 | |
with: | |
dotnet-version: '9.0.x' | |
# Setup | |
- name: Install GitVersion | |
uses: gittools/actions/gitversion/[email protected] | |
with: | |
preferLatestVersion: true | |
# Fetch version | |
- name: Determine Version | |
id: gitversion | |
uses: gittools/actions/gitversion/[email protected] | |
# Gen release notes | |
- name: Generate Release Notes | |
id: release_notes | |
run: | | |
PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
if [ -z "$PREVIOUS_TAG" ]; then | |
COMMITS=$(git log --pretty=format:"- %s" --no-merges) | |
else | |
COMMITS=$(git log ${PREVIOUS_TAG}..HEAD --pretty=format:"- %s" --no-merges) | |
fi | |
echo "RELEASE_NOTES<<EOF" >> $GITHUB_ENV | |
echo "## What's Changed" >> $GITHUB_ENV | |
echo "$COMMITS" >> $GITHUB_ENV | |
echo "EOF" >> $GITHUB_ENV | |
# Calc new version | |
- name: Calculate New Version | |
id: version | |
run: | | |
CURRENT_VERSION=${{ steps.gitversion.outputs.semVer }} | |
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION" | |
MAJOR="${VERSION_PARTS[0]}" | |
MINOR="${VERSION_PARTS[1]}" | |
PATCH="${VERSION_PARTS[2]}" | |
case "${{ github.event.inputs.release_type }}" in | |
"major") | |
NEW_VERSION="$((MAJOR + 1)).0.0" | |
;; | |
"minor") | |
NEW_VERSION="${MAJOR}.$((MINOR + 1)).0" | |
;; | |
"patch") | |
NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))" | |
;; | |
esac | |
echo "NEW_VERSION=${NEW_VERSION}" >> $GITHUB_ENV | |
# Update csproj | |
- name: Update Version | |
run: | | |
find . -name "*.csproj" -type f -exec sed -i "s/<Version>.*<\/Version>/<Version>${{ env.NEW_VERSION }}<\/Version>/g" {} \; | |
- name: Restore dependencies | |
run: dotnet restore | |
- name: Build | |
run: dotnet build --configuration Release --no-restore | |
- name: Pack | |
run: dotnet pack --configuration Release --no-build --output nupkgs | |
# Create release | |
- name: Create Release | |
id: create_release | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
tag_name: v${{ env.NEW_VERSION }} | |
release_name: Release v${{ env.NEW_VERSION }} | |
body: ${{ env.RELEASE_NOTES }} | |
draft: false | |
prerelease: false | |
# Move .nupkg into dist | |
- name: Upload Release Asset | |
uses: softprops/action-gh-release@v1 | |
with: | |
files: ./nupkgs/*.nupkg | |
tag_name: v${{ env.NEW_VERSION }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# Publish | |
- name: Push to NuGet | |
run: dotnet nuget push "./nupkgs/*.nupkg" --source "https://api.nuget.org/v3/index.json" --api-key ${{secrets.NUGET_API_KEY}} --skip-duplicate | |
# Commit | |
- name: Commit version update | |
run: | | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
git add . | |
git commit -m "Bump version to ${{ env.NEW_VERSION }}" | |
git tag -a "v${{ env.NEW_VERSION }}" -m "Release v${{ env.NEW_VERSION }}" | |
git push --follow-tags |