diff --git a/README.md b/README.md index 0309204..81a2e68 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,9 @@ jobs: # Flag to toggle pushing symbols along with nuget package to the server, disabled by default # INCLUDE_SYMBOLS: false + + # Flag to throw an error when trying to publish an existing version of a package + # THOW_ERROR_IF_VERSION_EXISTS: false ``` - Project gets published only if there's a `NUGET_KEY` configured in the repository @@ -82,6 +85,7 @@ TAG_FORMAT | `v*` | Format of the git tag, `[*]` gets replaced with actual versi NUGET_KEY | | API key to authenticate with NuGet server NUGET_SOURCE | `https://api.nuget.org` | NuGet server uri hosting the packages, defaults to https://api.nuget.org INCLUDE_SYMBOLS | `false` | Flag to toggle pushing symbols along with nuget package to the server, disabled by default +THOW_ERROR_IF_VERSION_EXISTS | `false` | Flag to throw an error when trying to publish an existing version of a package ## Outputs diff --git a/action.yml b/action.yml index 4048fe8..1454524 100644 --- a/action.yml +++ b/action.yml @@ -44,6 +44,10 @@ inputs: description: Flag to toggle pushing symbols along with nuget package to the server, disabled by default required: false default: false + THOW_ERROR_IF_VERSION_EXISTS: + description: Flag to throw an error when trying to publish an existing version of a package + required: false + default: false outputs: VERSION: diff --git a/index.js b/index.js index d6d54c6..22fdef3 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,7 @@ class Action { this.nugetKey = process.env.INPUT_NUGET_KEY || process.env.NUGET_KEY this.nugetSource = process.env.INPUT_NUGET_SOURCE || process.env.NUGET_SOURCE this.includeSymbols = JSON.parse(process.env.INPUT_INCLUDE_SYMBOLS || process.env.INCLUDE_SYMBOLS) + this.throwOnVersionExixts = JSON.parse(process.env.INPUT_THOW_ERROR_IF_VERSION_EXISTS || process.env.THOW_ERROR_IF_VERSION_EXISTS) } _printErrorAndExit(msg) { @@ -81,16 +82,29 @@ class Action { https.get(`${this.nugetSource}/v3-flatcontainer/${this.packageName}/index.json`, res => { let body = "" - if (res.statusCode == 404) + if (res.statusCode == 404){ + console.log(`No packages found. Pushing initial version...`) this._pushPackage(this.version, this.packageName) + } if (res.statusCode == 200) { res.setEncoding("utf8") res.on("data", chunk => body += chunk) res.on("end", () => { const existingVersions = JSON.parse(body) - if (existingVersions.versions.indexOf(this.version) < 0) + if (existingVersions.versions.indexOf(this.version) < 0) { + console.log(`This version is new, pushing...`) this._pushPackage(this.version, this.packageName) + } + else + { + let errorMsg = `Version ${this.version} already exists`; + console.log(errorMsg) + + if(this.throwOnVersionExixts) { + this._printErrorAndExit(`error: ${errorMsg}`) + } + } }) } }).on("error", e => {