Upload files to "Powershell Modules & NuGet Repo"
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
<#
|
||||
Author: Gabe Kerntke
|
||||
Date: 01/08/26
|
||||
|
||||
.Synopsis
|
||||
Script for the CI/CD Pipeline to create and publish PowerShell modules.
|
||||
|
||||
.Modified
|
||||
- GabeK (1/8/26): Script created
|
||||
#>
|
||||
|
||||
#Region Get-Numbered_Jobs
|
||||
function Get-Numbered_Jobs {
|
||||
param(
|
||||
[string]$ProjectId = "COMPANY/techci/openeng/powershell/vmware-tools",
|
||||
[string]$GitLabBase = "https://URLHERE"
|
||||
)
|
||||
# GitLab API requires URL-encoded project identifier; encode '/' as %2F
|
||||
#function Format-ProjectId ([string]$path) {
|
||||
# return [uri]::EscapeDataString($path).Replace("/", "%2F")
|
||||
#}
|
||||
|
||||
#$encodedId = Format-ProjectId $ProjectPath
|
||||
|
||||
$Headers = @{ "JOB-TOKEN" = $env:CI_JOB_TOKEN }
|
||||
#$Uri = "$GitLabBase/api/v4/projects/$encodedId/jobs?per_page=100"
|
||||
$Uri = "$GitLabBase/api/v4/projects/$([uri]::EscapeDataString($ProjectId))/jobs?per_page=100"
|
||||
|
||||
#$resp = Invoke-WebRequest -UseBasicParsing $Uri -Headers $Headers -Method Get -ErrorAction Stop
|
||||
#$jobs = $resp.Content | ConvertFrom-Json
|
||||
|
||||
$jobs = Invoke-RestMethod -UseBasicParsing $Uri -Headers $Headers -Method Get
|
||||
$Array = $jobs | Where-Object { $_.Stage -eq "Deploy" } | Select-Object -ExpandProperty id | Sort-Object -Descending
|
||||
$Array.Count
|
||||
}
|
||||
#EndRegion
|
||||
#Region Start-Loop_Updating_Version
|
||||
function Start-Loop_Updating_Version {
|
||||
$Version = "1.0.0"
|
||||
for ($i = 1; $i -le $Number_of_Jobs; $i++) {
|
||||
$New_Version = Update-Version -Version $Version
|
||||
$Version = $New_Version
|
||||
}
|
||||
}
|
||||
#EndRegion
|
||||
#Region Update-Version
|
||||
Function Update-Version {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version,
|
||||
# Which segment to increment: -1 = last (default), 0 = first, etc.
|
||||
[int]$SegmentIndex = -1,
|
||||
# Max value for non-major segments before rollover
|
||||
[int]$SegmentMax = 9
|
||||
)
|
||||
$Parts = $Version.Split('.')
|
||||
|
||||
# negative index normalization trick
|
||||
If ($SegmentIndex -lt 0) { $SegmentIndex = $Parts.Count + $SegmentIndex }
|
||||
|
||||
# Convert to integer
|
||||
$Numbers = foreach ($Part in $Parts) { [int]$Part }
|
||||
|
||||
# Increment with carry; major (index 0) has no cap
|
||||
$i = $SegmentIndex
|
||||
while ($i -ge 0) {
|
||||
$Numbers[$i]++
|
||||
|
||||
# Major segment: unlimited growth; stop carrying
|
||||
if ($i -eq 0) { break }
|
||||
|
||||
if ($Numbers[$i] -le $SegmentMax) { break }
|
||||
|
||||
# rollover this non-major segment and carry left
|
||||
$Numbers[$i] = 0
|
||||
$i--
|
||||
}
|
||||
($Numbers -join '.')
|
||||
}
|
||||
#EndRegion
|
||||
#Region New-Module_Manifest
|
||||
function New-Module_Manifest {
|
||||
$RootModule = Get-ChildItem -File -Filter *.psm1 -Recurse -ErrorAction Stop | Select-Object -First 1
|
||||
$Path = [System.IO.Path]::ChangeExtension($RootModule.FullName, '.psd1')
|
||||
|
||||
$manifest = @{
|
||||
Author = $GITLAB_USER_NAME
|
||||
Description = $CI_PROJECT_DESCRIPTION
|
||||
FunctionsToExport = @( $Function_Names )
|
||||
Path = $Path
|
||||
RootModule = $RootModule.Name
|
||||
CompanyName = 'COMPANY'
|
||||
LicenseUri = 'https://opensource.org/licenses/MIT'
|
||||
ModuleVersion = $Module_Version
|
||||
CmdletsToExport = ''
|
||||
AliasesToExport = ''
|
||||
}
|
||||
New-ModuleManifest @manifest
|
||||
}
|
||||
#EndRegion
|
||||
#Region New-Module
|
||||
Function New-Create_Module {
|
||||
# Variables
|
||||
$Current_Path = Get-Location | Select-Object -ExpandProperty Path
|
||||
$Module_Name = $Current_Path | Split-Path -Leaf
|
||||
$Out_File = $Current_Path + "\" + "COMPANY." + $Module_Name + ".psm1"
|
||||
|
||||
# Get all .ps1 files
|
||||
$Files = Get-ChildItem -Recurse -Depth 2 -Filter "*.ps1"
|
||||
|
||||
$Merged_Content = foreach ($File in $Files) {
|
||||
# Use the PowerShell Parser to extract code from scripts
|
||||
$ast = [System.Management.Automation.Language.Parser]::ParseFile($File.FullName, [ref]$null, [ref]$null)
|
||||
$ast.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true) |
|
||||
ForEach-Object { $_.Extent.Text }
|
||||
"`n" # Add a newline between functions
|
||||
}
|
||||
#Testing alternative method
|
||||
#foreach ($File in $Files) {
|
||||
# Get-Content $File.FullName | Add-Content -Path $Out_File
|
||||
#}
|
||||
$Merged_Content | Set-Content -Path $Out_File
|
||||
Write-Output "Module created at: $Out_File"
|
||||
}
|
||||
#EndRegion
|
||||
|
||||
#Region Start Script
|
||||
$Test = Get-ChildItem -Path $SourceDirectory -Include *.psm1 -Recurse
|
||||
If ($null -eq $Test) {
|
||||
New-Create_Module
|
||||
$Function_Names = Get-ChildItem -Recurse -Depth 2 | Where-Object { $_.Name -like "*.ps1" } | ForEach-Object {
|
||||
# Extract function names from each .ps1 file
|
||||
(Get-Command $_.FullName).ScriptBlock.Ast.FindAll(
|
||||
{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] },
|
||||
$false
|
||||
).Name
|
||||
} | Select-Object -Unique
|
||||
}
|
||||
Elseif (Test-Path $Test) {
|
||||
$Function_Names = Get-ChildItem -Recurse -Depth 2 -Filter *.psm1 | ForEach-Object {
|
||||
# Extract function names from each .psm1 file
|
||||
$tokens = $null; $errors = $null
|
||||
$ast = [System.Management.Automation.Language.Parser]::ParseFile($_.FullName, [ref]$tokens, [ref]$errors)
|
||||
$ast.FindAll(
|
||||
{ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] },
|
||||
$true
|
||||
).Name
|
||||
} | Select-Object -Unique
|
||||
}
|
||||
else {
|
||||
write-Output "unsure how we got here"
|
||||
exit 1
|
||||
}
|
||||
|
||||
$Number_of_Jobs = Get-Numbered_Jobs
|
||||
$Module_Version = Start-Loop_Updating_Version
|
||||
New-Module_Manifest
|
||||
Get-ChildItem -Path $SourceDirectory -Include *.psm1 -Recurse | ForEach-Object {
|
||||
$Module = (Split-Path $_.FullName)
|
||||
Write-Output "Publishing module $Module"
|
||||
Publish-Module -Path "$Module" -Repository "${NUGET_REPOSITORY_ID}" -ErrorAction Stop -Force -Verbose -NuGetApiKey "${username}:${ARTIFACTORY_TOKEN}"
|
||||
}
|
||||
#Region End Script
|
||||
#EndRegion
|
||||
Reference in New Issue
Block a user