-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
action.yml
341 lines (297 loc) · 13 KB
/
action.yml
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
name: BuildPipeScript
description: Builds code using PipeScript
inputs:
Script:
required: false
description: |
A PowerShell Script that uses PipeScript.
Any files outputted from the script will be added to the repository.
If those files have a .Message attached to them, they will be committed with that message.
SkipBuild:
required: false
description: If set, will not run Build-PipeScript.
CommitMessage:
required: false
description: |
If provided, will commit any remaining changes made to the workspace with this commit message.
If no commit message is provided, if a GitHub Event contains a commit message, that message will be used.
If no commit message is provided, and a commit message cannot be automatically detected, changes will not be committed.
InstallModule:
required: false
default: 'ugit'
description: |
A list of modules to be installed from the PowerShell gallery before scripts run.
Note: If ugit is not installed, commit messages will not be carried properly with multiple commits.
Serial:
required: false
description: If set, will build files one-by-one, instead of in parallel. This is currently the default.
Parallel:
required: false
description: If set, will build files in parallel.
BatchSize:
required: false
default: 11
description: The number of files to build in each batch.
ThrottleLimit:
required: false
default: 7
description: |
The throttle limit for parallel jobs.
If too many jobs are running at once, errors may occur.
UserEmail:
required: false
description: The user email associated with a git commit.
UserName:
required: false
description: The user name associated with a git commit.
branding:
icon: code
color: blue
runs:
using: composite
steps:
- name: PipeScriptAction
id: PipeScriptAction
shell: pwsh
env:
UserName: ${{inputs.UserName}}
Script: ${{inputs.Script}}
CommitMessage: ${{inputs.CommitMessage}}
InstallModule: ${{inputs.InstallModule}}
SkipBuild: ${{inputs.SkipBuild}}
UserEmail: ${{inputs.UserEmail}}
BatchSize: ${{inputs.BatchSize}}
Parallel: ${{inputs.Parallel}}
Serial: ${{inputs.Serial}}
ThrottleLimit: ${{inputs.ThrottleLimit}}
run: |
$Parameters = @{}
$Parameters.Script = ${env:Script}
$Parameters.SkipBuild = ${env:SkipBuild}
$Parameters.SkipBuild = $parameters.SkipBuild -match 'true';
$Parameters.CommitMessage = ${env:CommitMessage}
$Parameters.InstallModule = ${env:InstallModule}
$Parameters.InstallModule = $parameters.InstallModule -split ';' -replace '^[''"]' -replace '[''"]$'
$Parameters.Serial = ${env:Serial}
$Parameters.Serial = $parameters.Serial -match 'true';
$Parameters.Parallel = ${env:Parallel}
$Parameters.Parallel = $parameters.Parallel -match 'true';
$Parameters.BatchSize = ${env:BatchSize}
$Parameters.ThrottleLimit = ${env:ThrottleLimit}
$Parameters.UserEmail = ${env:UserEmail}
$Parameters.UserName = ${env:UserName}
foreach ($k in @($parameters.Keys)) {
if ([String]::IsNullOrEmpty($parameters[$k])) {
$parameters.Remove($k)
}
}
Write-Host "::debug:: PipeScriptAction $(@(foreach ($p in $Parameters.GetEnumerator()) {'-' + $p.Key + ' ' + $p.Value}) -join ' ')"
& {<#
.Synopsis
GitHub Action for PipeScript
.Description
GitHub Action for PipeScript. This will:
* Import PipeScript
* Run all *.PipeScript.ps1 files beneath the workflow directory
* Run a .PipeScriptScript parameter
Any files changed can be outputted by the script, and those changes can be checked back into the repo.
Make sure to use the "persistCredentials" option with checkout.
#>
param(
# A PowerShell Script that uses PipeScript.
# Any files outputted from the script will be added to the repository.
# If those files have a .Message attached to them, they will be committed with that message.
[string]
$Script,
# If set, will not run Build-PipeScript.
[switch]
$SkipBuild,
# If provided, will commit any remaining changes made to the workspace with this commit message.
# If no commit message is provided, if a GitHub Event contains a commit message, that message will be used.
# If no commit message is provided, and a commit message cannot be automatically detected, changes will not be committed.
[string]
$CommitMessage,
# A list of modules to be installed from the PowerShell gallery before scripts run.
# Note: If ugit is not installed, commit messages will not be carried properly with multiple commits.
[string[]]
$InstallModule = 'ugit',
# If set, will build files one-by-one, instead of in parallel. This is currently the default.
[switch]
$Serial,
# If set, will build files in parallel.
[switch]
$Parallel,
# The number of files to build in each batch.
[int]
$BatchSize = 11,
# The throttle limit for parallel jobs.
# If too many jobs are running at once, errors may occur.
[int]
$ThrottleLimit = 7,
# The user email associated with a git commit.
[string]
$UserEmail,
# The user name associated with a git commit.
[string]
$UserName
)
"::group::Parameters" | Out-Host
[PSCustomObject]$PSBoundParameters | Format-List | Out-Host
"::endgroup::" | Out-Host
$gitHubEvent = if ($env:GITHUB_EVENT_PATH) {
[IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json
} else { $null }
@"
::group::GitHubEvent
$($gitHubEvent | ConvertTo-Json -Depth 100)
::endgroup::
"@ | Out-Host
# Set -ErrorActionPreference to continue.
$global:ErrorActionPreference = 'continue'
#region -InstallModule
if ($InstallModule) {
"::group::Installing Modules" | Out-Host
foreach ($moduleToInstall in $InstallModule) {
$moduleInWorkspace = Get-ChildItem -Path $env:GITHUB_WORKSPACE -Recurse -File |
Where-Object Name -eq "$($moduleToInstall).psd1" |
Where-Object {
$(Get-Content $_.FullName -Raw) -match 'ModuleVersion'
}
if (-not $moduleInWorkspace) {
Install-Module $moduleToInstall -Scope CurrentUser -Force -AllowClobber
Import-Module $moduleToInstall -Force -PassThru | Out-Host
}
}
"::endgroup::" | Out-Host
}
#endregion -InstallModule
$PSD1Found = Get-ChildItem -Recurse -Filter "*.psd1" | Where-Object Name -eq 'PipeScript.psd1' | Select-Object -First 1
if ($PSD1Found) {
$PipeScriptModulePath = $PSD1Found
Import-Module $PSD1Found -Force -PassThru | Out-Host
} elseif ($env:GITHUB_ACTION_PATH) {
$PipeScriptModulePath = Join-Path $env:GITHUB_ACTION_PATH 'PipeScript.psd1'
if (Test-path $PipeScriptModulePath) {
Import-Module $PipeScriptModulePath -Force -PassThru | Out-Host
} else {
throw "PipeScript not found"
}
} elseif (-not (Get-Module PipeScript)) {
throw "Action Path not found"
}
"::notice title=ModuleLoaded::PipeScript Loaded from Path - $($PipeScriptModulePath)" | Out-Host
$anyFilesChanged = $false
$processScriptOutput = { process {
$out = $_
$outItem = Get-Item -Path $out -ErrorAction SilentlyContinue
$fullName, $shouldCommit =
if ($out -is [IO.FileInfo]) {
$out.FullName, (git status $out.Fullname -s)
} elseif ($outItem) {
$outItem.FullName, (git status $outItem.Fullname -s)
}
if ($shouldCommit) {
git add $fullName
if ($out.Message) {
git commit -m "$($out.Message)"
} elseif ($out.CommitMessage) {
git commit -m "$($out.CommitMessage)"
}
elseif ($CommitMessage) {
git commit -m $CommitMessage
}
elseif ($gitHubEvent.head_commit.message) {
git commit -m "$($gitHubEvent.head_commit.message)"
}
$anyFilesChanged = $true
}
$out
} }
if (-not $UserName) { $UserName = $env:GITHUB_ACTOR }
if (-not $UserEmail) {
$UserEmail =
if ($GitHubUserEmail) {
$GitHubUserEmail
} else {
}
}
git config --global user.email $UserEmail
git config --global user.name $UserName
if (-not $env:GITHUB_WORKSPACE) { throw "No GitHub workspace" }
$branchName = git rev-parse --abrev-ref HEAD
if (-not $branchName) {
return
}
try { $unshallowResults = git fetch --unshallow } catch { $_ | Out-Host }
if (-not $Parallel) { $serial= $true}
$PipeScriptStart = [DateTime]::Now
if ($Script) {
Invoke-PipeScript -Command $Script |
. $processScriptOutput |
Out-Host
}
$PipeScriptTook = [Datetime]::Now - $PipeScriptStart
"::notice:: .PipeScript ran in $($PipeScriptTook.TotalMilliseconds) ms" | Out-Host
"::notice:: Building Files in '$env:GITHUB_WORKSPACE'" | Out-Host
$BuildPipeScriptStart = [DateTime]::Now
$pipeScriptBuildErrors = $null
if (-not $SkipBuild) {
$buildOutputFiles = @(Build-Pipescript -Serial:$Serial -BatchSize:$BatchSize -ThrottleLimit:$ThrottleLimit -InputPath $env:GITHUB_WORKSPACE -ErrorVariable pipeScriptBuildErrors)
if ($buildOutputFiles) {
"::notice:: $($buildOutputFiles.Length) files outputted" | Out-Host
"$($buildOutputFiles.FullName -join [Environment]::newLine)" | Out-Host
$buildOutputFiles |
. $processScriptOutput |
Out-Host
}
}
if ($pipeScriptBuildErrors) {
"There were build errors" | Out-Host
"::error::$($pipeScriptBuildErrors | Out-String)" | Out-Host
$pipeScriptBuildErrors
exit 1
}
$BuildPipeScriptEnd = [DateTime]::Now
$BuildPipeScriptTook = $BuildPipeScriptEnd - $BuildPipeScriptStart
[long]$TotalFileLength = 0
$buildFilePaths = @(
foreach ($buildOutputFile in $buildOutputFiles) {
if ($buildOutputFile.FullName) {
$buildOutputFile.FullName
if ($buildOutputFile.Length) {
$TotalFileLength+=$buildOutputFile.Length
}
}
}
)
"::notice:: $($buildOutputFiles.Length) files built ( $([Math]::Round($TotalFileLength/1kb, 2))kb ) in $($BuildPipeScriptTook.TotalSeconds) seconds" | Out-Host
"::group:: Output Files" |
Out-Host
$buildFilePaths -join [Environment]::newLine | Out-Host
"::endgroup::" | Out-Host
if ($CommitMessage -or $anyFilesChanged) {
if ($CommitMessage) {
Get-ChildItem $env:GITHUB_WORKSPACE -Recurse |
ForEach-Object {
$gitStatusOutput = git status $_.Fullname -s
if ($gitStatusOutput) {
git add $_.Fullname
}
}
git commit -m $ExecutionContext.SessionState.InvokeCommand.ExpandString($CommitMessage)
}
$checkDetached = git symbolic-ref -q HEAD
if (-not $LASTEXITCODE) {
"::notice::Pulling Changes" | Out-Host
git pull | Out-Host
"::notice::Pushing Changes" | Out-Host
git push | Out-Host
"Git Push Output: $($gitPushed | Out-String)"
} else {
"::notice::Not pushing changes (on detached head)" | Out-Host
$LASTEXITCODE = 0
exit 0
}
}
} @Parameters