Skip to content

Commit 8471095

Browse files
committed
Added GitHub ref names to CI builds. Added not using Infected Libraries CI NuGet source in release builds. Added using non-standard NuGet feed for restoring.
1 parent f42258d commit 8471095

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

.github/workflows/Biohazrd.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ env:
1919
DOTNET_CLI_TELEMETRY_OPTOUT: true
2020
DOTNET_GENERATE_ASPNET_CERTIFICATE: false
2121
ContinuousIntegrationBuild: true
22+
# This URL will be added to the restore sources if it isn't the official NuGet.org
23+
# (This is mainly intended to allow using the NuGet.org test servers to test CI in forks.)
24+
CiNuGetApiUrl: ${{secrets.NUGET_API_URL}}
2225
jobs:
2326
build-and-test:
2427
strategy:
@@ -65,9 +68,11 @@ jobs:
6568
run: python .github/workflows/configure-build.py
6669
env:
6770
github_event_name: ${{github.event_name}}
71+
github_ref: ${{github.ref}}
6872
github_run_number: ${{github.run_number}}
6973
release_version: ${{github.event.release.tag_name}}
7074
workflow_dispatch_version: ${{github.event.inputs.version}}
75+
workflow_dispatch_will_publish_packages: ${{github.event.inputs.will_publish_packages}}
7176

7277
# ----------------------------------------------------------------------- Build
7378
- name: Restore
@@ -165,6 +170,7 @@ jobs:
165170
environment: NuGet.org
166171
# Release builds always publish packages to NuGet.org
167172
# Workflow dispatch builds will only publish packages if enabled and an explicit version number is given
173+
# Make sure this logic matches configure-build.py to ensure we don't accidentally depend on sibling CI pre-release packages
168174
if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.will_publish_packages == 'true' && github.event.inputs.version != '')
169175
steps:
170176
# ----------------------------------------------------------------------- Setup .NET

.github/workflows/configure-build.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,50 @@ def get_environment_variable(name):
2020

2121
github_event_name = get_environment_variable('github_event_name')
2222
github_run_number = get_environment_variable('github_run_number')
23+
github_ref = get_environment_variable('github_ref')
2324

2425
#==================================================================================================
2526
# Determine build settings
2627
#==================================================================================================
2728

28-
version = f'0.0.0-ci{github_run_number}'
29+
# For GitHub refs besides main, include the branch/tag name in the default version string
30+
ref_part = ''
31+
if github_ref != 'refs/heads/main':
32+
ref = github_ref
2933

34+
# Strip the ref prefix
35+
branch_prefix = 'refs/heads/'
36+
tag_prefix = 'refs/tags/'
37+
if ref.startswith(branch_prefix):
38+
ref = ref[len(branch_prefix):]
39+
elif ref.startswith(tag_prefix):
40+
ref = f'tag-{ref[len(tag_prefix):]}'
41+
42+
# Replace illegal characters with dashes
43+
ref = re.sub('[^0-9A-Za-z-]', '-', ref)
44+
45+
# Make the ref part
46+
ref_part = f'-{ref}'
47+
48+
# Build the default version string
49+
version = f'0.0.0{ref_part}-ci{github_run_number}'
50+
is_for_release = False
51+
52+
# Handle non-default version strings
53+
# Make sure logic relating to is_for_release matches the publish-packages-nuget-org in the workflow
3054
if github_event_name == 'release':
3155
version = get_environment_variable('release_version')
56+
is_for_release = True
3257
elif github_event_name == 'workflow_dispatch':
3358
workflow_dispatch_version = get_environment_variable('workflow_dispatch_version')
59+
workflow_dispatch_will_publish_packages = get_environment_variable('workflow_dispatch_will_publish_packages')
60+
3461
if workflow_dispatch_version is not None:
3562
version = workflow_dispatch_version
3663

64+
if workflow_dispatch_will_publish_packages.lower() == 'true':
65+
is_for_release = True
66+
3767
# Trim leading v off of version if present
3868
if version.startswith('v'):
3969
version = version[1:]
@@ -48,8 +78,9 @@ def get_environment_variable(name):
4878
#==================================================================================================
4979
# Emit MSBuild properties
5080
#==================================================================================================
51-
print(f"Configuring build environment to build version {version}")
81+
print(f"Configuring build environment to build{' and release' if is_for_release else ''} version {version}")
5282
gha.set_environment_variable('CiBuildVersion', version)
83+
gha.set_environment_variable('CiIsForRelease', str(is_for_release).lower())
5384

5485
#==================================================================================================
5586
# Final check to exit with an error code if any errors were printed

tooling/Common.csproj.props

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
<Features>strict</Features>
77
<Nullable>enable</Nullable>
88
<AnalysisLevel>5.0</AnalysisLevel>
9-
10-
<RestoreSources>https://api.nuget.org/v3/index.json;https://nuget-ci.infectedlibraries.net/index.json</RestoreSources>
11-
<!-- When building CI builds destined to be published on a public NuGet feed we only want to use it for restores -->
12-
<RestoreSources Condition="'$(CiNuGetApiUrl)' != ''">$(CiNuGetApiUrl)</RestoreSources>
139

1410
<!-- Common NuGet Package Properties -->
1511
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
@@ -47,4 +43,17 @@
4743
<!-- Package the license file -->
4844
<None Include="$(MSBuildThisFileDirectory)../LICENSE.txt" Pack="true" PackagePath="" Visible="false" />
4945
</ItemGroup>
46+
<!-- ================================================================================================================
47+
Configure NuGet package restore sources
48+
================================================================================================================= -->
49+
<PropertyGroup>
50+
<OfficialNuGetApiUrl>https://api.nuget.org/v3/index.json</OfficialNuGetApiUrl>
51+
<RestoreSources>$(OfficialNuGetApiUrl)</RestoreSources>
52+
53+
<!-- Append CI-specific NuGet API if it's not the official NuGet API -->
54+
<RestoreSources Condition="'$(CiNuGetApiUrl)' != '' and '$(CiNuGetApiUrl)' != '$(OfficialNuGetApiUrl)'">$(RestoreSources);$(CiNuGetApiUrl)</RestoreSources>
55+
56+
<!-- Allow consuming sibling CI packages if this build is not for packages which will be released publicly -->
57+
<RestoreSources Condition="'$(CiIsForRelease)' != 'true'">$(RestoreSources);https://nuget-ci.infectedlibraries.net/index.json</RestoreSources>
58+
</PropertyGroup>
5059
</Project>

0 commit comments

Comments
 (0)