-
Notifications
You must be signed in to change notification settings - Fork 1
173 lines (142 loc) · 6.5 KB
/
pull-request.yml
File metadata and controls
173 lines (142 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
name: Publish NuGet Package
on:
pull_request:
types: [closed]
jobs:
versioning:
name: Fetch and increment version
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
outputs:
new_version: ${{ steps.set_version.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v4
#################################################
# Fetch latest NuGet package version from GitHub Packages
#################################################
- name: Fetch latest NuGet version from GitHub Packages
id: latest_nuget_version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
NUGET_PACKAGE: "DotnetViewComponents"
run: |
echo "Fetching latest NuGet version for package '$NUGET_PACKAGE' in owner '$OWNER'"
API_URL="https://api.github.com/users/$OWNER/packages/nuget/$NUGET_PACKAGE/versions"
### fetch GitHub Packages
VERSIONS_JSON=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$API_URL")
### extract latest nuget pkg version
LATEST_VERSION=$(echo "$VERSIONS_JSON" | jq -r '.[0].name // "0.0.0"')
echo "Latest NuGet package version found: $LATEST_VERSION"
echo "version=$LATEST_VERSION" >> $GITHUB_OUTPUT
- name: Echo latest NuGet version
run: echo "Current version:${{ steps.latest_nuget_version.outputs.version }}"
#################################################
# Decide increment type: patch, minor, major
#################################################
- name: Set increment type based on branch and labels
id: increment_type
run: |
BRANCH_REF="${{ github.head_ref }}"
MERGE_MESSAGE="${{ github.event.pull_request.title }} ${{ github.event.pull_request.body }}"
LABELS="${{ join(github.event.pull_request.labels.*.name, ',') }}"
TYPE="patch"
echo "Branch: $BRANCH_REF"
echo "Merge message: $MERGE_MESSAGE"
echo "Labels: $LABELS"
if [[ "$BRANCH_REF" == major/* ]] || [[ "$LABELS" == *"Major"* ]] || [[ "$MERGE_MESSAGE" =~ (?i)MAJOR[[:space:]]CHANGE ]] || [[ "$MERGE_MESSAGE" =~ (?i)BREAKING[[:space:]]CHANGE ]]; then
TYPE="major"
elif [[ "$BRANCH_REF" == feature/* ]]; then
TYPE="minor"
elif [[ "$BRANCH_REF" == fix/* ]]; then
TYPE="patch"
fi
# Major version bump triggered by new major tag is skipped here
# because version baseline is fetched from Packages.
echo "Will increment: $TYPE"
echo "TYPE=$TYPE" >> $GITHUB_OUTPUT
#################################################
# Increment semantic version
#################################################
- name: Increment package version
id: bump_version
uses: christian-draeger/increment-semantic-version@1.2.3
with:
current-version: ${{ steps.latest_nuget_version.outputs.version }}
version-fragment: ${{ steps.increment_type.outputs.TYPE }}
- name: Echo new version
run: echo "Next version:${{ steps.bump_version.outputs.next-version }}"
#################################################
# Set new version as job output for downstream jobs
#################################################
- name: Set version output for job
id: set_version
run: echo "version=${{ steps.bump_version.outputs.next-version }}" >> $GITHUB_OUTPUT
nuget_publish:
name: Build, Pack, and Publish NuGet Package
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
needs: versioning
permissions:
contents: write
packages: write
env:
BUILD_CONFIG: "Release"
PROJECT_PATH: "DotnetViewComponents/DotnetViewComponents.csproj"
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x' # Specify your target .NET version
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v4.0.1
with:
versionSpec: '6.3.0'
- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/execute@v4.0.1
- name: Display versioning outputs
run: |
echo "NuGet Version: ${{ steps.gitversion.outputs.nuGetVersionV2 }}"
echo "SemVer: ${{ steps.gitversion.outputs.semVer }}"
echo "Outputs: ${{ toJson(steps.gitversion.outputs) }}"
- name: Use version in another step
run: |
VERSION="${{ steps.gitversion.outputs.major }}.${{ steps.gitversion.outputs.minor }}.${{ steps.gitversion.outputs.patch }}"
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Set version env
run: |
echo "Using version from versioning job: ${{ needs.versioning.outputs.new_version }}"
echo "NEXT_VERSION=${{ needs.versioning.outputs.new_version }}" >> $GITHUB_ENV
- name: Restore NuGet packages
run: dotnet restore $PROJECT_PATH
- name: Build project
run: dotnet build $PROJECT_PATH --configuration $BUILD_CONFIG --no-restore
- name: Pack NuGet package
run: dotnet pack $PROJECT_PATH --configuration $BUILD_CONFIG --no-build -o out /p:PackageVersion=$NEXT_VERSION
- name: Add GitHub NuGet source
run: dotnet nuget add source --username ${{ github.actor }} --password $GITHUB_TOKEN --store-password-in-clear-text --name github "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"
- name: Publish NuGet package to GitHub Packages
run: dotnet nuget push out/*.nupkg --api-key $GITHUB_TOKEN --source "github" --skip-duplicate
#################################################
# Publish release
#################################################
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v2.3.2
with:
tag_name: v${{ needs.versioning.outputs.new_version }}
release_name: Release v${{ needs.versioning.outputs.new_version }}
files: ./package/*.nupkg
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}