@@ -13,6 +13,10 @@ permissions:
1313jobs :
1414 pack_and_publish :
1515 runs-on : ubuntu-latest
16+ permissions :
17+ contents : read
18+ packages : write
19+ id-token : write
1620 steps :
1721 - uses : actions/checkout@v4
1822 with :
@@ -22,18 +26,48 @@ jobs:
2226 id : version
2327 shell : bash
2428 run : |
25- if [[ "${{ github.ref }}" =~ ^refs/tags/v ]]; then
26- REF="${{ github.ref }}"
29+ REF="${{ github.ref }}"
30+ EVENT="${{ github.event_name }}"
31+ CSPROJ="src/Kapusch.FacebookApisForAndroidComponents/Kapusch.FacebookApisForAndroidComponents.csproj"
32+ BASE_VERSION=$(grep -oE '<Version>[^<]+' "$CSPROJ" | head -n 1 | sed 's/<Version>//' || echo "0.1.0")
33+
34+ if [[ "$REF" =~ ^refs/tags/v ]]; then
2735 VERSION="${REF#refs/tags/v}"
36+ PUBLISH_TARGET="nuget"
37+
38+ git fetch origin +refs/heads/*:refs/remotes/origin/*
39+ TAGGED_SHA="${{ github.sha }}"
40+ CONTAINING_BRANCHES=$(git for-each-ref --format='%(refname:short)' refs/remotes/origin --contains "$TAGGED_SHA")
41+
42+ ON_MASTER="false"
43+ ON_RELEASE="false"
44+ while IFS= read -r branch; do
45+ [[ "$branch" == "origin/master" ]] && ON_MASTER="true"
46+ [[ "$branch" == origin/release/* ]] && ON_RELEASE="true"
47+ done <<< "$CONTAINING_BRANCHES"
48+
49+ if [[ "$VERSION" == *-* ]]; then
50+ if [[ "$ON_RELEASE" != "true" ]]; then
51+ echo "ERROR: Pre-release tag v$VERSION must point to a commit contained in origin/release/*"
52+ exit 1
53+ fi
54+ else
55+ if [[ "$ON_MASTER" != "true" ]]; then
56+ echo "ERROR: Stable tag v$VERSION must point to a commit contained in origin/master"
57+ exit 1
58+ fi
59+ fi
2860 else
29- CSPROJ="src/Kapusch.FacebookApisForAndroidComponents/Kapusch.FacebookApisForAndroidComponents.csproj"
30- BASE_VERSION=$(grep -oE '<Version>[^<]+' "$CSPROJ" | head -n 1 | sed 's/<Version>//' || echo "0.1.0")
3161 COMMIT_SHORT=$(git rev-parse --short HEAD)
3262 RUN_NUMBER="${{ github.run_number }}"
33- VERSION="${BASE_VERSION}-prerelease.${RUN_NUMBER}.${COMMIT_SHORT}"
63+ VERSION="${BASE_VERSION}-preview.${RUN_NUMBER}.${COMMIT_SHORT}"
64+ PUBLISH_TARGET="github"
3465 fi
66+
3567 echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
68+ echo "publish_target=${PUBLISH_TARGET}" >> "$GITHUB_OUTPUT"
3669 echo "Package version: ${VERSION}"
70+ echo "Publish target: ${PUBLISH_TARGET}"
3771
3872 - uses : actions/setup-dotnet@v4
3973 with :
@@ -72,7 +106,23 @@ jobs:
72106 -o artifacts/nuget \
73107 /p:PackageVersion="${{ steps.version.outputs.version }}"
74108
109+ - name : Push to NuGet.org
110+ if : steps.version.outputs.publish_target == 'nuget'
111+ uses : NuGet/login@v1
112+ id : nuget_login
113+ with :
114+ user : ${{ secrets.NUGET_USER }}
115+
116+ - name : Push to NuGet.org
117+ if : steps.version.outputs.publish_target == 'nuget'
118+ run : |
119+ dotnet nuget push artifacts/nuget/*.nupkg \
120+ --api-key "${{ steps.nuget_login.outputs.NUGET_API_KEY }}" \
121+ --source "https://api.nuget.org/v3/index.json" \
122+ --skip-duplicate
123+
75124 - name : Push to GitHub Packages
125+ if : steps.version.outputs.publish_target == 'github'
76126 env :
77127 NUGET_AUTH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
78128 run : |
0 commit comments