diff --git a/src/publish.mts b/src/publish.mts index e4717333..40567bd3 100644 --- a/src/publish.mts +++ b/src/publish.mts @@ -62,24 +62,31 @@ export const publish = async (pluginConfig: Config & UserConfig, context: Publis } const url = `${process.env.CI_SERVER_URL!}/api/v4/projects/${projectId}/packages/nuget/index.json`; - 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" }, - ); + + // 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`]; diff --git a/test/publish.test.mts b/test/publish.test.mts index a1c12efa..f54c7599 100644 --- a/test/publish.test.mts +++ b/test/publish.test.mts @@ -1,5 +1,5 @@ import { afterEach, beforeAll, describe, expect, it, jest } from "@jest/globals"; -import type { ExecaChildProcess, ExecaError, execa } from "execa"; +import type { ExecaChildProcess, ExecaError, ExecaReturnBase, execa } from "execa"; import { PublishContext } from "semantic-release"; import { publishFailed } from "../src/Helper.mjs"; import type { publish as publishType } from "../src/publish.mjs"; @@ -234,6 +234,13 @@ describe("publish", () => { }); it("should use gitlabRegistryProjectId over CI_PROJECT_ID if set", async () => { + execaMock.mockImplementationOnce(() => { + return { + stdout: + "dotnet nuget push -s https://gitlab.com/api/v4/projects/12345/packages/nuget/index.json -k 104E4 out/*.nupkg", + exitCode: 0, + } as Partial> as never; + }); execaMock.mockImplementationOnce(() => { return { command: @@ -255,8 +262,8 @@ describe("publish", () => { context, ); - expect(execaMock).toHaveBeenCalledTimes(2); - expect(execaMock.mock.calls[0]).toEqual([ + expect(execaMock).toHaveBeenCalledTimes(3); + expect(execaMock.mock.calls[1]).toEqual([ "dotnet", [ "nuget",