|
| 1 | + |
| 2 | +name: BuildPipeScript |
| 3 | +description: Builds code using PipeScript |
| 4 | +inputs: |
| 5 | + PipeScript: |
| 6 | + required: false |
| 7 | + description: | |
| 8 | + A PowerShell Script that uses PipeScript. |
| 9 | + Any files outputted from the script will be added to the repository. |
| 10 | + If those files have a .Message attached to them, they will be committed with that message. |
| 11 | + SkipBuild: |
| 12 | + required: false |
| 13 | + description: If set, will not run Build-PipeScript. |
| 14 | + CommitMessage: |
| 15 | + required: false |
| 16 | + description: | |
| 17 | + If provided, will commit any remaining changes made to the workspace with this commit message. |
| 18 | + If no commit message is provided, if a GitHub Event contains a commit message, that message will be used. |
| 19 | + If no commit message is provided, and a commit message cannot be automatically detected, changes will not be committed. |
| 20 | + UserEmail: |
| 21 | + required: false |
| 22 | + description: The user email associated with a git commit. |
| 23 | + UserName: |
| 24 | + required: false |
| 25 | + description: The user name associated with a git commit. |
| 26 | +branding: |
| 27 | + icon: code |
| 28 | + color: blue |
| 29 | +outputs: |
| 30 | + |
| 31 | + PipeScriptRuntime: |
| 32 | + description: The time it took the .PipeScript parameter to run |
| 33 | + value: ${{steps.PipeScriptAction.outputs.PipeScriptRuntime}} |
| 34 | + PipeScriptBuildRuntime: |
| 35 | + description: The time it took Build-PipeScript to run |
| 36 | + value: ${{steps.PipeScriptAction.outputs.PipeScriptBuildRuntime}} |
| 37 | + PipeScriptFilesBuilt: |
| 38 | + description: The files built using PipeScript (separated by semicolons) |
| 39 | + value: ${{steps.PipeScriptAction.outputs.PipeScriptFilesBuilt}} |
| 40 | + PipeScriptFilesBuiltCount: |
| 41 | + description: The number of .PipeScript.ps1 files that were run |
| 42 | + value: ${{steps.PipeScriptAction.outputs.PipeScriptFilesBuiltCount}} |
| 43 | +runs: |
| 44 | + using: composite |
| 45 | + steps: |
| 46 | + - name: PipeScriptAction |
| 47 | + id: PipeScriptAction |
| 48 | + shell: pwsh |
| 49 | + env: |
| 50 | + SkipBuild: ${{inputs.SkipBuild}} |
| 51 | + CommitMessage: ${{inputs.CommitMessage}} |
| 52 | + PipeScript: ${{inputs.PipeScript}} |
| 53 | + UserEmail: ${{inputs.UserEmail}} |
| 54 | + UserName: ${{inputs.UserName}} |
| 55 | + run: | |
| 56 | + $Parameters = @{} |
| 57 | + $Parameters.PipeScript = ${env:PipeScript} |
| 58 | + $Parameters.SkipBuild = ${env:SkipBuild} |
| 59 | + $Parameters.SkipBuild = $parameters.SkipBuild -match 'true'; |
| 60 | + $Parameters.CommitMessage = ${env:CommitMessage} |
| 61 | + $Parameters.UserEmail = ${env:UserEmail} |
| 62 | + $Parameters.UserName = ${env:UserName} |
| 63 | + foreach ($k in @($parameters.Keys)) { |
| 64 | + if ([String]::IsNullOrEmpty($parameters[$k])) { |
| 65 | + $parameters.Remove($k) |
| 66 | + } |
| 67 | + } |
| 68 | + Write-Host "::debug:: PipeScriptAction $(@(foreach ($p in $Parameters.GetEnumerator()) {'-' + $p.Key + ' ' + $p.Value}) -join ' ')" |
| 69 | + & {<# |
| 70 | + .Synopsis |
| 71 | + GitHub Action for PipeScript |
| 72 | + .Description |
| 73 | + GitHub Action for PipeScript. This will: |
| 74 | + |
| 75 | + * Import PipeScript |
| 76 | + * Run all *.PipeScript.ps1 files beneath the workflow directory |
| 77 | + * Run a .PipeScriptScript parameter |
| 78 | + |
| 79 | + Any files changed can be outputted by the script, and those changes can be checked back into the repo. |
| 80 | + Make sure to use the "persistCredentials" option with checkout. |
| 81 | + #> |
| 82 | + |
| 83 | + param( |
| 84 | + # A PowerShell Script that uses PipeScript. |
| 85 | + # Any files outputted from the script will be added to the repository. |
| 86 | + # If those files have a .Message attached to them, they will be committed with that message. |
| 87 | + [string] |
| 88 | + $PipeScript, |
| 89 | + |
| 90 | + # If set, will not run Build-PipeScript. |
| 91 | + [switch] |
| 92 | + $SkipBuild, |
| 93 | + |
| 94 | + # If provided, will commit any remaining changes made to the workspace with this commit message. |
| 95 | + # If no commit message is provided, if a GitHub Event contains a commit message, that message will be used. |
| 96 | + # If no commit message is provided, and a commit message cannot be automatically detected, changes will not be committed. |
| 97 | + [string] |
| 98 | + $CommitMessage, |
| 99 | + |
| 100 | + # The user email associated with a git commit. |
| 101 | + [string] |
| 102 | + $UserEmail, |
| 103 | + |
| 104 | + # The user name associated with a git commit. |
| 105 | + [string] |
| 106 | + $UserName |
| 107 | + ) |
| 108 | + |
| 109 | + "::group::Parameters" | Out-Host |
| 110 | + [PSCustomObject]$PSBoundParameters | Format-List | Out-Host |
| 111 | + "::endgroup::" | Out-Host |
| 112 | + |
| 113 | + $gitHubEvent = if ($env:GITHUB_EVENT_PATH) { |
| 114 | + [IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json |
| 115 | + } else { $null } |
| 116 | + |
| 117 | + @" |
| 118 | + ::group::GitHubEvent |
| 119 | + $($gitHubEvent | ConvertTo-Json -Depth 100) |
| 120 | + ::endgroup:: |
| 121 | + "@ | Out-Host |
| 122 | + |
| 123 | + if ($env:GITHUB_ACTION_PATH) { |
| 124 | + $PipeScriptModulePath = Join-Path $env:GITHUB_ACTION_PATH 'PipeScript.psd1' |
| 125 | + if (Test-path $PipeScriptModulePath) { |
| 126 | + Import-Module $PipeScriptModulePath -Force -PassThru | Out-String |
| 127 | + } else { |
| 128 | + throw "PipeScript not found" |
| 129 | + } |
| 130 | + } elseif (-not (Get-Module PipeScript)) { |
| 131 | + throw "Action Path not found" |
| 132 | + } |
| 133 | + |
| 134 | + "::notice title=ModuleLoaded::PipeScript Loaded from Path - $($PipeScriptModulePath)" | Out-Host |
| 135 | + |
| 136 | + $anyFilesChanged = $false |
| 137 | + $processScriptOutput = { process { |
| 138 | + $out = $_ |
| 139 | + $outItem = Get-Item -Path $out -ErrorAction SilentlyContinue |
| 140 | + $fullName, $shouldCommit = |
| 141 | + if ($out -is [IO.FileInfo]) { |
| 142 | + $out.FullName, (git status $out.Fullname -s) |
| 143 | + } elseif ($outItem) { |
| 144 | + $outItem.FullName, (git status $outItem.Fullname -s) |
| 145 | + } |
| 146 | + if ($shouldCommit) { |
| 147 | + git add $fullName |
| 148 | + if ($out.Message) { |
| 149 | + git commit -m "$($out.Message)" |
| 150 | + } elseif ($out.CommitMessage) { |
| 151 | + git commit -m "$($out.CommitMessage)" |
| 152 | + } |
| 153 | + elseif ($CommitMessage) { |
| 154 | + git commit -m $CommitMessage |
| 155 | + } |
| 156 | + elseif ($gitHubEvent.head_commit.message) { |
| 157 | + git commit -m "$($gitHubEvent.head_commit.message)" |
| 158 | + } |
| 159 | + $anyFilesChanged = $true |
| 160 | + } |
| 161 | + $out |
| 162 | + } } |
| 163 | + |
| 164 | + |
| 165 | + if (-not $UserName) { $UserName = $env:GITHUB_ACTOR } |
| 166 | + if (-not $UserEmail) { $UserEmail = "[email protected]" } |
| 167 | + git config --global user.email $UserEmail |
| 168 | + git config --global user.name $UserName |
| 169 | + |
| 170 | + if (-not $env:GITHUB_WORKSPACE) { throw "No GitHub workspace" } |
| 171 | + |
| 172 | + git pull | Out-Host |
| 173 | + |
| 174 | + $PipeScriptStart = [DateTime]::Now |
| 175 | + if ($PipeScript) { |
| 176 | + Invoke-PipeScript -Command $PipeScript | |
| 177 | + . $processScriptOutput | |
| 178 | + Out-Host |
| 179 | + } |
| 180 | + |
| 181 | + $PipeScriptTook = [Datetime]::Now - $PipeScriptScriptStart |
| 182 | + "::set-output name=PipeScriptRuntime::$($PipeScriptScriptTook.TotalMilliseconds)" | Out-Host |
| 183 | + |
| 184 | + $BuildPipeScriptStart = [DateTime]::Now |
| 185 | + if (-not $SkipBuild) { |
| 186 | + $buildOutputFiles = @(Build-Pipescript -InputPath $env:GITHUB_WORKSPACE) |
| 187 | + $buildOutputFiles | |
| 188 | + . $processScriptOutput | |
| 189 | + Out-Host |
| 190 | + } |
| 191 | + |
| 192 | + $BuildPipeScriptEnd = [DateTime]::Now |
| 193 | + $BuildPipeScriptTook = $BuildPipeScriptEnd - $BuildPipeScriptStart |
| 194 | + "::set-output name=PipeScriptFilesBuiltCount::$($buildOutputFiles.Length)" | Out-Host |
| 195 | + "::set-output name=PipeScriptFilesBuilt::$($buildOutputFiles -join ';')" | Out-Host |
| 196 | + "::set-output name=PipeScriptBuildRuntime::$($BuildPipeScriptTook.TotalMilliseconds)" | Out-Host |
| 197 | + if ($CommitMessage -or $anyFilesChanged) { |
| 198 | + if ($CommitMessage) { |
| 199 | + dir $env:GITHUB_WORKSPACE -Recurse | |
| 200 | + ForEach-Object { |
| 201 | + $gitStatusOutput = git status $_.Fullname -s |
| 202 | + if ($gitStatusOutput) { |
| 203 | + git add $_.Fullname |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + git commit -m $ExecutionContext.SessionState.InvokeCommand.ExpandString($CommitMessage) |
| 208 | + } |
| 209 | + |
| 210 | + $checkDetached = git symbolic-ref -q HEAD |
| 211 | + if (-not $LASTEXITCODE) { |
| 212 | + "::notice::Pushing Changes" | Out-Host |
| 213 | + git push |
| 214 | + "Git Push Output: $($gitPushed | Out-String)" |
| 215 | + } else { |
| 216 | + "::notice::Not pushing changes (on detached head)" | Out-Host |
| 217 | + $LASTEXITCODE = 0 |
| 218 | + exit 0 |
| 219 | + } |
| 220 | + } |
| 221 | + } @Parameters |
| 222 | +
|
0 commit comments