-
Notifications
You must be signed in to change notification settings - Fork 82
/
Deploy.PSDeploy.ps1
77 lines (70 loc) · 3.02 KB
/
Deploy.PSDeploy.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Write-Host "Executing Deploy.PS1"
if (
$env:BuildSystem -eq 'GitHub Actions'
) {
if ($env:BranchName -eq 'master' -and
$env:NuGetApiKey -and
$env:GitHubKey -and
$env:CommitMessage -match '!Deploy'
) {
$manifest = Import-PowerShellDataFile -Path "./$env:ProjectName/$env:ProjectName.psd1"
$manifest.RequiredModules | ForEach-Object {
if ([string]::IsNullOrEmpty($_)) {
return
}
$ReqModuleName = ([Microsoft.PowerShell.Commands.ModuleSpecification]$_).Name
$InstallModuleParams = @{Name = $ReqModuleName}
if ($ReqModuleVersion = ([Microsoft.PowerShell.Commands.ModuleSpecification]$_).RequiredVersion) {
$InstallModuleParams.Add('RequiredVersion', $ReqModuleVersion)
}
Install-Module @InstallModuleParams -Force
}
Deploy Module {
By PSGalleryModule {
FromSource $(Get-Item "./BuildOutput/$Env:ProjectName")
To PSGallery
WithOptions @{
ApiKey = $Env:NuGetApiKey
}
}
}
Write-Host "Creating GitHub release" -ForegroundColor Green
$updatedManifest = Import-PowerShellDataFile ./BuildOutput/$env:ProjectName/$env:ProjectName.psd1
$releaseData = @{
tag_name = '{0}' -f $updatedManifest.ModuleVersion
target_commitish = $env:GITHUB_SHA
name = '{0}' -f $updatedManifest.ModuleVersion
body = $updatedManifest.PrivateData.PSData.ReleaseNotes
draft = $false
prerelease = $false
}
$releaseParams = @{
Uri = "https://api.github.com/repos/$env:GITHUB_REPOSITORY/releases?access_token=$env:GitHubKey"
Method = 'POST'
ContentType = 'application/json'
Body = (ConvertTo-Json $releaseData -Compress)
UseBasicParsing = $true
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$newRelease = Invoke-RestMethod @releaseParams
Compress-Archive -DestinationPath "./BuildOutput/$($env:ProjectName)_$($updatedManifest.ModuleVersion).zip" -Path ./BuildOutput/$env:ProjectName/*.*
$uploadParams = @{
Uri = ($newRelease.upload_url -replace '\{\?name.*\}', '?name=AzSentinel_') +
$updatedManifest.ModuleVersion +
'.zip&access_token=' +
$env:GitHubKey
Method = 'POST'
ContentType = 'application/zip'
InFile = "./BuildOutput/$($env:ProjectName)_$($updatedManifest.ModuleVersion).zip"
}
$null = Invoke-RestMethod @uploadParams
} else {
write-host "Did not comply with release conditions"
Write-Host "BranchName: $env:BranchName"
Write-Host "NuGetApiKey: $env:NuGetApiKey"
Write-Host "GitHubKey: $env:GitHubKey"
Write-Host "CommitMessage: $env:CommitMessage"
}
} else {
Write-Host "Not In Github Actions. Skipped"
}