mirror of
https://github.com/actions/setup-dotnet.git
synced 2026-11-20 19:43:07 +00:00
Add support for installing .NET runtimes alongside SDKs (#806)
* Add support for installing .NET runtimes alongside SDKs * Add tests for preview runtime pairing and fallback logic in installer
This commit is contained in:
237
src/installer.ts
237
src/installer.ts
@@ -10,6 +10,7 @@ import {
|
||||
lstatSync,
|
||||
mkdtempSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
statSync
|
||||
} from 'fs';
|
||||
@@ -36,9 +37,25 @@ interface ReleaseIndexResponse {
|
||||
'releases-index': ReleaseIndexEntry[];
|
||||
}
|
||||
|
||||
interface RuntimeReleasesResponse {
|
||||
releases?: {
|
||||
runtime?: {version?: string} | null;
|
||||
'aspnetcore-runtime'?: {version?: string} | null;
|
||||
}[];
|
||||
}
|
||||
|
||||
const QUALITY_INPUT_MINIMAL_MAJOR_TAG = 6;
|
||||
const LATEST_PATCH_SYNTAX_MINIMAL_MAJOR_TAG = 5;
|
||||
|
||||
function hasAspNetCoreRuntime(version: string): boolean {
|
||||
const match = version.match(/^(\d+)\.(\d+)/);
|
||||
return (
|
||||
!match ||
|
||||
Number(match[1]) > 2 ||
|
||||
(Number(match[1]) === 2 && Number(match[2]) >= 1)
|
||||
);
|
||||
}
|
||||
|
||||
function channelForMajor(major: string): string {
|
||||
// Starting with .NET 5, the minor version is always zero.
|
||||
// Hardcode the earlier versions because they will not get new releases.
|
||||
@@ -700,6 +717,10 @@ export class DotnetCoreInstaller {
|
||||
);
|
||||
}
|
||||
|
||||
return this.findMatchingVersion(allowed);
|
||||
}
|
||||
|
||||
private findMatchingVersion(allowed: string[]): string | null {
|
||||
if (semver.valid(this.version)) {
|
||||
return allowed.find(version => version === this.version) ?? null;
|
||||
}
|
||||
@@ -771,6 +792,67 @@ export class DotnetCoreInstaller {
|
||||
return null;
|
||||
}
|
||||
|
||||
private getInstalledRuntimeVersions(framework: string): string[] {
|
||||
const runtimeDir = path.join(DotnetInstallDir.dirPath, 'shared', framework);
|
||||
try {
|
||||
return readdirSync(runtimeDir, {withFileTypes: true})
|
||||
.filter(entry => entry.isDirectory() || entry.isSymbolicLink())
|
||||
.map(entry => entry.name)
|
||||
.filter(version => semver.valid(version) !== null)
|
||||
.filter(version =>
|
||||
isFile(path.join(runtimeDir, version, `${framework}.deps.json`))
|
||||
);
|
||||
} catch {
|
||||
core.debug(`Unable to read the runtime directory '${runtimeDir}'.`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private findLocalRuntimeVersion(): string | null {
|
||||
if (!this.hasDotnetMuxer()) {
|
||||
return null;
|
||||
}
|
||||
const dotnetVersions = this.getInstalledRuntimeVersions(
|
||||
'Microsoft.NETCore.App'
|
||||
);
|
||||
const aspnetVersions = this.getInstalledRuntimeVersions(
|
||||
'Microsoft.AspNetCore.App'
|
||||
);
|
||||
// Preview pairs can have different build numbers, so a preview ASP.NET Core
|
||||
// runtime is paired with the .NET runtime declared in its runtimeconfig.json.
|
||||
const pairedVersions = new Set([
|
||||
...aspnetVersions,
|
||||
...aspnetVersions
|
||||
.filter(version => semver.prerelease(version))
|
||||
.flatMap(version => this.getDeclaredRuntimeVersion(version) ?? [])
|
||||
]);
|
||||
return this.findMatchingVersion(
|
||||
dotnetVersions.filter(
|
||||
version => !hasAspNetCoreRuntime(version) || pairedVersions.has(version)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private getDeclaredRuntimeVersion(aspnetVersion: string): string | undefined {
|
||||
const configPath = path.join(
|
||||
DotnetInstallDir.dirPath,
|
||||
'shared',
|
||||
'Microsoft.AspNetCore.App',
|
||||
aspnetVersion,
|
||||
'Microsoft.AspNetCore.App.runtimeconfig.json'
|
||||
);
|
||||
try {
|
||||
const framework = JSON.parse(readFileSync(configPath, 'utf8'))
|
||||
.runtimeOptions?.framework;
|
||||
return framework?.name === 'Microsoft.NETCore.App'
|
||||
? framework.version
|
||||
: undefined;
|
||||
} catch {
|
||||
core.debug(`Unable to read the runtime pairing from '${configPath}'.`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
public async installDotnet(): Promise<string | null> {
|
||||
// Before findLocalSdkVersion(), which looks under the resolved directory.
|
||||
DotnetInstallDir.setEnvironmentVariable();
|
||||
@@ -863,6 +945,161 @@ export class DotnetCoreInstaller {
|
||||
return this.parseInstalledVersion(dotnetInstallOutput.stdout);
|
||||
}
|
||||
|
||||
public async installRuntime(): Promise<string | null> {
|
||||
this.validateRuntimeVersion();
|
||||
DotnetInstallDir.setEnvironmentVariable();
|
||||
const isCrossArch =
|
||||
!!this.architecture &&
|
||||
normalizeArch(this.architecture) !== normalizeArch(os.arch());
|
||||
|
||||
if (!this.checkLatest && !isCrossArch) {
|
||||
const localVersion = this.findLocalRuntimeVersion();
|
||||
if (localVersion) {
|
||||
core.info(
|
||||
`'check-latest' is false and locally installed .NET runtimes (${localVersion}) satisfy the '${this.version}' request. Skipping download.`
|
||||
);
|
||||
return localVersion;
|
||||
}
|
||||
core.info(
|
||||
`'check-latest' is false but no complete local runtime installation satisfies the '${this.version}' request. Falling back to online installation.`
|
||||
);
|
||||
}
|
||||
|
||||
const versionResolver = new DotnetVersionResolver(
|
||||
this.version,
|
||||
this.quality,
|
||||
this.dotnetChannel
|
||||
);
|
||||
const dotnetVersion = await versionResolver.createDotnetVersion();
|
||||
if (this.quality && !dotnetVersion.qualityFlag) {
|
||||
core.warning(
|
||||
`The 'dotnet-quality' input can be used only with .NET runtime channels in A.B, A.B.x, A or A.x formats where the major tag is higher than 5, or 'latest' resolving to such a channel. You specified: ${dotnetVersion.value}. 'dotnet-quality' input is ignored for this runtime.`
|
||||
);
|
||||
}
|
||||
|
||||
const installAspNetCore = hasAspNetCoreRuntime(dotnetVersion.value);
|
||||
const aspnetVersion = installAspNetCore
|
||||
? await this.resolveAspNetCoreVersion(dotnetVersion)
|
||||
: undefined;
|
||||
if (!installAspNetCore) {
|
||||
core.warning(
|
||||
`ASP.NET Core shared runtimes are not available before .NET Core 2.1. Installing only the .NET runtime for '${this.version}'.`
|
||||
);
|
||||
}
|
||||
|
||||
const architectureArguments = isCrossArch
|
||||
? [
|
||||
IS_WINDOWS ? '-InstallDir' : '--install-dir',
|
||||
IS_WINDOWS
|
||||
? `"${path.join(DotnetInstallDir.dirPath, this.architecture!)}"`
|
||||
: path.join(DotnetInstallDir.dirPath, this.architecture!)
|
||||
]
|
||||
: [];
|
||||
|
||||
// Install the .NET runtime (Microsoft.NETCore.App) first, then the
|
||||
// ASP.NET Core runtime (Microsoft.AspNetCore.App)
|
||||
const dotnetRuntimeStdout = await this.installSharedRuntime(
|
||||
'dotnet',
|
||||
dotnetVersion,
|
||||
architectureArguments
|
||||
);
|
||||
if (aspnetVersion) {
|
||||
await this.installSharedRuntime(
|
||||
'aspnetcore',
|
||||
aspnetVersion,
|
||||
architectureArguments
|
||||
);
|
||||
}
|
||||
|
||||
// The output remains the base runtime version, even when build numbers differ.
|
||||
return this.parseInstalledVersion(dotnetRuntimeStdout);
|
||||
}
|
||||
|
||||
// Runs before the resolver, whose errors name 'dotnet-version'.
|
||||
private validateRuntimeVersion(): void {
|
||||
if (
|
||||
this.version.toLowerCase() === 'latest' ||
|
||||
semver.validRange(this.version)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
// Feature bands (A.B.Cxx) exist only for SDKs, so the install script can't resolve them for runtimes.
|
||||
const reason = DotnetCoreInstaller.FeatureBandSyntax.test(this.version)
|
||||
? ` The A.B.Cxx feature band syntax applies only to .NET SDKs and is supported only by 'dotnet-version'.`
|
||||
: '';
|
||||
throw new Error(
|
||||
`The 'dotnet-runtime' was supplied in invalid format: ${this.version}!${reason} Supported syntax: A.B.C, A.B, A.B.x, A, A.x, latest`
|
||||
);
|
||||
}
|
||||
|
||||
private async resolveAspNetCoreVersion(
|
||||
dotnetVersion: DotnetVersion
|
||||
): Promise<DotnetVersion> {
|
||||
const version = dotnetVersion.value;
|
||||
if (!semver.valid(version) || !semver.prerelease(version)) {
|
||||
return dotnetVersion;
|
||||
}
|
||||
|
||||
const channel = `${semver.major(version)}.${semver.minor(version)}`;
|
||||
const httpClient = new hc.HttpClient('actions/setup-dotnet', [], {
|
||||
allowRetries: true,
|
||||
maxRetries: 3
|
||||
});
|
||||
try {
|
||||
const response = await httpClient.getJson<RuntimeReleasesResponse>(
|
||||
`https://builds.dotnet.microsoft.com/dotnet/release-metadata/${channel}/releases.json`
|
||||
);
|
||||
const releases = response.result?.releases;
|
||||
const release = Array.isArray(releases)
|
||||
? releases.find(entry => entry?.runtime?.version === version)
|
||||
: undefined;
|
||||
const aspnetVersion = release?.['aspnetcore-runtime']?.version;
|
||||
if (
|
||||
response.statusCode === 200 &&
|
||||
typeof aspnetVersion === 'string' &&
|
||||
semver.valid(aspnetVersion)
|
||||
) {
|
||||
return {...dotnetVersion, value: aspnetVersion};
|
||||
}
|
||||
core.warning(
|
||||
`No ASP.NET Core release mapping was found for '${version}' (HTTP ${response.statusCode}). Trying the same exact version for this potentially unpublished build; installation failures remain fatal.`
|
||||
);
|
||||
} catch (error) {
|
||||
core.warning(
|
||||
`Unable to resolve the ASP.NET Core release paired with '${version}': ${error instanceof Error ? error.message : String(error)}. Trying the same exact version; installation failures remain fatal.`
|
||||
);
|
||||
} finally {
|
||||
httpClient.dispose();
|
||||
}
|
||||
return dotnetVersion;
|
||||
}
|
||||
|
||||
private async installSharedRuntime(
|
||||
runtime: 'dotnet' | 'aspnetcore',
|
||||
dotnetVersion: DotnetVersion,
|
||||
architectureArguments: string[]
|
||||
): Promise<string> {
|
||||
const runtimeInstallOutput = await new DotnetInstallScript()
|
||||
.useArchitecture(this.architecture)
|
||||
// If dotnet CLI is already installed - avoid overwriting it
|
||||
.useArguments(
|
||||
IS_WINDOWS ? '-SkipNonVersionedFiles' : '--skip-non-versioned-files'
|
||||
)
|
||||
.useArguments(IS_WINDOWS ? '-Runtime' : '--runtime', runtime)
|
||||
// Use version provided by user
|
||||
.useVersion(dotnetVersion, dotnetVersion.qualityFlag ? this.quality : '')
|
||||
.useArguments(...architectureArguments)
|
||||
.execute();
|
||||
|
||||
if (runtimeInstallOutput.exitCode) {
|
||||
throw new Error(
|
||||
`Failed to install ${runtime} runtime, exit code: ${runtimeInstallOutput.exitCode}. ${runtimeInstallOutput.stderr}`
|
||||
);
|
||||
}
|
||||
|
||||
return runtimeInstallOutput.stdout;
|
||||
}
|
||||
|
||||
private parseInstalledVersion(stdout: string): string | null {
|
||||
const regex = /(?<version>\d+\.\d+\.\d+[a-z0-9._-]*)/gm;
|
||||
const matchedResult = regex.exec(stdout);
|
||||
|
||||
@@ -49,6 +49,7 @@ export async function run() {
|
||||
//
|
||||
// dotnet-version is optional, but needs to be provided for most use cases.
|
||||
// If supplied, install / use from the tool cache.
|
||||
// dotnet-runtime is optional and allows installing runtime-only versions.
|
||||
// global-version-file may be specified to point to a specific global.json
|
||||
// and will be used to install an additional version.
|
||||
// If not supplied, look for version in ./global.json.
|
||||
@@ -71,12 +72,13 @@ export async function run() {
|
||||
globalJsonConstraints.set(version, {minimumVersion, rollForward});
|
||||
}
|
||||
};
|
||||
const runtimeVersions = core.getMultilineInput('dotnet-runtime');
|
||||
const installedDotnetVersions: (string | null)[] = [];
|
||||
const architecture = getArchitectureInput();
|
||||
const checkLatest = getCheckLatestInput();
|
||||
let dotnetChannel = core.getInput('dotnet-channel');
|
||||
|
||||
const isLatestRequested = versions.some(
|
||||
const isLatestRequested = [...versions, ...runtimeVersions].some(
|
||||
version => version && version.toLowerCase() === 'latest'
|
||||
);
|
||||
if (dotnetChannel && !isValidChannel(dotnetChannel)) {
|
||||
@@ -92,7 +94,7 @@ export async function run() {
|
||||
}
|
||||
} else if (dotnetChannel && !isLatestRequested) {
|
||||
core.warning(
|
||||
`The 'dotnet-channel' input is only supported when 'dotnet-version' is set to 'latest'.`
|
||||
`The 'dotnet-channel' input is only supported when 'dotnet-version' or 'dotnet-runtime' is set to 'latest'.`
|
||||
);
|
||||
dotnetChannel = '';
|
||||
}
|
||||
@@ -114,22 +116,26 @@ export async function run() {
|
||||
const globalJsonPath = path.join(process.cwd(), 'global.json');
|
||||
if (fs.existsSync(globalJsonPath)) {
|
||||
addVersionFromGlobalJson(globalJsonPath);
|
||||
} else {
|
||||
} else if (!runtimeVersions.length) {
|
||||
core.info(
|
||||
`The global.json wasn't found in the root directory. No .NET version will be installed.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const quality = core.getInput('dotnet-quality') as QualityOptions;
|
||||
|
||||
if (
|
||||
(versions.length || runtimeVersions.length) &&
|
||||
quality &&
|
||||
!qualityOptions.includes(quality)
|
||||
) {
|
||||
throw new Error(
|
||||
`Value '${quality}' is not supported for the 'dotnet-quality' option. Supported values are: daily, preview, ga.`
|
||||
);
|
||||
}
|
||||
|
||||
if (versions.length) {
|
||||
const quality = core.getInput('dotnet-quality') as QualityOptions;
|
||||
|
||||
if (quality && !qualityOptions.includes(quality)) {
|
||||
throw new Error(
|
||||
`Value '${quality}' is not supported for the 'dotnet-quality' option. Supported values are: daily, preview, ga.`
|
||||
);
|
||||
}
|
||||
|
||||
const skipLtsPrepass = core.getBooleanInput('skip-lts-prepass');
|
||||
|
||||
let dotnetInstaller: DotnetCoreInstaller;
|
||||
@@ -151,15 +157,7 @@ export async function run() {
|
||||
const installedVersion = await dotnetInstaller.installDotnet();
|
||||
installedDotnetVersions.push(installedVersion);
|
||||
}
|
||||
if (
|
||||
architecture &&
|
||||
normalizeArch(architecture) !== normalizeArch(os.arch())
|
||||
) {
|
||||
process.env['DOTNET_INSTALL_DIR'] = path.join(
|
||||
DotnetInstallDir.dirPath,
|
||||
architecture
|
||||
);
|
||||
}
|
||||
setDotnetInstallDir(architecture);
|
||||
DotnetInstallDir.addToPath();
|
||||
|
||||
const workloadsInput = core.getInput('workloads');
|
||||
@@ -186,6 +184,30 @@ export async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
if (runtimeVersions.length) {
|
||||
const uniqueRuntimeVersions = new Set<string>(
|
||||
runtimeVersions.map(v => (v.toLowerCase() === 'latest' ? 'latest' : v))
|
||||
);
|
||||
// Runtimes have no SDK feature bands, so A.B.Cxx uses its A.B channel.
|
||||
const runtimeChannel = dotnetChannel.replace(/^(\d+\.\d+)\.\dxx$/, '$1');
|
||||
for (const runtimeVersion of uniqueRuntimeVersions) {
|
||||
const dotnetInstaller = new DotnetCoreInstaller(
|
||||
runtimeVersion,
|
||||
quality,
|
||||
architecture,
|
||||
runtimeVersion === 'latest' ? runtimeChannel : undefined,
|
||||
checkLatest
|
||||
);
|
||||
await dotnetInstaller.installRuntime();
|
||||
}
|
||||
// installRuntime() resets DOTNET_INSTALL_DIR to the base directory.
|
||||
setDotnetInstallDir(architecture);
|
||||
// The SDK installation has already added the same directory to PATH.
|
||||
if (!versions.length) {
|
||||
DotnetInstallDir.addToPath();
|
||||
}
|
||||
}
|
||||
|
||||
const sourceUrl: string = core.getInput('source-url');
|
||||
const configFile: string = core.getInput('config-file');
|
||||
if (sourceUrl) {
|
||||
@@ -211,6 +233,18 @@ export async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
function setDotnetInstallDir(architecture: SupportedArchitecture | ''): void {
|
||||
if (
|
||||
architecture &&
|
||||
normalizeArch(architecture) !== normalizeArch(os.arch())
|
||||
) {
|
||||
process.env['DOTNET_INSTALL_DIR'] = path.join(
|
||||
DotnetInstallDir.dirPath,
|
||||
architecture
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function getArchitectureInput(): SupportedArchitecture | '' {
|
||||
const raw = (core.getInput('architecture') || '').trim();
|
||||
if (!raw) return '';
|
||||
|
||||
Reference in New Issue
Block a user