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: Fix versioning in package deployment (v3) #1305

Draft
wants to merge 52 commits into
base: v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b74d7c2
allow push
DaveSkender Dec 29, 2024
7ebade3
fix vs code settings
DaveSkender Dec 29, 2024
d2a868a
drop in gitversion config
DaveSkender Dec 29, 2024
54ed13e
more tries
DaveSkender Dec 29, 2024
e9cbf41
update gitversion
DaveSkender Dec 30, 2024
94360ba
fix gitversion spec version
DaveSkender Dec 30, 2024
a0079b9
more upgrades
DaveSkender Dec 30, 2024
e6d9361
more updates
DaveSkender Dec 30, 2024
d77b743
try moving gitversion files
DaveSkender Dec 30, 2024
b6fe133
tweaks
DaveSkender Dec 30, 2024
f100137
simplify config
DaveSkender Dec 30, 2024
a227925
more tweaks
DaveSkender Dec 30, 2024
cc99260
add target path
DaveSkender Dec 30, 2024
47c2f87
fix target path
DaveSkender Dec 30, 2024
b45a939
update spec
DaveSkender Dec 30, 2024
00dea0d
add back v preview
DaveSkender Dec 30, 2024
56a8316
add Git state check
DaveSkender Dec 30, 2024
8ddc3a4
add checkers
DaveSkender Dec 30, 2024
37eb4ea
use explicit version
DaveSkender Dec 30, 2024
db6c434
fixes
DaveSkender Dec 30, 2024
e1fbab4
fixes
DaveSkender Dec 30, 2024
8252b1d
remove wildcard branch name
DaveSkender Dec 30, 2024
825d6bb
update use of vars
DaveSkender Dec 30, 2024
9ff95e1
update setup
DaveSkender Dec 30, 2024
a477787
use hard path to config
DaveSkender Dec 30, 2024
a1d758c
update target path
DaveSkender Dec 30, 2024
d9801e0
fix path
DaveSkender Dec 30, 2024
260705e
add .net 9
DaveSkender Dec 30, 2024
1d63336
try .net 8
DaveSkender Dec 30, 2024
df86e3e
try 6.0.4
DaveSkender Dec 30, 2024
417f479
try dotnet tool installer directly
DaveSkender Dec 30, 2024
85a7e31
setup and show
DaveSkender Dec 30, 2024
a894e8e
try another
DaveSkender Dec 30, 2024
62fd876
more
DaveSkender Dec 30, 2024
8e8ac3b
don't use mainline
DaveSkender Dec 30, 2024
5a00958
add regex
DaveSkender Dec 30, 2024
d6f98d4
remove path env var
DaveSkender Dec 30, 2024
c3b9d1a
add back ismainbranc
DaveSkender Dec 30, 2024
fd35923
update config outs
DaveSkender Dec 30, 2024
01c4465
update config
DaveSkender Dec 30, 2024
e324ff1
source v branches from main
DaveSkender Dec 30, 2024
c906169
use unknown
DaveSkender Dec 30, 2024
69bf207
updates with -arguments
DaveSkender Dec 30, 2024
9065944
update dotnet syntax
DaveSkender Dec 30, 2024
70ad8a0
updates with CLI variant
DaveSkender Dec 31, 2024
7bb9b4e
use general preview label
DaveSkender Dec 31, 2024
d5e1ffb
update labels
DaveSkender Dec 31, 2024
2457f01
Update gitversion.yml
DaveSkender Dec 31, 2024
168202d
Update deploy-package.yml
DaveSkender Dec 31, 2024
721fed0
Update deploy-package.yml
DaveSkender Dec 31, 2024
878858c
Update deploy-package.yml
DaveSkender Dec 31, 2024
5d22ad4
Update deploy-package.yml
DaveSkender Dec 31, 2024
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
215 changes: 144 additions & 71 deletions .github/workflows/deploy-package.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
name: Deploy NuGet package

# Version format: major.minor.patch[-preview.suffix]
# - Versioning managed by GitVersion
# - Preview suffix toggled by "preview" input
# - Non-preview versions on nuget.org restricted to main branch;
# runs targeting nuget.org from non-"main" branches will fail early.
# - Default: patch increment unless "+semver:" message found
# or will use "next-version" if set in config
# - Preview numbers auto-increment (e.g., preview.1 -> preview.2)
# - Branches are only tagged with the version after deployments to nuget.org

on:
push:
branches:
- main
- 'v*'
- fix-deployer

workflow_dispatch:
inputs:
environment:
Expand All @@ -23,66 +39,121 @@ on:
required: true

concurrency:
group: ${{ inputs.environment }}
group: ${{ github.ref_name }}
cancel-in-progress: true

jobs:
build:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Debug GitVersion config presence
run: cat src/gitversion.yml

- name: Setup GitVersion Tool
run: dotnet tool install --global GitVersion.Tool --version 6.0.5

- name: Show GitVersion config
run: dotnet-gitversion /showconfig /config src/gitversion.yml

- name: Show GitVersion config (with Json output)
run: dotnet-gitversion /output json /l console /config src/gitversion.yml

- name: Validate preview settings
run: |
if [[ "${{ github.event.inputs.environment }}" == "nuget.org" ]] && \
[[ "${{ github.ref }}" != "refs/heads/main" ]] && \
[[ "${{ github.event.inputs.preview }}" != "true" ]]; then
echo "::error::Non-preview versions on nuget.org are only allowed from main branch"
exit 1
fi

package:
needs: validate
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version_info.outputs.version }}
pkg_url: ${{ steps.package_info.outputs.pkg_url }}
pkg_name: ${{ steps.package_info.outputs.pkg_name }}
url: ${{ steps.package_info.outputs.url }}
name: ${{ steps.package_info.outputs.name }}
environ: ${{ steps.settings.outputs.environ }}
preview: ${{ steps.settings.outputs.preview }}
dry_run: ${{ steps.settings.outputs.dry_run }}

steps:

- name: Set default inputs for dispatch
id: settings
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "environ=${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "preview=${{ inputs.preview }}" >> $GITHUB_OUTPUT
echo "dry_run=${{ inputs.dry_run }}" >> $GITHUB_OUTPUT
else
echo "environ=NO_ENVIRONMENT" >> $GITHUB_OUTPUT
echo "preview=true" >> $GITHUB_OUTPUT
echo "dry_run=true" >> $GITHUB_OUTPUT
fi

- name: Checkout source
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.x"
dotnet-quality: "ga"
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y libxml2-utils

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.x"
dotnet-quality: "ga"

- name: Setup GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: "5.x"
preferLatestVersion: true

- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y libxml2-utils
- name: Debug Git state
run: |
git branch
git status
git log --oneline -n 5

- name: Setup GitVersion Tool
run: |
# Install GitVersion as a global tool
dotnet tool install --global GitVersion.Tool --version 6.0.5
# Add the tool to PATH
echo "$HOME/.dotnet/tools" >> $GITHUB_PATH

- name: Determine version
id: gitversion
uses: gittools/actions/gitversion/[email protected]
with:
updateAssemblyInfo: true
useConfigFile: true
configFilePath: src/gitversion.yml
run: |
# Execute GitVersion and capture JSON output
gitversion /output json /config src/gitversion.yml > gitversion.json
# Extract desired outputs
version=$(jq -r '.SemVer' gitversion.json)
prerelease_tag=$(jq -r '.PreReleaseTag' gitversion.json)
prerelease_number=$(jq -r '.PreReleaseNumber' gitversion.json)
echo "SemVer=$version" >> $GITHUB_OUTPUT
echo "PreReleaseTag=$prerelease_tag" >> $GITHUB_OUTPUT
echo "PreReleaseNumber=$prerelease_number" >> $GITHUB_OUTPUT

- name: Compose version
id: version_info
run: |
# get base version
base="${{ steps.gitversion.outputs.majorMinorPatch }}"

# determine preview suffix
preview_tag="${{ inputs.preview && '-preview.' || '' }}"

# determine preview number
preview_num="${{ inputs.preview && steps.gitversion.outputs.preReleaseNumber || '' }}"
# get base version from GitVersion (respects tags on main, next-version on others)
base="${{ steps.gitversion.outputs.SemVer }}"

# preview suffix controlled by workflow input + gitversion config
if [[ "${{ steps.settings.outputs.preview }}" == "true" ]]; then
# respect GitVersion preview tag config from gitversion.yml
preview_tag="-${{ steps.gitversion.outputs.PreReleaseTag }}."
preview_num="${{ steps.gitversion.outputs.PreReleaseNumber }}"
else
preview_tag=""
preview_num=""
fi

# combine all parts
ver="${base}${preview_tag}${preview_num}"

echo "version=$ver" >> "$GITHUB_OUTPUT"
Expand All @@ -91,11 +162,11 @@ jobs:
id: package_info
run: |
PACKAGE_NAME=$(xmllint --xpath "//PropertyGroup/PackageId/text()" src/Indicators.csproj)
echo "pkg_name=${PACKAGE_NAME}" >> $GITHUB_OUTPUT
if [[ "${{ inputs.environment }}" == "nuget.org" ]]; then
echo "pkg_url=https://www.nuget.org/packages/${PACKAGE_NAME}/${{ steps.version_info.outputs.version }}" >> $GITHUB_OUTPUT
echo "name=${PACKAGE_NAME}" >> $GITHUB_OUTPUT
if [[ "${{ steps.settings.outputs.environ }}" == "nuget.org" ]]; then
echo "url=https://www.nuget.org/packages/${PACKAGE_NAME}/${{ steps.version_info.outputs.version }}" >> $GITHUB_OUTPUT
else
echo "pkg_url=https://github.com/${{ github.repository }}/packages/nuget/${PACKAGE_NAME}/${{ steps.version_info.outputs.version }}" >> $GITHUB_OUTPUT
echo "url=https://github.com/${{ github.repository }}/packages/nuget/${PACKAGE_NAME}/${{ steps.version_info.outputs.version }}" >> $GITHUB_OUTPUT
fi

- name: Build library
Expand Down Expand Up @@ -125,17 +196,18 @@ jobs:
- name: Summary output
run: |
{
echo "| Version No. | Component |"
echo "| :---------- | :---------------------------------------------- |"
echo "| Major | ${{ steps.gitversion.outputs.major }} |"
echo "| Minor | ${{ steps.gitversion.outputs.minor }} |"
echo "| Patch | ${{ steps.gitversion.outputs.patch }} |"
echo "| Base | ${{ steps.gitversion.outputs.majorMinorPatch }} |"
echo "| Composed | ${{ steps.version_info.outputs.version }} |"
echo "| Version No. | Component |"
echo "| :---------------- | :----------------------------------------------- |"
echo "| SemVer | ${{ steps.gitversion.outputs.SemVer }} |"
echo "| PreReleaseTag | ${{ steps.gitversion.outputs.PreReleaseTag }} |"
echo "| PreReleaseNumber | ${{ steps.gitversion.outputs.PreReleaseNumber }} |"
echo "| Composed | ${{ steps.version_info.outputs.version }} |"
echo "| Package | ${{ steps.package_info.outputs.name }} |"
echo "| Package URL | ${{ steps.package_info.outputs.url }} |"
} >> $GITHUB_STEP_SUMMARY

deploy:
needs: build
needs: package
runs-on: ubuntu-latest
if: success()

Expand All @@ -144,8 +216,18 @@ jobs:
packages: write

environment:
name: ${{ !inputs.dry_run && inputs.environment || '' }}
url: ${{ needs.build.outputs.pkg_url }}
name: ${{ needs.package.outputs.environ }}
url: ${{ needs.package.outputs.url }}

env:
version: ${{ needs.package.outputs.version }}
preview: ${{ needs.package.outputs.preview }}
dry_run: ${{ needs.package.outputs.dry_run }}
environ: ${{ needs.package.outputs.environ }}
url: ${{ needs.package.outputs.url }}
name: ${{ needs.package.outputs.name }}
NUGET_PUBLISH_URL: ${{ needs.package.outputs.environ == 'nuget.org' && 'https://api.nuget.org/v3/index.json' || 'https://nuget.pkg.github.com/${{ github.repository }}/index.json' }}
NUGET_API_KEY: ${{ needs.package.outputs.environ == 'nuget.org' && secrets.NUGET_TOKEN || secrets.GITHUB_TOKEN }}

steps:

Expand All @@ -155,47 +237,38 @@ jobs:
dotnet-version: "9.x"
dotnet-quality: "ga"

- name: Setup NuGet
uses: nuget/setup-nuget@v2
with:
nuget-api-key: ${{ secrets.NUGET_TOKEN }}
nuget-version: '6.x'


- name: Download package
uses: actions/download-artifact@v4
with:
name: packages
path: NuGet

- name: Publish package
if: ${{ !inputs.dry_run }}
env:
API_KEY: ${{ inputs.environment == 'nuget.org' && secrets.NUGET_TOKEN || secrets.GITHUB_TOKEN }}
if: ${{ !env.dry_run }}
run: >
dotnet nuget push NuGet/*.nupkg
--source "${{ vars.NUGET_PUBLISH_URL }}"
--api-key "$API_KEY"
--source "${{ env.NUGET_PUBLISH_URL }}"
--api-key "${{ env.NUGET_API_KEY }}"
--skip-duplicate

- name: Tag and draft release note
uses: ncipollo/release-action@v1
if: ${{ !inputs.dry_run && inputs.environment == 'nuget.org' }}
if: ${{ !env.dry_run && env.environ == 'nuget.org' }}
with:
body: |
## Release ${{ needs.build.outputs.version }}
## Release ${{ env.version }}

📦 Package deployed to [${{ inputs.environment }}](${{ needs.build.outputs.pkg_url }})
📦 Package deployed to [${{ env.environ }}](${{ env.url }})

### Package Details
- **Name**: ${{ needs.build.outputs.pkg_name }}
- **Version**: ${{ needs.build.outputs.version }}
- **Preview**: ${{ inputs.preview && 'Yes' || 'No' }}
- **Name**: ${{ env.name }}
- **Version**: ${{ env.version }}
- **Preview**: ${{ env.preview && 'Yes' || 'No' }}
generateReleaseNotes: true
draft: true
makeLatest: ${{ !inputs.preview }}
prerelease: ${{ inputs.preview }}
tag: v${{ needs.build.outputs.version }}
makeLatest: ${{ !env.preview }}
prerelease: ${{ env.preview }}
tag: v${{ env.version }}
commit: ${{ github.sha }}
token: ${{ secrets.GITHUB_TOKEN }}

Expand All @@ -206,10 +279,10 @@ jobs:
echo "## Package Deployment"
echo "| Parameter | Value |"
echo "|:------------|:------|"
echo "| Mode | ${{ inputs.dry_run && '🔍 DRY RUN' || '🚀 DEPLOY' }} |"
echo "| Mode | ${{ env.dry_run && '🔍 DRY RUN' || '🚀 DEPLOY' }} |"
echo "| Status | ${{ job.status == 'success' && '✅ Success' || '❌ Failed' }} |"
echo "| Environment | ${{ inputs.environment }} |"
echo "| Version | ${{ needs.build.outputs.version }} |"
echo "| Package | [${{ needs.build.outputs.pkg_name }}](${{ needs.build.outputs.pkg_url }}) |"
echo "| Preview | ${{ inputs.preview && '✓' || '✗' }} |"
echo "| Environment | ${{ env.environ }} |"
echo "| Version | ${{ env.version }} |"
echo "| Package | [${{ env.name }}](${{ env.url }}) |"
echo "| Preview | ${{ env.preview && '✓' || '✗' }} |"
} >> $GITHUB_STEP_SUMMARY
2 changes: 1 addition & 1 deletion .github/workflows/test-codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: ["main"]

pull_request:
branches: ["*"]
branches: ["main"]

schedule:
- cron: '37 14 * * 3'
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// file settings
"files.autoSave": "off",
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 3000,
"files.encoding": "utf8",
"files.exclude": {
Expand Down
27 changes: 23 additions & 4 deletions src/gitversion.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# https://gitversion.net/docs/reference/configuration

mode: ContinuousDelivery
next-version: 3.0.0

Expand All @@ -8,7 +10,24 @@ patch-version-bump-message: '\+semver:\s?(fix|patch)'
no-bump-message: '\+semver:\s?(none|skip)'

branches:
pull-request:
tag: preview.
feature:
tag: preview.
main:
increment: Patch
source-branches: []
is-main-branch: true
label: ""

vNext:
regex: v[0-9].*
increment: Patch
source-branches: [main]
tag: "preview."

unknown:
increment: Patch
source-branches: [main,vNext]
tag: "preview."

ignore:
sha: []

merge-message-formats: {}
Loading