-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-workflow-manually.ps1
More file actions
62 lines (50 loc) · 1.76 KB
/
run-workflow-manually.ps1
File metadata and controls
62 lines (50 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
param()
# Hardcoded GitHub Personal Access Token (with repo/workflows scope)
$token = $env:GITHUB_TOKEN
# GitHub repo info
$owner = "Ruh-Al-Tarikh"
$repo = "System-Automation-Hub"
# Get list of workflow files
$workflowDir = ".github/workflows"
if (Test-Path $workflowDir) {
$workflows = Get-ChildItem $workflowDir -Filter *.yml | Select-Object -ExpandProperty Name
} else {
$workflows = @()
}
Write-Host "Available workflows to run:"
if ($workflows.Count -eq 0) {
Write-Host "No workflows found in $workflowDir" -ForegroundColor Yellow
return
}
$workflows | ForEach-Object { Write-Host "- $_" }
# Prompt user to choose workflow
$workflowFile = Read-Host "Enter the workflow file to trigger (e.g., ci.yml)"
if (-not ($workflows -contains $workflowFile)) {
Write-Host "Workflow file not found!" -ForegroundColor Red
return
}
# Confirm before dispatch
$answer = Read-Host "Trigger workflow '$workflowFile'? (Y/N)"
if ($answer -ne 'Y') {
Write-Host "Cancelled."
return
}
# GitHub API endpoint
$uri = "https://api.github.com/repos/$owner/$repo/actions/workflows/$workflowFile/dispatches"
# Prepare request headers
$headers = @{
Authorization = "Bearer $token"
"User-Agent" = "PowerShell"
Accept = "application/vnd.github+json"
}
# Body with branch (main) to run workflow on
$body = @{ ref = "main" } | ConvertTo-Json
# Trigger the workflow
try {
Invoke-RestMethod -Uri $uri -Headers $headers -Method Post -Body $body
Write-Host "Workflow '$workflowFile' dispatched successfully!"
Write-Host "Check https://github.com/$owner/$repo/actions for status."
} catch {
Write-Host "Error triggering workflow: $($_.Exception.Message)" -ForegroundColor Red
}