Skip to content

Commit 7b6980a

Browse files
Merge pull request #8 from im-open/add-auth-token
Add auth token
2 parents 53ac357 + 3a5b0c8 commit 7b6980a

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,31 @@ 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. |
26+
| `trust-server-certificate` | false | false | A boolean that controls whether or not to validate the SQL Server TLS certificate. |
2627

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

2930
```json
3031
{
3132
"version": "1.0.0",
3233
"packageName": "some_package",
33-
"nugetUrl": "https://www.some-nuget-repo.com"
34+
"nugetUrl": "https://www.some-nuget-repo.com",
35+
"authToken": "ghp_fdijlfdsakeizdkliejfezejw"
3436
}
3537
```
3638

39+
**Notes**
40+
* 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.
41+
* 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`.
42+
3743
## Example
3844

3945
```yml
@@ -50,11 +56,12 @@ jobs:
5056

5157
- name: Download and Run Dependencies
5258
# You may also reference the major or major.minor version
53-
uses: im-open/install-and-run-db-dependency-scripts@v1.1.2
59+
uses: im-open/install-and-run-db-dependency-scripts@v1.2.0
5460
with:
5561
db-server-name: 'localhost,1433'
5662
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"}]'
63+
trust-server-certificate: 'true'
64+
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"}]'
5865
```
5966
6067
## Contributing

action.yml

Lines changed: 7 additions & 2 deletions
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.
@@ -22,6 +22,10 @@ inputs:
2222
db-password:
2323
description: 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.
2424
required: false
25+
trust-server-certificate:
26+
description: A boolean that controls whether or not to validate the SQL Server TLS certificate.
27+
required: false
28+
default: 'false'
2529

2630
runs:
2731
using: 'composite'
@@ -42,4 +46,5 @@ runs:
4246
-dbName "${{ inputs.db-name }}" `
4347
-useIntegratedSecurity:$${{ inputs.use-integrated-security }} `
4448
-username "${{ inputs.db-username }}" `
45-
-password $securePassword
49+
-password $securePassword `
50+
-trustServerCertificate:$${{ inputs.trust-server-certificate }}

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"

src/run-db-dependencies.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ param (
33
[string]$dbName,
44
[switch]$useIntegratedSecurity = $false,
55
[string]$username,
6-
[securestring]$password
6+
[securestring]$password,
7+
[switch]$trustServerCertificate
78
)
89

910
Write-Host "Running database dependency scripts"
@@ -21,6 +22,10 @@ $sqlCmdParams = @(
2122
"-Verbose"
2223
)
2324

25+
if ($trustServerCertificate) {
26+
$sqlCmdParams += "-TrustServerCertificate"
27+
}
28+
2429
$temp2 = [string]::Join(" ", $sqlCmdParams)
2530
$temp = @("Invoke-Sqlcmd $temp2")
2631

0 commit comments

Comments
 (0)