Skip to content

Commit ec54081

Browse files
+semver:feature Allowing the addition of an auth token to download dependencies
1 parent 53ac357 commit ec54081

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,30 @@ A GitHub Action that takes in a list of dependency scripts for a database, downl
1515

1616
## Inputs
1717

18-
| Parameter | Is Required | Default | Description |
19-
| ------------------------- | ----------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
20-
| `db-server-name` | true | N/A | The server where the dependency files will be run. |
21-
| `db-name` | true | N/A | The name of the database where the dependency files will run. |
22-
| `dependency-list` | true | N/A | A json string containing a list of objects with the name of the dependency package, the version, and the url where the package is stored. |
23-
| `use-integrated-security` | true | false | Use domain integrated security. If false, a db-username and db-password should be specified. If true, those parameters will be ignored if specified. |
24-
| `db-username` | false | N/A | The username to use to login to the database. This is required if use-integrated-security is false, otherwise it's optional and will be ignored. |
25-
| `db-password` | false | N/A | The password for the user logging in to the database. This is required if use-integrated-security is false, otherwise it's optional and will be ignored. |
18+
| Parameter | Is Required | Default | Description |
19+
| ------------------------- | ----------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
20+
| `db-server-name` | true | N/A | The server where the dependency files will be run. |
21+
| `db-name` | true | N/A | The name of the database where the dependency files will run. |
22+
| `dependency-list` | true | N/A | A json string containing a list of objects with the name of the dependency package, the version,the url where the package is stored, and optionally the auth token needed to download the package. |
23+
| `use-integrated-security` | true | false | Use domain integrated security. If false, a db-username and db-password should be specified. If true, those parameters will be ignored if specified. |
24+
| `db-username` | false | N/A | The username to use to login to the database. This is required if use-integrated-security is false, otherwise it's optional and will be ignored. |
25+
| `db-password` | false | N/A | The password for the user logging in to the database. This is required if use-integrated-security is false, otherwise it's optional and will be ignored. |
2626

2727
The `dependency-list` should be an array of objects with the following properties:
2828

2929
```json
3030
{
3131
"version": "1.0.0",
3232
"packageName": "some_package",
33-
"nugetUrl": "https://www.some-nuget-repo.com"
33+
"nugetUrl": "https://www.some-nuget-repo.com",
34+
"authToken": "ghp_fdijlfdsakeizdkliejfezejw"
3435
}
3536
```
3637

38+
**Notes**
39+
* The `authToken` property is optionally used for nuget sources that require a bearer token, such as GitHub Packages. It should not be included if it is unnecessary.
40+
* The `nugetUrl` for GitHub Packages can be pretty tricky to lookup, so for reference the pattern is as follows: `https://nuget.pkg.github.com/<owner>/download/<package-name>/<version>/<file-name>.nupkg`. Here's an example of how that could look if this repo were publishing a package called `MyDbObject`: `https://nuget.pkg.github.com/im-open/download/MyDbObject/1.0.0/MyDbObject.1.0.0.nupkg`.
41+
3742
## Example
3843

3944
```yml
@@ -54,7 +59,7 @@ jobs:
5459
with:
5560
db-server-name: 'localhost,1433'
5661
db-name: 'LocalDb'
57-
dependency-list: '[{"version":"1.0.0","packageName":"dbo.Something","nugetUrl":"https://nuget.pkg.github.com/my-org/my-repo/dbo.Something.nupkg"},{"version":"1.2.0","packageName":"dbo.SomeOtherThing","nugetUrl":"https://nuget.pkg.github.com/my-org/my-repo/dbo.SomeOtherThing.nupkg"}]'
62+
dependency-list: '[{"version":"1.0.0","packageName":"dbo.Something","nugetUrl":"https://nuget.pkg.github.com/my-org/download/Something/1.0.0/dbo.Something.1.0.0.nupkg","authToken":"ghp_dkfsjakldafl"},{"version":"1.2.0","packageName":"dbo.SomeOtherThing","nugetUrl":"https://nuget.pkg.github.com/my-org/download/SomeOtherThing/1.2.0/dbo.SomeOtherThing1.2.0.nupkg","authToken":"ghp_dkfsjakldafl"}]'
5863
```
5964
6065
## Contributing

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ inputs:
1010
description: The name of the database where the dependency files will run.
1111
required: true
1212
dependency-list:
13-
description: A json string containing a list of objects with the name of the dependency package, the version, and the url where the package is stored.
13+
description: A json string containing a list of objects with the name of the dependency package, the version,the url where the package is stored, and optionally the auth token needed to download the package.
1414
required: true
1515
use-integrated-security:
1616
description: Use domain integrated security. If false, a db-username and db-password should be specified. If true, those parameters will be ignored if specified.

src/download-db-dependencies.ps1

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,59 @@ param(
22
[PSCustomObject[]]$dependencies
33
)
44

5-
if ($null -eq $dependencies -or !$dependencies.PSobject.Properties.name.Contains("Count" ) -or $dependencies.Count -eq 0)
6-
{
5+
if ($null -eq $dependencies -or !$dependencies.PSobject.Properties.name.Contains("Count") -or $dependencies.Count -eq 0) {
76
return
87
}
98

109
Write-Host "Downloading database objects"
1110

12-
$nugetFolder = "$PSScriptRoot\.nuget"
13-
$dependencyOutputFolder = "$PSScriptRoot\.dependencies"
14-
$targetNugetExe = "$nugetFolder\nuget.exe"
15-
$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
11+
$dependencyOutputFolder = "$PSScriptRoot/.dependencies"
1612

17-
if (![System.IO.File]::Exists($targetNugetExe))
18-
{
19-
Write-Host "Downloading nuget.exe"
20-
New-Item -ItemType Directory -Path $nugetFolder
21-
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
22-
}
23-
24-
if (-Not (Test-Path $dependencyOutputFolder))
25-
{
13+
if (-Not (Test-Path $dependencyOutputFolder)) {
2614
New-Item -ItemType Directory -Path $dependencyOutputFolder
2715
}
2816

29-
foreach ($dependency in $dependencies)
30-
{
17+
foreach ($dependency in $dependencies) {
3118
#Download Package
3219
$packageName = $dependency.packageName
3320
$version = $dependency.version
3421
$url = $dependency.nugetUrl
35-
$nugetOutput = "$dependencyOutputFolder\$packageName.nupkg"
22+
$nugetOutput = "$dependencyOutputFolder/$packageName.nupkg"
23+
24+
$headers = If ($dependency.authToken) { @{ "Authorization" = "Bearer $($dependency.authToken)" } } Else { @{} };
3625
Write-Host "Downloading $packageName.$version"
3726
Remove-Item $nugetOutput -Force -Recurse -ErrorAction Ignore
3827

39-
try
40-
{
41-
Invoke-WebRequest $url -OutFile $nugetOutput
28+
try {
29+
Invoke-WebRequest $url -OutFile $nugetOutput -Headers $headers
4230
}
43-
catch
44-
{
31+
catch {
4532
Write-Error $_;
4633
}
4734

4835
#Extract Package
49-
$extractionLocation = "$dependencyOutputFolder\$packageName"
36+
$extractionLocation = "$dependencyOutputFolder/$packageName"
5037
Remove-Item $extractionLocation -Force -Recurse -ErrorAction Ignore
38+
New-Item -ItemType Directory -Path $extractionLocation
5139
Add-Type -AssemblyName System.IO.Compression.FileSystem
52-
[System.IO.Compression.ZipFile]::ExtractToDirectory($nugetOutput, $extractionLocation)
40+
41+
$zip = [System.IO.Compression.ZipFile]::OpenRead($nugetOutput)
42+
43+
foreach ($item in $zip.Entries) {
44+
$itemDirectory = Join-Path -Path $extractionLocation -ChildPath (Split-Path -parent $item.FullName)
45+
46+
try {
47+
if ($itemDirectory -and -not (Test-Path $itemDirectory)) {
48+
New-Item -ItemType Directory -Path $itemDirectory
49+
}
50+
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($item, (Join-Path -Path $extractionLocation -ChildPath $item.FullName), $false)
51+
}
52+
catch {
53+
# Write out any errors that happen but continue on
54+
Write-Host "An error occurred. Writing out the information and moving on."
55+
Write-Host $_
56+
}
57+
}
5358
}
5459

5560
Write-Host "Finished downloading database objects"

0 commit comments

Comments
 (0)