UNPKG

@droidsolutions-oss/semantic-release-nuget

Version:

Semantic Release plugin to create and publish NuGet packages.

81 lines (80 loc) 3.77 kB
import SemanticReleaseError from "@semantic-release/error"; import { execa } from "execa"; import { resolve, sep } from "path"; import { isExecaError, publishFailed } from "./Helper.mjs"; export const publish = async (pluginConfig, context) => { const dotnet = pluginConfig.dotnet || "dotnet"; const registry = pluginConfig.nugetServer ?? "https://api.nuget.org/v3/index.json"; const packagePath = resolve("out"); const baseCliArgs = ["nuget", "push"]; const token = process.env.NUGET_TOKEN; if (pluginConfig.skipPublishToNuget) { context.logger.log("Skipping publish to NuGet server because skipPublishToNuget is set to true."); } else { try { const cliArgs = [...baseCliArgs, "-s", registry, "-k", token]; cliArgs.push(`${packagePath}${sep}*.nupkg`); const argStrings = cliArgs.map((value) => (value === token ? "[redacted]" : value)).join(" "); context.logger.log(`running command "${dotnet} ${argStrings}" ...`); await execa(dotnet, cliArgs, { stdio: "inherit" }); } catch (error) { context.logger.error(`${dotnet} push failed: ${error.message}`); if (isExecaError(error)) { let description = error.command; // hide token from SR output if (error.command && error.command.includes(token)) { description = description.replace(token, "[redacted]"); } throw new SemanticReleaseError(`publish to registry ${registry} failed with exit code ${error.exitCode}`, publishFailed, description); } throw new SemanticReleaseError(`publish to registry ${registry} failed`, publishFailed, error.message); } } if (pluginConfig.publishToGitLab !== true) { return; } try { let projectId = parseInt(process.env.CI_PROJECT_ID, 10); let gitlabToken = process.env.CI_JOB_TOKEN; let gitlabUser = "gitlab-ci-token"; if (pluginConfig.gitlabRegistryProjectId) { projectId = pluginConfig.gitlabRegistryProjectId; gitlabToken = token; gitlabUser = pluginConfig.gitlabUser; } const url = `${process.env.CI_SERVER_URL}/api/v4/projects/${projectId}/packages/nuget/index.json`; // Check if there is already a NuGet source with the GitLab url before adding it const { stdout } = await execa(dotnet, ["nuget", "list", "source", "--format", "short"]); if (stdout?.includes(url) === true) { context.logger.log(`GitLab NuGet source ${url} already exists, skip adding`); } else { context.logger.log(`Adding GitLab as NuGet source ${url}`); await execa(dotnet, [ "nuget", "add", "source", url, "--name", "gitlab", "--username", gitlabUser, "--password", gitlabToken, "--store-password-in-clear-text", ], { stdio: "inherit" }); } const cliArgs = [...baseCliArgs, "--source", "gitlab", `${packagePath}/*.nupkg`]; context.logger.log(`running command "${dotnet} ${cliArgs.join(" ")}" ...`); await execa(dotnet, cliArgs, { stdio: "inherit" }); } catch (error) { context.logger.error(`${dotnet} push failed: ${error.message}`); if (isExecaError(error)) { throw new SemanticReleaseError(`publish to GitLab failed with exit code ${error.exitCode}`, publishFailed, error.command); } throw new SemanticReleaseError("publish to GitLab failed", publishFailed, error.message); } };