Skip to content

Conversation

@HeyItsGilbert
Copy link
Member

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.

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.
@netlify
Copy link

netlify bot commented Nov 25, 2025

Deploy Preview for psake ready!

Name Link
🔨 Latest commit d6d7069
🔍 Latest deploy log https://app.netlify.com/projects/psake/deploys/692679d03d0f790008f5156d
😎 Deploy Preview https://deploy-preview-29--psake.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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.
Copy link
Member Author

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.

Comment on lines 215 to 255
### 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"
Copy link
Member Author

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.
@HeyItsGilbert HeyItsGilbert merged commit 5afb0ea into main Nov 26, 2025
8 checks passed
@HeyItsGilbert HeyItsGilbert deleted the claude/psake-object-return-016fDSLBaTGgtru8wmHKu6Ap branch November 26, 2025 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants