Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.

Commit dbb450e

Browse files
Deadpiklebrandedoutcast
authored andcommitted
add support for custom NuGet package name
* Add ability to specify PACKAGE_NAME (#12) * Add package name capabilities
1 parent 23486ff commit dbb450e

File tree

3 files changed

+130
-122
lines changed

3 files changed

+130
-122
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name: publish to nuget
99
on:
1010
push:
1111
branches:
12-
- master # Your default release branch
12+
- master # Default release branch
1313
jobs:
1414
publish:
1515
name: list on nuget
@@ -34,6 +34,7 @@ jobs:
3434
# TAG_COMMIT: true # Flag to enable / disalge git tagging
3535
# TAG_FORMAT: v* # Format of the git tag, [*] gets replaced with version
3636
# NUGET_KEY: ${{secrets.NUGET_API_KEY}} # nuget.org API key
37+
# PACKAGE_NAME: NuGet package name, required when it's different from project name. Defaults to project name
3738
```
3839

3940
- With all settings on default, updates to project version are monitored on every push / PR merge to master & a new tag is created
@@ -50,6 +51,7 @@ VERSION_REGEX | `<Version>(.*)<\/Version>` | Regex pattern to extract version in
5051
TAG_COMMIT | `true` | Flag to enable / disable git tagging
5152
TAG_FORMAT | `v*` | `[*]` is a placeholder for the actual project version
5253
NUGET_KEY | | API key to authorize the package upload to nuget.org
54+
PACKAGE_NAME | | Name of the NuGet package, required when it's different from project name
5355

5456
**Note:**
5557
For multiple projects, every input except `PROJECT_FILE_PATH` can be given as `env` variable at [job / workflow level](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#env)

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ inputs:
1414
required: false
1515
default: <Version>(.*)<\/Version>
1616
TAG_COMMIT:
17-
description: Whether to create a tag when there's a version change or not
17+
description: Whether to create a tag when there's a version change
1818
required: false
1919
default: true
2020
TAG_FORMAT:
@@ -24,6 +24,9 @@ inputs:
2424
NUGET_KEY:
2525
description: API key for the NuGet feed
2626
required: false
27+
PACKAGE_NAME:
28+
description: NuGet package name, required when it's different from the project name
29+
required: false
2730

2831
runs:
2932
using: node12

index.js

Lines changed: 123 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,123 @@
1-
const path = require("path"),
2-
spawnSync = require("child_process").spawnSync,
3-
fs = require("fs"),
4-
https = require("https")
5-
6-
class Action {
7-
constructor() {
8-
this.PROJECT_FILE_PATH = process.env.INPUT_PROJECT_FILE_PATH
9-
this.VERSION_FILE_PATH = process.env.INPUT_VERSION_FILE_PATH || process.env.VERSION_FILE_PATH
10-
this.VERSION_REGEX = new RegExp(process.env.INPUT_VERSION_REGEX || process.env.VERSION_REGEX)
11-
this.TAG_COMMIT = JSON.parse(process.env.INPUT_TAG_COMMIT || process.env.TAG_COMMIT)
12-
this.TAG_FORMAT = process.env.INPUT_TAG_FORMAT || process.env.TAG_FORMAT
13-
this.NUGET_KEY = process.env.INPUT_NUGET_KEY || process.env.NUGET_KEY
14-
}
15-
16-
_warn(msg) {
17-
console.log(`##[warning]${msg}`)
18-
}
19-
20-
_fail(msg) {
21-
console.log(`##[error]${msg}`)
22-
throw new Error(msg)
23-
}
24-
25-
_execCmd(cmd, options) {
26-
const INPUT = cmd.split(" "), TOOL = INPUT[0], ARGS = INPUT.slice(1)
27-
return spawnSync(TOOL, ARGS, options)
28-
}
29-
30-
_execAndCapture(cmd) {
31-
return this._execCmd(cmd, { encoding: "utf-8" }).stdout
32-
}
33-
34-
_execInProc(cmd) {
35-
this._execCmd(cmd, { encoding: "utf-8", stdio: [process.stdin, process.stdout, process.stderr] })
36-
}
37-
38-
_resolveIfExists(filePath, msg) {
39-
const FULLPATH = path.resolve(process.env.GITHUB_WORKSPACE, filePath)
40-
if (!fs.existsSync(FULLPATH)) this._fail(msg)
41-
return FULLPATH
42-
}
43-
44-
_pushPackage() {
45-
if (!this.NUGET_KEY) {
46-
this._warn("😢 nuget_key not given")
47-
return
48-
}
49-
50-
if (!this._execAndCapture("dotnet --version")) {
51-
this._warn("😭 dotnet not found")
52-
return
53-
}
54-
55-
this._execInProc(`dotnet pack -c Release ${this.PROJECT_FILE_PATH} -o .`)
56-
const NUGET_PUSH_RESPONSE = this._execAndCapture(`dotnet nuget push *.nupkg -s https://api.nuget.org/v3/index.json -k ${this.NUGET_KEY}`)
57-
const NUGET_ERROR_REGEX = /(error: Response status code does not indicate success.*)/
58-
59-
if (NUGET_ERROR_REGEX.test(NUGET_PUSH_RESPONSE))
60-
this._fail(`😭 ${NUGET_ERROR_REGEX.exec(NUGET_PUSH_RESPONSE)[1]}`)
61-
}
62-
63-
_tagCommit(version) {
64-
if (this.TAG_COMMIT) {
65-
const TAG = this.TAG_FORMAT.replace("*", version)
66-
67-
if (this._execAndCapture(`git ls-remote --tags origin ${TAG}`).indexOf(TAG) >= 0) {
68-
this._warn(`😢 tag ${TAG} already exists`)
69-
return
70-
}
71-
72-
this._execInProc(`git tag ${TAG}`)
73-
this._execInProc(`git push origin ${TAG}`)
74-
}
75-
}
76-
77-
_pushAndTag(CURRENT_VERSION, PACKAGE_NAME) {
78-
console.log(`👍 found a new version (${CURRENT_VERSION}) of ${PACKAGE_NAME}`)
79-
this._tagCommit(CURRENT_VERSION)
80-
this._pushPackage()
81-
}
82-
83-
run() {
84-
if (!this.PROJECT_FILE_PATH)
85-
this._fail("😭 project file not given")
86-
87-
this.PROJECT_FILE_PATH = this._resolveIfExists(this.PROJECT_FILE_PATH, "😭 project file not found")
88-
this.VERSION_FILE_PATH = !this.VERSION_FILE_PATH ? this.PROJECT_FILE_PATH : this._resolveIfExists(this.VERSION_FILE_PATH, "😭 version file not found")
89-
90-
const FILE_CONTENT = fs.readFileSync(this.VERSION_FILE_PATH, { encoding: "utf-8" }),
91-
VERSION_INFO = this.VERSION_REGEX.exec(FILE_CONTENT)
92-
93-
if (!VERSION_INFO)
94-
this._fail("😢 unable to extract version info")
95-
96-
const CURRENT_VERSION = VERSION_INFO[1],
97-
PACKAGE_NAME = path.basename(this.PROJECT_FILE_PATH).split(".").slice(0, -1).join(".")
98-
99-
https.get(`https://api.nuget.org/v3-flatcontainer/${PACKAGE_NAME}/index.json`, res => {
100-
let body = ""
101-
102-
if (res.statusCode == 404)
103-
this._pushAndTag(CURRENT_VERSION, PACKAGE_NAME)
104-
105-
if (res.statusCode == 200) {
106-
res.setEncoding("utf8")
107-
res.on("data", chunk => body += chunk)
108-
res.on("end", () => {
109-
const existingVersions = JSON.parse(body)
110-
if (existingVersions.versions.indexOf(CURRENT_VERSION) < 0)
111-
this._pushAndTag(CURRENT_VERSION, PACKAGE_NAME)
112-
})
113-
}
114-
}).on("error", e => {
115-
this._warn(`😢 error reaching nuget.org ${e.message}`)
116-
})
117-
}
118-
}
119-
120-
new Action().run()
1+
const path = require("path"),
2+
spawnSync = require("child_process").spawnSync,
3+
fs = require("fs"),
4+
https = require("https")
5+
6+
class Action {
7+
constructor() {
8+
this.PROJECT_FILE_PATH = process.env.INPUT_PROJECT_FILE_PATH
9+
this.VERSION_FILE_PATH = process.env.INPUT_VERSION_FILE_PATH || process.env.VERSION_FILE_PATH
10+
this.VERSION_REGEX = new RegExp(process.env.INPUT_VERSION_REGEX || process.env.VERSION_REGEX)
11+
this.TAG_COMMIT = JSON.parse(process.env.INPUT_TAG_COMMIT || process.env.TAG_COMMIT)
12+
this.TAG_FORMAT = process.env.INPUT_TAG_FORMAT || process.env.TAG_FORMAT
13+
this.NUGET_KEY = process.env.INPUT_NUGET_KEY || process.env.NUGET_KEY
14+
this.PACKAGE_NAME = process.env.INPUT_PACKAGE_NAME || process.env.PACKAGE_NAME
15+
}
16+
17+
_warn(msg) {
18+
console.log(`##[warning]${msg}`)
19+
}
20+
21+
_fail(msg) {
22+
console.log(`##[error]${msg}`)
23+
throw new Error(msg)
24+
}
25+
26+
_execCmd(cmd, options) {
27+
const INPUT = cmd.split(" "), TOOL = INPUT[0], ARGS = INPUT.slice(1)
28+
return spawnSync(TOOL, ARGS, options)
29+
}
30+
31+
_execAndCapture(cmd) {
32+
return this._execCmd(cmd, { encoding: "utf-8" }).stdout
33+
}
34+
35+
_execInProc(cmd) {
36+
this._execCmd(cmd, { encoding: "utf-8", stdio: [process.stdin, process.stdout, process.stderr] })
37+
}
38+
39+
_resolveIfExists(filePath, msg) {
40+
const FULLPATH = path.resolve(process.env.GITHUB_WORKSPACE, filePath)
41+
if (!fs.existsSync(FULLPATH)) this._fail(msg)
42+
return FULLPATH
43+
}
44+
45+
_pushPackage() {
46+
if (!this.NUGET_KEY) {
47+
this._warn("😢 nuget_key not given")
48+
return
49+
}
50+
51+
if (!this._execAndCapture("dotnet --version")) {
52+
this._warn("😭 dotnet not found")
53+
return
54+
}
55+
56+
this._execInProc(`dotnet pack -c Release ${this.PROJECT_FILE_PATH} -o .`)
57+
const NUGET_PUSH_RESPONSE = this._execAndCapture(`dotnet nuget push *.nupkg -s https://api.nuget.org/v3/index.json -k ${this.NUGET_KEY}`)
58+
const NUGET_ERROR_REGEX = /(error: Response status code does not indicate success.*)/
59+
60+
if (NUGET_ERROR_REGEX.test(NUGET_PUSH_RESPONSE))
61+
this._fail(`😭 ${NUGET_ERROR_REGEX.exec(NUGET_PUSH_RESPONSE)[1]}`)
62+
}
63+
64+
_tagCommit(version) {
65+
if (this.TAG_COMMIT) {
66+
const TAG = this.TAG_FORMAT.replace("*", version)
67+
68+
if (this._execAndCapture(`git ls-remote --tags origin ${TAG}`).indexOf(TAG) >= 0) {
69+
this._warn(`😢 tag ${TAG} already exists`)
70+
return
71+
}
72+
73+
this._execInProc(`git tag ${TAG}`)
74+
this._execInProc(`git push origin ${TAG}`)
75+
}
76+
}
77+
78+
_pushAndTag(version, name) {
79+
console.log(`👍 found a new version (${version}) of ${name}`)
80+
this._tagCommit(version)
81+
this._pushPackage()
82+
}
83+
84+
run() {
85+
if (!this.PROJECT_FILE_PATH)
86+
this._fail("😭 project file not given")
87+
88+
this.PROJECT_FILE_PATH = this._resolveIfExists(this.PROJECT_FILE_PATH, "😭 project file not found")
89+
this.VERSION_FILE_PATH = !this.VERSION_FILE_PATH ? this.PROJECT_FILE_PATH : this._resolveIfExists(this.VERSION_FILE_PATH, "😭 version file not found")
90+
91+
const FILE_CONTENT = fs.readFileSync(this.VERSION_FILE_PATH, { encoding: "utf-8" }),
92+
VERSION_INFO = this.VERSION_REGEX.exec(FILE_CONTENT)
93+
94+
if (!VERSION_INFO)
95+
this._fail("😢 unable to extract version info")
96+
97+
const CURRENT_VERSION = VERSION_INFO[1]
98+
99+
if (!this.PACKAGE_NAME)
100+
this.PACKAGE_NAME = path.basename(this.PROJECT_FILE_PATH).split(".").slice(0, -1).join(".")
101+
102+
https.get(`https://api.nuget.org/v3-flatcontainer/${this.PACKAGE_NAME}/index.json`, res => {
103+
let body = ""
104+
105+
if (res.statusCode == 404)
106+
this._pushAndTag(CURRENT_VERSION, this.PACKAGE_NAME)
107+
108+
if (res.statusCode == 200) {
109+
res.setEncoding("utf8")
110+
res.on("data", chunk => body += chunk)
111+
res.on("end", () => {
112+
const existingVersions = JSON.parse(body)
113+
if (existingVersions.versions.indexOf(CURRENT_VERSION) < 0)
114+
this._pushAndTag(CURRENT_VERSION, this.PACKAGE_NAME)
115+
})
116+
}
117+
}).on("error", e => {
118+
this._warn(`😢 error reaching nuget.org ${e.message}`)
119+
})
120+
}
121+
}
122+
123+
new Action().run()

0 commit comments

Comments
 (0)