|
1 | 1 | name: Deploy |
| 2 | + |
2 | 3 | on: |
3 | 4 | push: |
4 | 5 | branches: [ main ] |
5 | 6 | workflow_dispatch: |
| 7 | + |
6 | 8 | defaults: |
7 | 9 | run: |
8 | 10 | shell: pwsh |
| 11 | + |
9 | 12 | jobs: |
10 | 13 | Deploy: |
11 | | - runs-on: windows-latest |
| 14 | + runs-on: ubuntu-latest |
12 | 15 | steps: |
13 | | - - uses: actions/checkout@v2 |
14 | | - with: |
15 | | - fetch-depth: 2 |
16 | | - - name: Discover Changes |
17 | | - run: | |
18 | | - $changedModules = $(git diff --name-only HEAD HEAD~) |
19 | | - $changedModules |
20 | | - $files = $changedModules -split ' ' | ForEach-Object{[System.IO.FileInfo] $_} |
21 | | - $modules = @() |
22 | | - foreach ($file in $files) |
23 | | - { |
24 | | - if((Test-Path $file.FullName)){ |
25 | | - $fileDirectoryParent = $file.Directory.Parent |
26 | | - if ($fileDirectoryParent -and $fileDirectoryParent.Name -eq "Modules") { |
27 | | - $modules += $file.Directory |
28 | | - } |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 2 |
| 19 | + |
| 20 | + - name: Discover Changed Modules |
| 21 | + id: discover |
| 22 | + shell: pwsh |
| 23 | + run: | |
| 24 | + $changed = git diff --name-only HEAD~ HEAD |
| 25 | + $changed |
| 26 | + $modules = @() |
| 27 | + foreach ($file in $changed) { |
| 28 | + $parts = $file -split '/' |
| 29 | + if ($parts.Length -ge 2 -and $parts[0] -eq 'Modules') { |
| 30 | + $modules += $parts[1] |
29 | 31 | } |
30 | | - } |
31 | | - $modules.Length |
32 | | - if($modules.Length -eq 0){ |
33 | | - exit |
34 | | - } |
35 | | - $changedModulesPath = mkdir -Name "StagingChangedModules\" -Force |
36 | | - For ($i=0; $i -lt $modules.Length; $i++) |
37 | | - { |
38 | | - $module = $modules[$i] |
39 | | - Copy-Item -Path $module.FullName -Destination $changedModulesPath -Recurse -Force |
40 | | - } |
41 | | - - name: Update Changed Modules |
42 | | - run: | |
43 | | - if(!(Test-Path StagingChangedModules)) { |
44 | | - Write-Host "No modules changed" |
45 | | - exit |
46 | | - } |
47 | | - $moduleFolders = Get-ChildItem StagingChangedModules |
48 | | - foreach ($item in $moduleFolders){ |
49 | | - $moduleName = $item.Name |
50 | | - $manifest = Get-ChildItem $item.PSPath | Where-Object{$_.Name -like "*psd1"} |
51 | | - if(!$manifest){ |
52 | | - Write-Error "The manifest for $moduleName was not found" |
| 32 | + } |
| 33 | + $modules = $modules | Sort-Object -Unique |
| 34 | + Write-Host "Changed modules: $($modules -join ', ')" |
| 35 | + if ($modules.Length -eq 0) { |
| 36 | + "modules=" >> $env:GITHUB_OUTPUT |
| 37 | + return |
| 38 | + } |
| 39 | + $staging = New-Item -ItemType Directory -Force -Path StagingChangedModules |
| 40 | + foreach ($m in $modules) { |
| 41 | + Copy-Item -Path "Modules/$m" -Destination $staging.FullName -Recurse -Force |
| 42 | + } |
| 43 | + "modules=$($modules -join ',')" >> $env:GITHUB_OUTPUT |
| 44 | +
|
| 45 | + - name: Bump Module Version |
| 46 | + if: steps.discover.outputs.modules != '' |
| 47 | + shell: pwsh |
| 48 | + run: | |
| 49 | + $moduleFolders = Get-ChildItem StagingChangedModules -Directory |
| 50 | + foreach ($item in $moduleFolders) { |
| 51 | + $manifest = Get-ChildItem $item.FullName -Filter '*.psd1' | Select-Object -First 1 |
| 52 | + if (-not $manifest) { |
| 53 | + Write-Error "Manifest for $($item.Name) was not found" |
| 54 | + continue |
53 | 55 | } |
54 | | - $content = Get-Content $manifest.PSPath | ForEach-Object{ |
55 | | - $_ |
56 | | - if ($_ -match "ModuleVersion"){ |
57 | | - $version = [System.Version]($_ -split "'")[1] |
58 | | - } |
59 | | - } |
60 | | - $major = 0 |
61 | | - $minor = 0 |
62 | | - $build = 0 |
63 | | - $minorRev = 1 |
64 | | - if($version.Major -gt 0){$major = $version.Major} |
65 | | - if($version.Minor -gt 0){$minor = $version.Minor} |
66 | | - if($version.Build -gt 0){$build = $version.Build} |
67 | | - if($version.MinorRevision -gt 0){$minorRev = $version.MinorRevision + 1} |
68 | | - $updatedVersion = New-Object -TypeName system.Version -ArgumentList $major, $minor, $build, $minorRev |
69 | | - $PushPath = ".\Modules\" +($manifest.PSChildName).Substring(0, $manifest.PSChildName.length - 5 ) + "\"+ ($manifest.PSChildName) |
70 | | - $PublishPath = ".\StagingChangedModules\" +($manifest.PSChildName).Substring(0, $manifest.PSChildName.length - 5 ) + "\"+ ($manifest.PSChildName) |
71 | | - |
72 | | - Update-ModuleManifest -Path $PushPath -ModuleVersion $updatedVersion |
73 | | - Update-ModuleManifest -Path $PublishPath -ModuleVersion $updatedVersion |
74 | | - |
75 | | - Write-Host "$moduleName's version was updated from $version to $updatedVersion" |
76 | | - } |
77 | | - - name: Push Changes |
78 | | - run: | |
79 | | - if(!(Test-Path StagingChangedModules)) { |
80 | | - Write-Host "No modules changed" |
81 | | - exit |
82 | | - } |
83 | | - git config --global user.email "tylerjones321@gmail.com" |
84 | | - git config --global user.name "worseTyler" |
85 | | - git add Modules/\*.psd1 |
86 | | - git commit -m "[skip ci] Commit from build agent" |
87 | | - git push |
88 | | - - name: Publish To Gallery |
89 | | - run: | |
90 | | - if(!(Test-Path StagingChangedModules)) { |
91 | | - Write-Host "No modules changed" |
92 | | - Write-Host "Nothing to Publish" |
93 | | - exit |
94 | | - } |
95 | | - $moduleFolders = Get-ChildItem StagingChangedModules |
96 | | - foreach ($item in $moduleFolders){ |
97 | | - Publish-Module -Path $item.FullName -NuGetApiKey ${{ secrets.POWERSHELL_GALLERY_API_KEY }} -Verbose |
98 | | - } |
| 56 | + $current = (Test-ModuleManifest -Path $manifest.FullName).Version |
| 57 | + $build = if ($current.Build -ge 0) { $current.Build } else { 0 } |
| 58 | + $revision = if ($current.Revision -ge 0) { $current.Revision + 1 } else { 1 } |
| 59 | + $updated = [Version]::new($current.Major, $current.Minor, $build, $revision) |
| 60 | + $repoManifest = Join-Path '.' 'Modules' $item.Name $manifest.Name |
| 61 | + Update-ModuleManifest -Path $repoManifest -ModuleVersion $updated |
| 62 | + Update-ModuleManifest -Path $manifest.FullName -ModuleVersion $updated |
| 63 | + Write-Host "$($item.Name): $current -> $updated" |
| 64 | + } |
| 65 | +
|
| 66 | + - name: Commit Version Bump |
| 67 | + if: steps.discover.outputs.modules != '' |
| 68 | + shell: bash |
| 69 | + run: | |
| 70 | + git config --global user.email "actions@github.com" |
| 71 | + git config --global user.name "github-actions[bot]" |
| 72 | + git add Modules/*/*.psd1 |
| 73 | + if ! git diff --staged --quiet; then |
| 74 | + git commit -m "[skip ci] Bump module version from build agent" |
| 75 | + git push |
| 76 | + fi |
| 77 | +
|
| 78 | + - name: Install PSResourceGet |
| 79 | + if: steps.discover.outputs.modules != '' |
| 80 | + shell: pwsh |
| 81 | + run: | |
| 82 | + if (-not (Get-Module -ListAvailable -Name Microsoft.PowerShell.PSResourceGet)) { |
| 83 | + Install-Module -Name Microsoft.PowerShell.PSResourceGet -Force -Scope CurrentUser |
| 84 | + } |
| 85 | +
|
| 86 | + - name: Publish to PowerShell Gallery |
| 87 | + if: steps.discover.outputs.modules != '' |
| 88 | + shell: pwsh |
| 89 | + env: |
| 90 | + PSGALLERY_API_KEY: ${{ secrets.POWERSHELL_GALLERY_API_KEY }} |
| 91 | + run: | |
| 92 | + Import-Module Microsoft.PowerShell.PSResourceGet |
| 93 | + foreach ($item in Get-ChildItem StagingChangedModules -Directory) { |
| 94 | + Write-Host "Publishing $($item.Name) ..." |
| 95 | + Publish-PSResource -Path $item.FullName -ApiKey $env:PSGALLERY_API_KEY -Repository PSGallery -Verbose |
| 96 | + } |
0 commit comments