Skip to content

Commit

Permalink
feat: Moved GitHub pipeline state/toggle action to script to enable l…
Browse files Browse the repository at this point in the history
…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
AlexanderSehr and eriqua authored Dec 4, 2023
1 parent f2230a8 commit 272047f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 16 deletions.
35 changes: 19 additions & 16 deletions .github/workflows/avm.platform.toggle-avm-workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ name: "avm.platform.toggle-avm-workflows"
on:
workflow_dispatch:
inputs:
workMode:
targetState:
type: choice
description: "Enable or disable workflows"
required: true
options:
- "enable"
- "disable"
default: "disable"
- "Enable"
- "Disable"
default: "Disable"
includePattern:
type: string
description: "RegEx which workflows are included"
Expand All @@ -33,19 +33,22 @@ jobs:
fetch-depth: 0
- env:
GH_TOKEN: ${{ github.token }}
name: ${{ inputs.workMode }} AVM workflows
name: ${{ inputs.targetState }} AVM workflows
shell: pwsh
run: |
$repo = "${{ github.repository_owner }}/${{ github.event.repository.name }}"
$workflows = gh workflow list --repo $repo --all --json "name,state,id" | ConvertFrom-Json -Depth 100
$relevantWorkflows = $workflows | Where-Object {
$_.name -match "${{ inputs.includePattern }}" -and $_.name -notmatch "${{ inputs.excludePattern }}"
}
# Load used functions
. (Join-Path $env:GITHUB_WORKSPACE 'avm' 'utilities' 'pipelines' 'platform' 'Switch-WorkflowSate.ps1')
foreach ($workflow in $relevantWorkflows) {
if (("${{ inputs.workMode }}" -eq "disable" -and $workflow.state -eq "active") -or ("${{ inputs.workMode }}" -eq "enable" -and $workflow.state -ne "active"))
{
Write-Verbose "${{ inputs.workMode }} $($workflow.name)" -Verbose
gh workflow ${{ inputs.workMode }} $workflow.id --repo $repo
}
$functionInput = @{
RepositoryOwner = '${{ github.repository_owner }}'
RepositoryName = '${{ github.event.repository.name }}'
TargetState = '${{ inputs.targetState }}'.ToLower()
IncludePattern = '${{ inputs.includePattern }}'
ExlcudePattern = '${{ inputs.excludePattern }}'
}
Write-Verbose "Invoke function with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
# Get the modified child resources
Switch-WorkflowSate @functionInput -Verbose
96 changes: 96 additions & 0 deletions avm/utilities/pipelines/platform/Switch-WorkflowSate.ps1
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)
}
}
}

0 comments on commit 272047f

Please sign in to comment.