Skip to content

Commit e5ec6ee

Browse files
Add preserve-credentials switch with default true
Co-authored-by: MariusStorhaug <[email protected]>
1 parent d2a0f7f commit e5ec6ee

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 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

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)