Skip to content

Commit

Permalink
handling source type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebel028 committed Dec 3, 2020
1 parent 6c1fcca commit 605df46
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
name: build, pack & publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.5.9

# - name: Setup dotnet
# uses: actions/setup-dotnet@v1
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
# Format of the git tag, [*] gets replaced with actual version
# TAG_FORMAT: v*

# API key to authenticate with NuGet server
# API key to authenticate with NuGet server, or a token, issued for GITHUB_USER if you use GPR
# NUGET_KEY: ${{secrets.NUGET_API_KEY}}

# NuGet server uri hosting the packages, defaults to https://api.nuget.org
Expand All @@ -71,7 +71,8 @@ VERSION_REGEX | `^\s*<Version>(.*)<\/Version>\s*$` | Regex pattern to extract ve
VERSION_STATIC| | Useful with external providers like Nerdbank.GitVersioning, ignores VERSION_FILE_PATH & VERSION_REGEX
TAG_COMMIT | `true` | Flag to toggle git tagging, enabled by default
TAG_FORMAT | `v*` | Format of the git tag, `[*]` gets replaced with actual version
NUGET_KEY | | API key to authenticate with NuGet server
GITHUB_USER |`[GITHUB_ACTOR]` | Required for packages pushed to Github Package Registry. User allowed to push to repository, defaults to GITHUB_ACTOR (user that triggered the action)
NUGET_KEY | | API key to authenticate with NuGet server, or a token, issued for GITHUB_USER if you use GPR
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

Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ inputs:
description: Format of the git tag, [*] gets replaced with actual version
required: false
default: v*
GITHUB_USER:
description: Required for packages pushed to Github Package Registry. User allowed to push to repository, defaults to GITHUB_ACTOR (user that triggered the action)
required: false
NUGET_KEY:
description: API key to authenticate with NuGet server
description: API key to authenticate with NuGet server, or a token, issued for GITHUB_USER if you use GPR
required: false
NUGET_SOURCE:
description: NuGet server uri hosting the packages, defaults to https://api.nuget.org
Expand Down
33 changes: 29 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const os = require("os"),
spawnSync = require("child_process").spawnSync

class Action {

SOURCE_NAME = "default";


constructor() {
this.projectFile = process.env.INPUT_PROJECT_FILE_PATH
this.packageName = process.env.INPUT_PACKAGE_NAME || process.env.PACKAGE_NAME
Expand All @@ -13,9 +17,22 @@ class Action {
this.version = process.env.INPUT_VERSION_STATIC || process.env.VERSION_STATIC
this.tagCommit = JSON.parse(process.env.INPUT_TAG_COMMIT || process.env.TAG_COMMIT)
this.tagFormat = process.env.INPUT_TAG_FORMAT || process.env.TAG_FORMAT
this.githubUser = process.env.INPUT_GITHUB_USER || process.env.GITHUB_ACTOR
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)



if (this.nugetSource.startsWith(`https://nuget.pkg.github.com/`)) {
this.sourceType = "GPR"
const addSourceCmd = `dotnet nuget add source ${this.nugetSource} --name ${(this.SOURCE_NAME)} --username ${this.githubUser} --password ${this.nugetKey}`
this._executeCommand(addSourceCmd, { encoding: "utf-8" })
} else {
this.sourceType = "NuGet"
const addSourceCmd = `dotnet nuget add source ${this.nugetSource} --name ${(this.SOURCE_NAME)}`
this._executeCommand(addSourceCmd, { encoding: "utf-8" })
}
}

_printErrorAndExit(msg) {
Expand Down Expand Up @@ -64,8 +81,9 @@ class Action {
const packages = fs.readdirSync(".").filter(fn => fn.endsWith("nupkg"))
console.log(`Generated Package(s): ${packages.join(", ")}`)

const pushCmd = `dotnet nuget push *.nupkg -s ${this.nugetSource}/v3/index.json -k ${this.nugetKey} --skip-duplicate ${!this.includeSymbols ? "-n 1" : ""}`,
pushOutput = this._executeCommand(pushCmd, { encoding: "utf-8" }).stdout
const pushCmd = `dotnet nuget push *.nupkg -s ${(this.SOURCE_NAME)} ${this.nugetSource !== "GPR"? `-k ${this.nugetKey}`: ""} --skip-duplicate ${!this.includeSymbols ? "-n 1" : ""}`

const pushOutput = this._executeCommand(pushCmd, { encoding: "utf-8" }).stdout

console.log(pushOutput)

Expand Down Expand Up @@ -94,15 +112,22 @@ class Action {

console.log(`Package Name: ${this.packageName}`)
let requestUrl = ""
let options;

//small hack to get package versions from Github Package Registry
if (this.nugetSource.startsWith(`https://nuget.pkg.github.com/`)) {
if (this.sourceType === "GPR") {
requestUrl = `${this.nugetSource}/download/${this.packageName}/index.json`
options = {
auth:{
user: this.githubUser,
pass: this.nugetKey
}
}
} else {
requestUrl = `${this.nugetSource}/v3-flatcontainer/${this.packageName}/index.json`
}

https.get(requestUrl, res => {
https.get(requestUrl, options, res => {
let body = ""

if (res.statusCode == 404)
Expand Down

0 comments on commit 605df46

Please sign in to comment.