|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + Trigger GitHub Actions workflows using GitHub CLI |
| 5 | + |
| 6 | +.DESCRIPTION |
| 7 | + Reproducible script to trigger all GitHub Actions workflows for the CS506 project. |
| 8 | + Automatically detects repository location and triggers workflows. |
| 9 | + |
| 10 | +.PARAMETER WorkflowName |
| 11 | + Optional: Name of specific workflow to trigger. If not specified, all workflows are triggered. |
| 12 | + |
| 13 | +.PARAMETER ListOnly |
| 14 | + If specified, only list available workflows without triggering them. |
| 15 | + |
| 16 | +.EXAMPLE |
| 17 | + .\trigger_workflows.ps1 |
| 18 | + Triggers all workflows |
| 19 | + |
| 20 | +.EXAMPLE |
| 21 | + .\trigger_workflows.ps1 -ListOnly |
| 22 | + Lists all available workflows |
| 23 | + |
| 24 | +.EXAMPLE |
| 25 | + .\trigger_workflows.ps1 -WorkflowName "Linear Regression Test" |
| 26 | + Triggers only the Linear Regression Test workflow |
| 27 | +#> |
| 28 | + |
| 29 | +param( |
| 30 | + [Parameter(Mandatory=$false)] |
| 31 | + [string]$WorkflowName, |
| 32 | + |
| 33 | + [Parameter(Mandatory=$false)] |
| 34 | + [switch]$ListOnly |
| 35 | +) |
| 36 | + |
| 37 | +# Colors for output |
| 38 | +function Write-Success { param($msg) Write-Host "✅ $msg" -ForegroundColor Green } |
| 39 | +function Write-Error { param($msg) Write-Host "❌ $msg" -ForegroundColor Red } |
| 40 | +function Write-Info { param($msg) Write-Host "ℹ️ $msg" -ForegroundColor Cyan } |
| 41 | +function Write-Warning { param($msg) Write-Host "⚠️ $msg" -ForegroundColor Yellow } |
| 42 | + |
| 43 | +# Check prerequisites |
| 44 | +Write-Info "Checking prerequisites..." |
| 45 | + |
| 46 | +# Check if GitHub CLI is installed |
| 47 | +try { |
| 48 | + $ghVersion = gh --version 2>&1 | Select-Object -First 1 |
| 49 | + Write-Success "GitHub CLI installed: $ghVersion" |
| 50 | +} catch { |
| 51 | + Write-Error "GitHub CLI (gh) not found. Install from: https://cli.github.com/" |
| 52 | + Write-Info "Windows: winget install GitHub.cli" |
| 53 | + exit 1 |
| 54 | +} |
| 55 | + |
| 56 | +# Check if authenticated |
| 57 | +try { |
| 58 | + $authStatus = gh auth status 2>&1 |
| 59 | + if ($LASTEXITCODE -ne 0) { |
| 60 | + Write-Error "Not authenticated with GitHub CLI" |
| 61 | + Write-Info "Run: gh auth login" |
| 62 | + exit 1 |
| 63 | + } |
| 64 | + Write-Success "Authenticated with GitHub CLI" |
| 65 | +} catch { |
| 66 | + Write-Error "Failed to check authentication status" |
| 67 | + exit 1 |
| 68 | +} |
| 69 | + |
| 70 | +# Auto-detect repository root (go up from script directory until we find .git) |
| 71 | +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 72 | +$currentDir = $scriptDir |
| 73 | +$repoRoot = $null |
| 74 | + |
| 75 | +while ($currentDir) { |
| 76 | + if (Test-Path (Join-Path $currentDir ".git")) { |
| 77 | + $repoRoot = $currentDir |
| 78 | + break |
| 79 | + } |
| 80 | + $parent = Split-Path -Parent $currentDir |
| 81 | + if ($parent -eq $currentDir) { break } # Reached root |
| 82 | + $currentDir = $parent |
| 83 | +} |
| 84 | + |
| 85 | +if (-not $repoRoot) { |
| 86 | + Write-Error "Could not find Git repository root" |
| 87 | + exit 1 |
| 88 | +} |
| 89 | + |
| 90 | +Write-Success "Repository root: $repoRoot" |
| 91 | + |
| 92 | +# Change to repository root |
| 93 | +Push-Location $repoRoot |
| 94 | + |
| 95 | +try { |
| 96 | + # Get repository information |
| 97 | + Write-Info "Getting repository information..." |
| 98 | + $repoInfo = gh repo view --json nameWithOwner,defaultBranchRef | ConvertFrom-Json |
| 99 | + $repoName = $repoInfo.nameWithOwner |
| 100 | + $defaultBranch = $repoInfo.defaultBranchRef.name |
| 101 | + |
| 102 | + Write-Success "Repository: $repoName" |
| 103 | + Write-Success "Default branch: $defaultBranch" |
| 104 | + |
| 105 | + # List workflows |
| 106 | + Write-Info "Fetching available workflows..." |
| 107 | + $workflows = gh workflow list --json name,id,path,state | ConvertFrom-Json |
| 108 | + |
| 109 | + if ($workflows.Count -eq 0) { |
| 110 | + Write-Warning "No workflows found in repository" |
| 111 | + exit 0 |
| 112 | + } |
| 113 | + |
| 114 | + Write-Success "Found $($workflows.Count) workflow(s):" |
| 115 | + $workflows | ForEach-Object { |
| 116 | + $status = if ($_.state -eq 'active') { '✅ active' } else { '⚠️ ' + $_.state } |
| 117 | + Write-Host " - $($_.name) ($status)" |
| 118 | + Write-Host " Path: $($_.path)" -ForegroundColor Gray |
| 119 | + } |
| 120 | + |
| 121 | + if ($ListOnly) { |
| 122 | + Write-Info "List-only mode. Exiting." |
| 123 | + exit 0 |
| 124 | + } |
| 125 | + |
| 126 | + # Filter workflows if specific name provided |
| 127 | + $workflowsToTrigger = if ($WorkflowName) { |
| 128 | + $filtered = $workflows | Where-Object { $_.name -like "*$WorkflowName*" } |
| 129 | + if ($filtered.Count -eq 0) { |
| 130 | + Write-Error "No workflow found matching: $WorkflowName" |
| 131 | + exit 1 |
| 132 | + } |
| 133 | + $filtered |
| 134 | + } else { |
| 135 | + $workflows |
| 136 | + } |
| 137 | + |
| 138 | + Write-Host "`n" + ("="*70) |
| 139 | + Write-Host "TRIGGERING WORKFLOWS" |
| 140 | + Write-Host ("="*70) |
| 141 | + |
| 142 | + $triggered = @() |
| 143 | + foreach ($workflow in $workflowsToTrigger) { |
| 144 | + if ($workflow.state -ne 'active') { |
| 145 | + Write-Warning "Skipping inactive workflow: $($workflow.name)" |
| 146 | + continue |
| 147 | + } |
| 148 | + |
| 149 | + Write-Info "Triggering: $($workflow.name)" |
| 150 | + try { |
| 151 | + # Trigger the workflow using workflow_dispatch |
| 152 | + gh workflow run $workflow.id --ref $defaultBranch 2>&1 | Out-Null |
| 153 | + |
| 154 | + if ($LASTEXITCODE -eq 0) { |
| 155 | + Write-Success "Triggered: $($workflow.name)" |
| 156 | + $triggered += $workflow |
| 157 | + Start-Sleep -Seconds 2 # Brief delay between triggers |
| 158 | + } else { |
| 159 | + Write-Error "Failed to trigger: $($workflow.name)" |
| 160 | + } |
| 161 | + } catch { |
| 162 | + Write-Error "Error triggering $($workflow.name): $_" |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if ($triggered.Count -eq 0) { |
| 167 | + Write-Warning "No workflows were triggered" |
| 168 | + exit 0 |
| 169 | + } |
| 170 | + |
| 171 | + Write-Host "`n" + ("="*70) |
| 172 | + Write-Host "MONITORING WORKFLOW RUNS" |
| 173 | + Write-Host ("="*70) |
| 174 | + |
| 175 | + Write-Info "Waiting for workflow runs to appear (5 seconds)..." |
| 176 | + Start-Sleep -Seconds 5 |
| 177 | + |
| 178 | + # Get recent runs |
| 179 | + Write-Info "Fetching recent workflow runs..." |
| 180 | + $runs = gh run list --limit 10 --json databaseId,name,status,conclusion,createdAt,htmlUrl,workflowName | ConvertFrom-Json |
| 181 | + |
| 182 | + if ($runs.Count -eq 0) { |
| 183 | + Write-Warning "No recent runs found. Workflows may still be queuing." |
| 184 | + Write-Info "Check status at: https://github.com/$repoName/actions" |
| 185 | + exit 0 |
| 186 | + } |
| 187 | + |
| 188 | + # Display recent runs |
| 189 | + Write-Success "Recent workflow runs:" |
| 190 | + $runs | Select-Object -First $triggered.Count | ForEach-Object { |
| 191 | + $statusIcon = switch ($_.status) { |
| 192 | + 'completed' { |
| 193 | + switch ($_.conclusion) { |
| 194 | + 'success' { '✅' } |
| 195 | + 'failure' { '❌' } |
| 196 | + 'cancelled' { '⚠️' } |
| 197 | + default { '❓' } |
| 198 | + } |
| 199 | + } |
| 200 | + 'in_progress' { '🔄' } |
| 201 | + 'queued' { '⏳' } |
| 202 | + default { '❓' } |
| 203 | + } |
| 204 | + |
| 205 | + $statusText = if ($_.status -eq 'completed') { $_.conclusion } else { $_.status } |
| 206 | + Write-Host " $statusIcon $($_.workflowName) - $statusText" |
| 207 | + Write-Host " URL: $($_.htmlUrl)" -ForegroundColor Gray |
| 208 | + } |
| 209 | + |
| 210 | + Write-Host "`n" + ("="*70) |
| 211 | + Write-Host "SUMMARY" |
| 212 | + Write-Host ("="*70) |
| 213 | + Write-Success "Triggered $($triggered.Count) workflow(s)" |
| 214 | + Write-Info "Monitor progress at: https://github.com/$repoName/actions" |
| 215 | + |
| 216 | + # Provide commands for monitoring |
| 217 | + Write-Host "`nUseful commands:" |
| 218 | + Write-Host " gh run list # List recent runs" -ForegroundColor Gray |
| 219 | + Write-Host " gh run list --limit 20 # List more runs" -ForegroundColor Gray |
| 220 | + Write-Host " gh run watch # Watch latest run" -ForegroundColor Gray |
| 221 | + Write-Host " gh run view <run_id> # View specific run details" -ForegroundColor Gray |
| 222 | + |
| 223 | +} finally { |
| 224 | + Pop-Location |
| 225 | +} |
0 commit comments