Skip to content

Commit

Permalink
Use Git tags for version numbers in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
rmunn committed Jul 17, 2024
1 parent 803b3bd commit 2debb17
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
139 changes: 139 additions & 0 deletions .github/workflows/nuget-ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
name: Build NuGet packages
on:
push:
branches:
- main
pull_request:
branches:
- main

on:
push:
# Note: main is NOT included here
branches: [ develop ]
tags:
- v*
pull_request:
branches: [ develop ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
# We use `git describe` to find tags in commit history, so we need complete repo history
fetch-depth: 0

- name: Find most recent tag
shell: bash
run: |
DESCRIBE=$(git describe --long --match "v*")
TAG=$(echo "$DESCRIBE" | grep -E -o '^v[0-9]+\.[0-9]+\.[0-9]+')
echo "DESCRIBE=$DESCRIBE" >> "$GITHUB_ENV"
echo "TAG=$TAG" >> "$GITHUB_ENV"
- name: Split version number from tag into major/minor/patch sections
shell: bash
run: |
MAJOR=$(echo "$TAG" | sed -E 's/^v([0-9]+)\.([0-9]+)\.([0-9]+)$/\1/')
MINOR=$(echo "$TAG" | sed -E 's/^v([0-9]+)\.([0-9]+)\.([0-9]+)$/\2/')
PATCH=$(echo "$TAG" | sed -E 's/^v([0-9]+)\.([0-9]+)\.([0-9]+)$/\3/')
echo "MAJOR=$MAJOR" >> "$GITHUB_ENV"
echo "MINOR=$MINOR" >> "$GITHUB_ENV"
echo "PATCH=$PATCH" >> "$GITHUB_ENV"
- name: Get GitHub build number
shell: bash
run: echo "BUILD_NUMBER=${{ github.run_number }}" >> "$GITHUB_ENV"

- name: Get PR number for prerelease suffix
if: github.event_name == 'pull_request'
shell: bash
run: echo "PR_NUMBER=${{ github.event.number }}" >> "$GITHUB_ENV"

- name: Calculate prerelease suffix
shell: bash
run: |
SUFFIX=""
if [ -n "$PR_NUMBER" ]; then
SUFFIX="-PR${PR_NUMBER}.${BUILD_NUMBER}"
elif [ "$GITHUB_REF" = "refs/heads/develop" ]; then
SUFFIX="-beta.${BUILD_NUMBER}"
elif [ "$GITHUB_REF" = "refs/heads/main" ]; then
SUFFIX="-rc.${BUILD_NUMBER}"
fi
echo "SUFFIX=$SUFFIX" >> "$GITHUB_ENV"
- name: Calculate version number bump
# Same logic as GitVersion:
# * "+semver: breaking" or "+semver: major" in commit log will produce major version bump (and reset minor and patch to 0)
# * "+semver: feature" or "+semver: minor" in commit log will produce minor version bump (and reset patch to 0)
# Default is to bump the patch version
# Git log format "%B" is the raw body with no author's email or anything else
shell: bash
run: |
COMMIT_COUNT=$(echo "$DESCRIBE" | sed -E 's/^[^-]+-([^-]+)-.*$/\1/')
if [ -n "$COMMIT_COUNT" -a "$COMMIT_COUNT" -gt 0 ]; then
# Calculate bump based on commit messages
RAW_LOG=$(git log --format="%B" "$TAG"..HEAD)
if grep -E '\+semver: (breaking|major)' <<< "$RAW_LOG"; then
MAJOR=$(($MAJOR + 1))
MINOR=0
PATCH=0
elif grep -E '\+semver: (feature|minor)' <<< "$RAW_LOG"; then
MINOR=$(($MINOR + 1))
PATCH=0
else
PATCH=$(($PATCH + 1))
fi
fi
echo "MAJOR=$MAJOR" >> "$GITHUB_ENV"
echo "MINOR=$MINOR" >> "$GITHUB_ENV"
echo "PATCH=$PATCH" >> "$GITHUB_ENV"
- name: Set version number variables for MSBuild
shell: bash
run: |
echo "PACKAGE_VERSION=${MAJOR}.${MINOR}.${PATCH}${SUFFIX}" >> "$GITHUB_ENV"
if [ $MAJOR -eq 0 ]; then
echo "ASSEMBLY_VERSION=0.${MINOR}.0.0" >> "$GITHUB_ENV"
else
echo "ASSEMBLY_VERSION=${MAJOR}.0.0.0" >> "$GITHUB_ENV"
fi
echo "FILE_VERSION=${MAJOR}.${MINOR}.${PATCH}.${BUILD_NUMBER}" >> "$GITHUB_ENV"
- name: Display version number for debugging
shell: bash
run: echo "Calculated version number is $PACKAGE_VERSION"

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

- name: Build & test
run: dotnet test --configuration Release --logger GitHubActions

- name: Pack
shell: bash
run: |
dotnet pack --include-symbols /p:PackageVersion="$PACKAGE_VERSION" /p:AssemblyVersion="$ASSEMBLY_VERSION" /p:FileVersion="$FILE_VERSION"
- name: Upload packages to build artifacts
uses: actions/upload-artifact@v4
with:
name: nuget-packages
path: src/artifacts/package/release/*nupkg

- name: Publish package to NuGet.org
if: ${{ github.event_name == 'push' && github.ref.startswith('refs/tags') }}
shell: bash
run: |
echo Would run the following:
echo dotnet nuget push "src/artifacts/package/release/*nupkg" --skip-duplicate --api-key "$NUGET_API_KEY" --source https://api.nuget.org/v3/index.json
env:
NUGET_API_KEY: fake-key-for-testing-purposes
# NUGET_API_KEY: ${{ secrets.SILLSDEV_PUBLISH_NUGET_ORG }}
9 changes: 9 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<UseArtifactsOutput>true</UseArtifactsOutput>
<IsPackable Condition=" '$(IsPackable)' == '' ">false</IsPackable>
</PropertyGroup>

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<AssemblyVersion Condition=" '$(AssemblyVersion)' == '' ">$(ASSEMBLY_VERSION)</AssemblyVersion>
<AssemblyVersion Condition=" '$(AssemblyVersion)' == '' ">0.1.0.0</AssemblyVersion>
<FileVersion Condition=" '$(FileVersion)' == '' ">$(FILE_VERSION)</FileVersion>
<FileVersion Condition=" '$(FileVersion)' == '' ">0.1.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/SIL.Harmony.Core/SIL.Harmony.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<RootNamespace>SIL.Harmony.Core</RootNamespace>
<IsPackable>true</IsPackable>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="SIL.Harmony"/>
Expand Down
1 change: 1 addition & 0 deletions src/SIL.Harmony/SIL.Harmony.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<RootNamespace>SIL.Harmony</RootNamespace>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 2debb17

Please sign in to comment.