Skip to content

Commit b5ebe75

Browse files
🚀[Feature]: Control if GitHub credentials are persisted (#50)
You now have the option to decide if your GitHub credentials should be kept or automatically disconnected after the script finishes. This gives you more flexibility and helps improve security when needed. ### How It Works * **By default**, your GitHub credentials will be preserved, just like before—no changes needed on your part. * **If you want your credentials to be automatically disconnected after the script runs**, set the new `PreserveCredentials` option to `false`. ### Example Here’s how to disconnect your credentials after running a script: ```yaml - name: Run script with credential cleanup uses: PSModule/GitHub-Script@v1 with: PreserveCredentials: false Script: | Get-GitHubUser # Your credentials will be disconnected after this step ``` That’s it! Use this new option if you want to make sure your GitHub connection is cleaned up automatically after your workflow. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: MariusStorhaug <[email protected]> Co-authored-by: Marius Storhaug <[email protected]>
1 parent 417cef9 commit b5ebe75

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

.github/workflows/TestWorkflow.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,52 @@ jobs:
498498
Get-GitHubGitConfig | Format-Table -AutoSize | Out-String
499499
}
500500
}
501+
502+
ActionTestPreserveCredentialsFalse:
503+
name: PreserveCredentials False
504+
runs-on: ${{ inputs.runs-on }}
505+
steps:
506+
# Need to check out as part of the test, as its a local action
507+
- name: Checkout repo
508+
uses: actions/checkout@v4
509+
510+
- name: Action-Test with PreserveCredentials false
511+
uses: ./
512+
with:
513+
Token: ${{ secrets.TEST_USER_PAT }}
514+
PreserveCredentials: false
515+
Prerelease: ${{ inputs.Prerelease }}
516+
Script: |
517+
LogGroup 'Get-GitHubUser with credentials that will be cleaned up' {
518+
Get-GitHubUser | Format-Table -AutoSize | Out-String
519+
}
520+
521+
- name: Verify credentials are cleaned up
522+
shell: pwsh
523+
run: |
524+
try {
525+
# Import GitHub module to check contexts
526+
Import-Module -Name GitHub -ErrorAction SilentlyContinue
527+
528+
# Check if Get-GitHubContext command is available
529+
if (Get-Command Get-GitHubContext -ErrorAction SilentlyContinue) {
530+
# Get available contexts
531+
$contexts = Get-GitHubContext -ListAvailable
532+
533+
Write-Host "Available GitHub contexts: $($contexts | ConvertTo-Json -Depth 3)"
534+
535+
# Verify that no contexts are available (should be null or empty)
536+
if ($null -eq $contexts -or $contexts.Count -eq 0) {
537+
Write-Host "✅ SUCCESS: No GitHub contexts found after cleanup"
538+
} else {
539+
Write-Host "❌ FAILURE: Found $($contexts.Count) GitHub context(s) after cleanup"
540+
$contexts | Format-Table -AutoSize | Out-String | Write-Host
541+
exit 1
542+
}
543+
} else {
544+
Write-Host "⚠️ WARNING: Get-GitHubContext command not available"
545+
}
546+
} catch {
547+
Write-Host "❌ FAILURE: Error checking GitHub contexts: $($_.Exception.Message)"
548+
exit 1
549+
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ To get started with your own GitHub PowerShell based action, [create a new repos
2424
| `ShowInit` | Show information about the initialization. | false | `'false'` |
2525
| `ShowOutput` | Show the script's output. | false | `'false'` |
2626
| `WorkingDirectory` | The working directory where the script runs. | false | `'.'` |
27+
| `PreserveCredentials` | Preserve credentials after script execution. If false, disconnects GitHub contexts and CLI using Disconnect-GitHubAccount. | false | `'true'` |
2728

2829
### Outputs
2930

@@ -199,3 +200,17 @@ Runs a script that uses the GitHub PowerShell module and outputs the result.
199200
Set-GitHubStepSummary -Summary $result.WISECAT
200201
Write-GitHubNotice -Message $result.Zen -Title 'GitHub Zen'
201202
```
203+
204+
#### Example 6: Run a script with credential cleanup
205+
206+
Runs a script with `PreserveCredentials` set to `false` to automatically disconnect GitHub credentials after execution.
207+
208+
```yaml
209+
- name: Run script with credential cleanup
210+
uses: PSModule/GitHub-Script@v1
211+
with:
212+
PreserveCredentials: false
213+
Script: |
214+
Get-GitHubUser
215+
# Credentials will be disconnected after this step
216+
```

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ inputs:
5858
description: The working directory where the script will run from.
5959
required: false
6060
default: '.'
61+
PreserveCredentials:
62+
description: Preserve credentials after script execution. If false, disconnects GitHub contexts and CLI using Disconnect-GitHubAccount.
63+
required: false
64+
default: 'true'
6165

6266
outputs:
6367
result:
@@ -84,6 +88,7 @@ runs:
8488
PSMODULE_GITHUB_SCRIPT_INPUT_ShowOutput: ${{ inputs.ShowOutput }}
8589
PSMODULE_GITHUB_SCRIPT_INPUT_Prerelease: ${{ inputs.Prerelease }}
8690
PSMODULE_GITHUB_SCRIPT_INPUT_ErrorView: ${{ inputs.ErrorView }}
91+
PSMODULE_GITHUB_SCRIPT_INPUT_PreserveCredentials: ${{ inputs.PreserveCredentials }}
8792
run: |
8893
# ${{ inputs.Name }}
8994
try {

scripts/clean.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
Write-Debug "Cleaning up..."
22
Write-Debug "LASTEXITCODE: $LASTEXITCODE"
33
Write-Debug "PSMODULE_GITHUB_SCRIPT: $env:PSMODULE_GITHUB_SCRIPT"
4+
5+
# Check if credentials should be preserved
6+
$preserveCredentials = $env:PSMODULE_GITHUB_SCRIPT_INPUT_PreserveCredentials -eq 'true'
7+
Write-Debug "PreserveCredentials: $preserveCredentials"
8+
9+
if (-not $preserveCredentials) {
10+
Write-Debug "Disconnecting GitHub contexts and CLI..."
11+
try {
12+
# Import GitHub module if not already imported
13+
if (-not (Get-Module -Name GitHub -ErrorAction SilentlyContinue)) {
14+
Import-Module -Name GitHub -ErrorAction SilentlyContinue
15+
}
16+
17+
# Disconnect GitHub account if the module and function are available
18+
if (Get-Command Disconnect-GitHubAccount -ErrorAction SilentlyContinue) {
19+
Disconnect-GitHubAccount
20+
Write-Debug "Successfully disconnected GitHub account"
21+
} else {
22+
Write-Debug "Disconnect-GitHubAccount command not available"
23+
}
24+
} catch {
25+
Write-Warning "Failed to disconnect GitHub account: $($_.Exception.Message)"
26+
}
27+
}
28+
429
$env:PSMODULE_GITHUB_SCRIPT = $false
530
Write-Debug "PSMODULE_GITHUB_SCRIPT: $env:PSMODULE_GITHUB_SCRIPT"

0 commit comments

Comments
 (0)