-
-
Notifications
You must be signed in to change notification settings - Fork 0
Return objects from psake scripts #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return objects from psake scripts #29
Conversation
This addresses a common question about how to return data from psake scripts, such as artifact URLs or build metadata. The new documentation covers: - Script-level variables for simple data - Output files (JSON/YAML) for CI/CD integration - Environment variables for pipeline integration - BuildTearDown for centralized reporting - Anti-patterns to avoid - Complete working examples The documentation is added to the Tutorial - Advanced section as it covers patterns for working with build outputs and artifacts.
✅ Deploy Preview for psake ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
The previous version incorrectly stated that script-level variables could be used to return data from psake to the calling script. This is wrong because psake executes the build file in its own scope. Key corrections: - Removed script-level variables as primary recommendation - Made output files (JSON/YAML) the #1 recommended approach - Added clear anti-pattern section explaining why script variables don't work outside psake invocations - Distinguished between inter-task communication (script vars work) and returning data from psake (must use files) - Added direct answer to original question at top of file output section Thanks to the user for catching this fundamental error.
| exit 0 | ||
| ``` | ||
|
|
||
| **Answer to the original question:** For your use case of returning a hashtable with an `ArtifactUrl` field, write it to a JSON file as shown above. This is the standard, reliable approach. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this. We don't use "Answer" anywhere else.
| ### 4. BuildTearDown for Centralized Reporting | ||
|
|
||
| Use `BuildTearDown` to generate summary reports or outputs that run after all tasks complete (even on failure). | ||
|
|
||
| ```powershell title="psakefile.ps1" | ||
| # Track build metadata | ||
| $script:BuildMetrics = @{ | ||
| TasksExecuted = @() | ||
| StartTime = $null | ||
| EndTime = $null | ||
| ArtifactUrl = $null | ||
| } | ||
|
|
||
| BuildSetup { | ||
| $script:BuildMetrics.StartTime = Get-Date | ||
| } | ||
|
|
||
| TaskSetup { | ||
| $taskName = $psake.context.Peek().currentTaskName | ||
| $script:BuildMetrics.TasksExecuted += $taskName | ||
| } | ||
|
|
||
| BuildTearDown { | ||
| $script:BuildMetrics.EndTime = Get-Date | ||
| $duration = $script:BuildMetrics.EndTime - $script:BuildMetrics.StartTime | ||
|
|
||
| # Create summary report (IMPORTANT: Write to file, not just variables) | ||
| $summary = @{ | ||
| Success = $psake.build_success | ||
| Duration = $duration.TotalSeconds | ||
| TasksExecuted = $script:BuildMetrics.TasksExecuted | ||
| ArtifactUrl = $script:BuildMetrics.ArtifactUrl | ||
| Timestamp = (Get-Date).ToString("o") | ||
| } | ||
|
|
||
| # Write to file so it's accessible after Invoke-psake completes | ||
| $summary | ConvertTo-Json | Set-Content ./build-summary.json | ||
|
|
||
| Write-Host "`n========== Build Summary ==========" -ForegroundColor Cyan | ||
| Write-Host "Status: $(if ($psake.build_success) { 'SUCCESS' } else { 'FAILED' })" | ||
| Write-Host "Duration: $($duration.TotalSeconds) seconds" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BuildTearDown, BuildSetup, are not valid commands. TaskSetup and TaskTearDown are commands, but they run before and after each task.
1. Removed "Answer to the original question" callout as requested 2. Removed entire "BuildTearDown for Centralized Reporting" section - BuildSetup and BuildTearDown are not valid psake commands - TaskSetup and TaskTearDown exist but run before/after each task 3. Updated Best Practices Summary to remove BuildTearDown reference 4. Updated Quick Reference table to remove BuildTearDown row 5. Updated See Also links to remove BuildTearDown mention All references to the non-existent BuildSetup/BuildTearDown commands have been removed from the documentation.
This addresses a common question about how to return data from psake scripts, such as artifact URLs or build metadata. The new documentation covers:
The documentation is added to the Tutorial - Advanced section as it covers patterns for working with build outputs and artifacts.