forked from Azure/bicep-registry-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Moved GitHub pipeline state/toggle action to script to enable l…
…ocal testing & execution (Azure#698) ## Description Moved GitHub pipeline state/toggle action to script to enable local testing & execution Examples: - [Running enable on all](https://github.com/AlexanderSehr/bicep-registry-modules/actions/runs/7088913136/job/19292448089) - [Running disable for only network](https://github.com/AlexanderSehr/bicep-registry-modules/actions/runs/7088929214/job/19292497453) --------- Co-authored-by: Erika Gressi <[email protected]>
- Loading branch information
1 parent
f2230a8
commit 272047f
Showing
2 changed files
with
115 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<# | ||
.SYNOPSIS | ||
Toggle the state of GitHub workflows in a given repository to either being enabled or disabled. | ||
.DESCRIPTION | ||
Toggle the state of GitHub workflows in a given repository to either being enabled or disabled. | ||
.PARAMETER TargetState | ||
Mandatory. The state to set the workflows to. Must be either 'enable' or 'disable'. | ||
.PARAMETER RepositoryOwner | ||
Mandatory. The owning organization of the repository. For example, 'MyOrg'. | ||
.PARAMETER RepositoryName | ||
Optional. The name of the repository. Defaults to 'bicep-registry-modules'. | ||
.PARAMETER IncludePattern | ||
Optional. A regex pattern to match against the workflow names. Defaults to 'avm\.(?:res|ptn)' - avm.res & avm.ptn. | ||
.PARAMETER ExlcudePattern | ||
Optional. A regex pattern that should not match against the workflow names. Defaults to '^$' - empty. | ||
.PARAMETER GitHubToken | ||
Optional. The GitHub PAT token to use for authentication when interacting with GitHub. If not provided, the PAT must be available in the environment variable 'GH_TOKEN' | ||
.EXAMPLE | ||
Switch-WorkflowSate -RepositoryOwner 'Paul' -RepositoryName 'bicep-registry-modules' -TargetState 'enable' -GitHubToken ('iAmAToken' | ConvertTo-SecureString -AsPlainText -Force) | ||
Enable any AVM res/ptn workflow in the [Paul/bicep-registry-modules] repository that is not in state 'active' using a custom GitHub PAT token. | ||
.EXAMPLE | ||
Switch-WorkflowSate -RepositoryOwner 'Paul' -RepositoryName 'bicep-registry-modules' -TargetState 'disable' | ||
Disable any workflow in the [Paul/bicep-registry-modules] repository that has the state 'active', assuming you have a GitHub PAT token 'GH_TOKEN' set in your environment. | ||
.EXAMPLE | ||
Switch-WorkflowSate -RepositoryOwner 'Paul' -RepositoryName 'bicep-registry-modules' -TargetState 'disable' -IncludePattern 'avm\.res\.network\.' | ||
Disable any workflow with a naming matching [avm.res.network*] in the [Paul/bicep-registry-modules] repository that has the state 'active', assuming you have a GitHub PAT token 'GH_TOKEN' set in your environment. | ||
.EXAMPLE | ||
Switch-WorkflowSate -RepositoryOwner 'Paul' -RepositoryName 'bicep-registry-modules' -TargetState 'disable' -IncludePattern 'avm\.res\.network' -ExlcudePattern 'avm\.res\.network\.virtual-network' | ||
Disable any workflow with a naming matching [avm.res.network*] but exluding those that match the name [avm.res.network.virtual-network] in the [Paul/bicep-registry-modules] repository that has the state 'active', assuming you have a GitHub PAT token 'GH_TOKEN' set in your environment. | ||
#> | ||
function Switch-WorkflowSate { | ||
|
||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateSet('enable', 'disable')] | ||
[string] $TargetState, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[string] $RepositoryOwner, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $RepositoryName = 'bicep-registry-modules', | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $IncludePattern = 'avm\.(?:res|ptn)', | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $ExlcudePattern = '^$', | ||
|
||
[Parameter(Mandatory = $false)] | ||
[secureString] $GitHubToken | ||
) | ||
|
||
if (-not [String]::IsNullOrEmpty($GitHubToken)) { | ||
$env:GH_TOKEN = $GitHubToken | ConvertFrom-SecureString -AsPlainText | ||
} | ||
|
||
$repo = "$RepositoryOwner/$RepositoryName" | ||
|
||
if ($repo -eq 'Azure/bicep-registry-modules') { | ||
throw 'Function should not run for [Azure/bicep-registry-modules].' | ||
} | ||
|
||
$workflows = gh workflow list --repo $repo --all --json "name,state,id" | ConvertFrom-Json -Depth 100 | ||
$relevantWorkflows = $workflows | Where-Object { | ||
$_.name -match $IncludePattern -and $_.name -notmatch $ExlcudePattern | ||
} | ||
|
||
if ($TargetState -eq 'disable') { | ||
foreach ($workflow in ($relevantWorkflows | Where-Object { $_.state -eq 'active' })) { | ||
gh workflow $TargetState $workflow.id --repo $repo | ||
Write-Verbose ('- Disabled workflow [{0}]' -f $workflow.name) | ||
} | ||
} else { | ||
foreach ($workflow in ($relevantWorkflows | Where-Object { $_.state -ne 'active' })) { | ||
gh workflow $TargetState $workflow.id --repo $repo | ||
Write-Verbose ('+ Enabled workflow [{0}]' -f $workflow.name) | ||
} | ||
} | ||
} |