Skip to content

Commit 93ef5f2

Browse files
Merge pull request #94 from StartAutomating/GitHubWorkflowBuildSteps
GitHub Workflow Steps
2 parents a691e61 + 654bbde commit 93ef5f2

13 files changed

+326
-6
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: UpdateModuleTag
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
branches:
6+
- main
7+
- master
8+
paths-ignore:
9+
- 'docs/**'
10+
- '*.help.txt'
11+
- '*.md'
12+
13+
jobs:
14+
UpdateModuleTag:
15+
if: ${{github.event.action == 'closed' && github.event.merged == true}}
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Check out repository
19+
uses: actions/checkout@v2
20+
- name: TagModuleVersion
21+
id: TagModuleVersion
22+
shell: pwsh
23+
run: |
24+
$Parameters = @{}
25+
$Parameters.ModulePath = ${env:ModulePath}
26+
$Parameters.UserEmail = ${env:UserEmail}
27+
$Parameters.UserName = ${env:UserName}
28+
$Parameters.TagVersionFormat = ${env:TagVersionFormat}
29+
$Parameters.TagAnnotationFormat = ${env:TagAnnotationFormat}
30+
foreach ($k in @($parameters.Keys)) {
31+
if ([String]::IsNullOrEmpty($parameters[$k])) {
32+
$parameters.Remove($k)
33+
}
34+
}
35+
Write-Host "::debug:: TagModuleVersion $(@(foreach ($p in $Parameters.GetEnumerator()) {'-' + $p.Key + ' ' + $p.Value}) -join ' ')"
36+
& {param(
37+
[string]
38+
$ModulePath,
39+
40+
# The user email associated with a git commit.
41+
[string]
42+
$UserEmail,
43+
44+
# The user name associated with a git commit.
45+
[string]
46+
$UserName,
47+
48+
# The tag version format (default value: 'v$(imported.Version)')
49+
# This can expand variables. $imported will contain the imported module.
50+
[string]
51+
$TagVersionFormat = 'v$($imported.Version)',
52+
53+
# The tag version format (default value: '$($imported.Name) $(imported.Version)')
54+
# This can expand variables. $imported will contain the imported module.
55+
[string]
56+
$TagAnnotationFormat = '$($imported.Name) $($imported.Version)'
57+
)
58+
59+
$imported =
60+
if (-not $ModulePath) {
61+
$orgName, $moduleName = $env:BUILD_REPOSITORY_ID -split "/"
62+
Import-Module ".\$moduleName.psd1" -Force -PassThru -Global
63+
} else {
64+
Import-Module $modulePath -Force -PassThru -Global
65+
}
66+
67+
if (-not $imported) { return }
68+
69+
$targetVersion =$ExecutionContext.InvokeCommand.ExpandString($TagVersionFormat)
70+
71+
$versionTagExists = git tag --list | Where-Object { $_ -eq $targetVersion }
72+
73+
if ($versionTagExists) {
74+
"::warning::Version $($versionTagExists)"
75+
return
76+
}
77+
78+
if (-not $UserName) { $UserName = $env:GITHUB_ACTOR }
79+
if (-not $UserEmail) { $UserEmail = "[email protected]" }
80+
git config --global user.email $UserEmail
81+
git config --global user.name $UserName
82+
83+
git tag -a $targetVersion -m $ExecutionContext.InvokeCommand.ExpandString($TagAnnotationFormat)
84+
git push --tags
85+
86+
if ($env:GITHUB_ACTOR) {
87+
exit 0
88+
}
89+
90+
91+
} @Parameters
92+

Convert-BuildStep.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,20 @@ $collectParameters
422422
Import-Module `$($modulePathVariable) -Force -PassThru
423423
$logParameters
424424
$Name `@Parameters
425+
"@)
426+
$innerScript = $sb
427+
} elseif ($BuildOption.RootDirectory -and $ScriptBlock.File -and
428+
$ScriptBlock.File -like "$($BuildOption.RootDirectory)*") {
429+
$relativeScriptPath =
430+
$ScriptBlock.File.Substring(
431+
$BuildOption.RootDirectory.Length
432+
).TrimStart(
433+
[IO.Path]::DirectorySeparatorChar
434+
).Replace('\','/')
435+
$sb = [ScriptBlock]::Create(@"
436+
$CollectParameters
437+
$logParameters
438+
& './$relativeScriptPath' `@Parameters
425439
"@)
426440
$innerScript = $sb
427441
} else {

Expand-BuildStep.ps1

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
}
109109

110110
$splatMe.Remove('StepMap')
111-
:nextKey foreach ($kv in $stepMap.GetEnumerator()) {
111+
112+
:nextKey foreach ($kv in $stepMap.GetEnumerator()) {
112113
if ($kv.Key.EndsWith('s') -and -not $singleton) { # Already pluralized
113114
$thingType = $kv.Key.Substring(0,$kv.Key.Length -1)
114115
$propName = $kv.Key
@@ -117,8 +118,9 @@
117118
$propName = $kv.Key
118119
} else {
119120
$thingType = $kv.Key
121+
$thingTypePlural = $kv.Key + 's'
120122
$propName =
121-
if ($SingleItemName -notcontains $thingType -and
123+
if ($SingleItemName -notcontains $thingType -and
122124
$thingType -notmatch '\W$' -and
123125
$theComponentNames.Keys -contains $thingType) {
124126
$kv.Key.Substring(0,1).ToLower() + $kv.Key.Substring(1) + 's'
@@ -312,6 +314,26 @@
312314
$outObject[$propName] -isnot [Collections.IList]) {
313315
$outObject[$propName] = @($outObject[$propName])
314316
}
317+
318+
if ($SingleItemName -contains $propName -and
319+
$outObject[$propName] -is [Collections.IList]) {
320+
$newOut = [Ordered]@{}
321+
foreach ($obj in $outObject[$propName]) {
322+
if ($obj -is [Collections.IDictionary]) {
323+
$k = @($obj.Keys)[0]
324+
if ($obj.Count -eq 1 -and
325+
$obj.Keys -contains $k) {
326+
$newOut[$k] = $obj.$k
327+
} else {
328+
$newOut[$k] = $obj
329+
}
330+
}
331+
elseif ($obj -is [string]) {
332+
$newOut[$obj] = [Ordered]@{}
333+
}
334+
}
335+
$outObject[$propName] = $newOut
336+
}
315337
}
316338
}
317339
$outObject

GitHub/Jobs/PublishToGallery.psd1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@{
2+
"runs-on" = "ubuntu-latest"
3+
if = '${{ success() }}'
4+
steps = @(
5+
@{
6+
name = 'Check out repository'
7+
uses = 'actions/checkout@v2'
8+
}, 'PublishPowerShellGallery'
9+
)
10+
}
11+
12+

GitHub/Jobs/UpdateModuleTag.psd1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@{
2+
"runs-on" = "ubuntu-latest"
3+
if = '${{ success() }}'
4+
steps = @(
5+
@{
6+
name = 'Check out repository'
7+
uses = 'actions/checkout@v2'
8+
}, 'TagModuleVersion'
9+
)
10+
}
11+

GitHub/On/Demand.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'workflow_dispatch'
1+
@{'workflow_dispatch'=@{}}

GitHub/On/PullRequestMerged.psd1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
pull_request = @{
3+
branches =@('main','master')
4+
if = @'
5+
${{github.event.action == 'closed' && github.event.merged == true}}
6+
'@
7+
"paths-ignore" = @("docs/**","*.help.txt", "*.md")
8+
}
9+
}
10+

GitHub/On/PullToMain.psd1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
pull_request = @{
3+
branches =@('main','master')
4+
"paths-ignore" = @("docs/**","*.help.txt", "*.md")
5+
}
6+
}
7+
8+

GitHub/On/Push.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
'push'
1+
@{'push'=@{}}
22

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
param(
2+
[string]
3+
$ModulePath
4+
)
5+
$gitHubEvent = if ($env:GITHUB_EVENT_PATH) {
6+
[IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json
7+
} else { $null }
8+
9+
10+
@"
11+
::group::GitHubEvent
12+
$($gitHubEvent | ConvertTo-Json -Depth 100)
13+
::endgroup::
14+
"@ | Out-Host
15+
16+
if (-not ($gitHubEvent.head_commit.message -match "Merge Pull Request #(?<PRNumber>\d+)") -and
17+
(-not $gitHubEvent.psobject.properties['inputs'])) {
18+
"::warning::Pull Request has not merged, skipping" | Out-Host
19+
return
20+
}
21+
22+
23+
$imported =
24+
if (-not $ModulePath) {
25+
$orgName, $moduleName = $env:GITHUB_REPOSITORY -split "/"
26+
Import-Module ".\$moduleName.psd1" -Force -PassThru -Global
27+
} else {
28+
Import-Module $modulePath -Force -PassThru -Global
29+
}
30+
31+
if (-not $imported) { return }
32+
33+
$foundModule = try { Find-Module -Name $imported.Name -ErrorAction SilentlyContinue } catch {}
34+
35+
if ($foundModule -and $foundModule.Version -ge $imported.Version) {
36+
"::warning::Gallery Version of $moduleName is more recent ($($foundModule.Version) >= $($imported.Version))" | Out-Host
37+
} else {
38+
39+
$gk = '${{secrets.GALLERYKEY}}'
40+
41+
$rn = Get-Random
42+
$moduleTempFolder = Join-Path $pwd "$rn"
43+
$moduleTempPath = Join-Path $moduleTempFolder $moduleName
44+
New-Item -ItemType Directory -Path $moduleTempPath -Force | Out-Host
45+
46+
Write-Host "Staging Directory: $ModuleTempPath"
47+
48+
$imported | Split-Path |
49+
Get-ChildItem -Force |
50+
Where-Object Name -NE $rn |
51+
Copy-Item -Destination $moduleTempPath -Recurse
52+
53+
$moduleGitPath = Join-Path $moduleTempPath '.git'
54+
Write-Host "Removing .git directory"
55+
if (Test-Path $moduleGitPath) {
56+
Remove-Item -Recurse -Force $moduleGitPath
57+
}
58+
Write-Host "Module Files:"
59+
Get-ChildItem $moduleTempPath -Recurse
60+
Write-Host "Publishing $moduleName [$($imported.Version)] to Gallery"
61+
Publish-Module -Path $moduleTempPath -NuGetApiKey $gk
62+
if ($?) {
63+
Write-Host "Published to Gallery"
64+
} else {
65+
Write-Host "Gallery Publish Failed"
66+
exit 1
67+
}
68+
}

0 commit comments

Comments
 (0)