Skip to content

Commit 9c85d99

Browse files
author
James Brundage
committed
Updating Action (Preferring local bits Fixes #165)
1 parent e920816 commit 9c85d99

File tree

3 files changed

+258
-95
lines changed

3 files changed

+258
-95
lines changed

GitHub/Actions/PSDevOpsAction.ps1

Lines changed: 123 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,43 +47,111 @@ $UserEmail,
4747
$UserName
4848
)
4949

50+
#region Initial Logging
51+
52+
# Output the parameters passed to this script (for debugging)
5053
"::group::Parameters" | Out-Host
5154
[PSCustomObject]$PSBoundParameters | Format-List | Out-Host
5255
"::endgroup::" | Out-Host
5356

54-
if ($env:GITHUB_ACTION_PATH) {
55-
$PSDevOpsModulePath = Join-Path $env:GITHUB_ACTION_PATH 'PSDevOps.psd1'
56-
if (Test-path $PSDevOpsModulePath) {
57-
Import-Module $PSDevOpsModulePath -Force -PassThru | Out-String
58-
} else {
59-
throw "PSDevOps not found"
60-
}
61-
} elseif (-not (Get-Module PSDevOps)) {
62-
throw "Action Path not found"
57+
# Get the GitHub Event
58+
$gitHubEvent =
59+
if ($env:GITHUB_EVENT_PATH) {
60+
[IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json
61+
} else { $null }
62+
63+
# Log the GitHub Event
64+
@"
65+
::group::GitHubEvent
66+
$($gitHubEvent | ConvertTo-Json -Depth 100)
67+
::endgroup::
68+
"@ | Out-Host
69+
70+
# Check that there is a workspace (and throw if there is not)
71+
if (-not $env:GITHUB_WORKSPACE) { throw "No GitHub workspace" }
72+
73+
#endregion Initial Logging
74+
75+
# Check to ensure we are on a branch
76+
$branchName = git rev-parse --abrev-ref HEAD
77+
# If we were not, return.
78+
if (-not $branchName) {
79+
"::warning::Not on a branch" | Out-Host
80+
return
6381
}
6482

65-
"::notice title=ModuleLoaded::PSDevOps Loaded from Path - $($PSDevOpsModulePath)" | Out-Host
83+
#region Configure UserName and Email
84+
if (-not $UserName) {
85+
$UserName =
86+
if ($env:GITHUB_TOKEN) {
87+
Invoke-RestMethod -uri "https://api.github.com/user" -Headers @{
88+
Authorization = "token $env:GITHUB_TOKEN"
89+
} |
90+
Select-Object -First 1 -ExpandProperty name
91+
} else {
92+
$env:GITHUB_ACTOR
93+
}
94+
}
6695

96+
if (-not $UserEmail) {
97+
$GitHubUserEmail =
98+
if ($env:GITHUB_TOKEN) {
99+
Invoke-RestMethod -uri "https://api.github.com/user/emails" -Headers @{
100+
Authorization = "token $env:GITHUB_TOKEN"
101+
} |
102+
Select-Object -First 1 -ExpandProperty email
103+
} else {''}
104+
$UserEmail =
105+
if ($GitHubUserEmail) {
106+
$GitHubUserEmail
107+
} else {
108+
"$UserName@github.com"
109+
}
110+
}
111+
git config --global user.email $UserEmail
112+
git config --global user.name $UserName
113+
#endregion Configure UserName and Email
67114

68-
$ght =
69-
if ($GitHubToken) {
70-
$GitHubToken
71-
} elseif ($env:GITHUB_TOKEN) {
72-
$env:GITHUB_TOKEN
115+
git pull | Out-Host
116+
117+
118+
#region Load Action Module
119+
$ActionModuleName = "EZOut"
120+
$ActionModuleFileName = "$ActionModuleName.psd1"
121+
122+
# Try to find a local copy of the action's module.
123+
# This allows the action to use the current branch's code instead of the action's implementation.
124+
$PSD1Found = Get-ChildItem -Recurse -Filter "*.psd1" |
125+
Where-Object Name -eq $ActionModuleFileName |
126+
Select-Object -First 1
127+
128+
$ActionModulePath, $ActionModule =
129+
# If there was a .PSD1 found
130+
if ($PSD1Found) {
131+
$PSD1Found.FullName # import from there.
132+
Import-Module $PSD1Found.FullName -Force -PassThru
133+
}
134+
# Otherwise, if we have a GITHUB_ACTION_PATH
135+
elseif ($env:GITHUB_ACTION_PATH)
136+
{
137+
$actionModulePath = Join-Path $env:GITHUB_ACTION_PATH $ActionModuleFileName
138+
if (Test-path $actionModulePath) {
139+
$actionModulePath
140+
Import-Module $actionModulePath -Force -PassThru
141+
} else {
142+
throw "$actionModuleName not found"
143+
}
144+
}
145+
elseif (-not (Get-Module $ActionModuleName)) {
146+
throw "$actionModulePath could not be loaded."
73147
}
74-
"::group::Connecting to Github" | Out-Host
75-
$connectStart = [DateTime]::now
76-
Connect-GitHub -PersonalAccessToken $GitHubToken -PassThru |
77-
ForEach-Object {
78-
$githubModule = $_
79-
"::notice title=Connected::Connect-GitHub finished - $($githubModule.ExportedCommands.Count) Commands Imported" | Out-Host
80-
$githubModule.ExportedCommands.Keys -join [Environment]::Newline | Out-Host
81-
} |
82-
Out-Host
83-
"::endgroup::" | Out-Host
148+
149+
"::notice title=ModuleLoaded::$actionModuleName Loaded from Path - $($actionModulePath)" | Out-Host
150+
#endregion Load Action Module
151+
84152

85153
$anyFilesChanged = $false
86-
$processScriptOutput = { process {
154+
filter ProcessScriptOutput {
87155
$out = $_
88156
$outItem = Get-Item -Path $out -ErrorAction SilentlyContinue
89157
$fullName, $shouldCommit =
@@ -98,11 +166,32 @@ $processScriptOutput = { process {
98166
git commit -m "$($out.Message)"
99167
} elseif ($out.CommitMessage) {
100168
git commit -m "$($out.CommitMessage)"
169+
} elseif ($gitHubEvent.head_commit.message) {
170+
git commit -m "$($gitHubEvent.head_commit.message)"
101171
}
102172
$anyFilesChanged = $true
103173
}
104174
$out
105-
} }
175+
}
176+
177+
#endregion Declare Functions and Variables
178+
179+
$ght =
180+
if ($GitHubToken) {
181+
$GitHubToken
182+
} elseif ($env:GITHUB_TOKEN) {
183+
$env:GITHUB_TOKEN
184+
}
185+
"::group::Connecting to Github" | Out-Host
186+
$connectStart = [DateTime]::now
187+
Connect-GitHub -PersonalAccessToken $GitHubToken -PassThru |
188+
ForEach-Object {
189+
$githubModule = $_
190+
"::notice title=Connected::Connect-GitHub finished - $($githubModule.ExportedCommands.Count) Commands Imported" | Out-Host
191+
$githubModule.ExportedCommands.Keys -join [Environment]::Newline | Out-Host
192+
} |
193+
Out-Host
194+
"::endgroup::" | Out-Host
106195

107196

108197
if (-not $UserName) { $UserName = $env:GITHUB_ACTOR }
@@ -117,11 +206,11 @@ git pull | Out-Host
117206
$PSDevOpsScriptStart = [DateTime]::Now
118207
if ($PSDevOpsScript) {
119208
Invoke-Expression -Command $PSDevOpsScript |
120-
. $processScriptOutput |
209+
ProcessScriptOutput |
121210
Out-Host
122211
}
123212
$PSDevOpsScriptTook = [Datetime]::Now - $PSDevOpsScriptStart
124-
"::set-output name=PSDevOpsScriptRuntime::$($PSDevOpsScriptTook.TotalMilliseconds)" | Out-Host
213+
# "::set-output name=PSDevOpsScriptRuntime::$($PSDevOpsScriptTook.TotalMilliseconds)" | Out-Host
125214

126215
$PSDevOpsPS1Start = [DateTime]::Now
127216
$PSDevOpsPS1List = @()
@@ -134,15 +223,15 @@ if (-not $SkipPSDevOpsPS1) {
134223
$PSDevOpsPS1Count++
135224
"::notice title=Running::$($_.Fullname)" | Out-Host
136225
. $_.FullName |
137-
. $processScriptOutput |
226+
ProcessScriptOutput |
138227
Out-Host
139228
}
140229
}
141230
$PSDevOpsPS1EndStart = [DateTime]::Now
142231
$PSDevOpsPS1Took = [Datetime]::Now - $PSDevOpsPS1Start
143-
"::set-output name=PSDevOpsPS1Count::$($PSDevOpsPS1List.Length)" | Out-Host
144-
"::set-output name=PSDevOpsPS1Files::$($PSDevOpsPS1List -join ';')" | Out-Host
145-
"::set-output name=PSDevOpsPS1Runtime::$($PSDevOpsPS1Took.TotalMilliseconds)" | Out-Host
232+
# "::set-output name=PSDevOpsPS1Count::$($PSDevOpsPS1List.Length)" | Out-Host
233+
# "::set-output name=PSDevOpsPS1Files::$($PSDevOpsPS1List -join ';')" | Out-Host
234+
# "::set-output name=PSDevOpsPS1Runtime::$($PSDevOpsPS1Took.TotalMilliseconds)" | Out-Host
146235
if ($CommitMessage -or $anyFilesChanged) {
147236
if ($CommitMessage) {
148237
dir $env:GITHUB_WORKSPACE -Recurse |
@@ -155,10 +244,7 @@ if ($CommitMessage -or $anyFilesChanged) {
155244

156245
git commit -m $ExecutionContext.SessionState.InvokeCommand.ExpandString($CommitMessage)
157246
}
158-
159-
160-
161-
247+
162248
$checkDetached = git symbolic-ref -q HEAD
163249
if (-not $LASTEXITCODE) {
164250
"::notice::Pushing Changes" | Out-Host

PSDevOps.GitHubAction.PSDevOps.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#requires -Module PSDevOps
2+
New-GitHubAction -Name "UsePSDevOps" -Description 'PowerShell Tools for DevOps (including a PowerShell wrapper for the GitHub REST API)' -Action PSDevOpsAction -Icon activity |
3+
Set-Content .\action.yml -Encoding UTF8 -PassThru
4+
5+

0 commit comments

Comments
 (0)