Skip to content

🚀[Feature]: Control if GitHub credentials are persisted #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/TestWorkflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,52 @@ jobs:
Get-GitHubGitConfig | Format-Table -AutoSize | Out-String
}
}

ActionTestPreserveCredentialsFalse:
name: PreserveCredentials False
runs-on: ${{ inputs.runs-on }}
steps:
# Need to check out as part of the test, as its a local action
- name: Checkout repo
uses: actions/checkout@v4

- name: Action-Test with PreserveCredentials false
uses: ./
with:
Token: ${{ secrets.TEST_USER_PAT }}
PreserveCredentials: false
Prerelease: ${{ inputs.Prerelease }}
Script: |
LogGroup 'Get-GitHubUser with credentials that will be cleaned up' {
Get-GitHubUser | Format-Table -AutoSize | Out-String
}

- name: Verify credentials are cleaned up
shell: pwsh
run: |
try {
# Import GitHub module to check contexts
Import-Module -Name GitHub -ErrorAction SilentlyContinue

# Check if Get-GitHubContext command is available
if (Get-Command Get-GitHubContext -ErrorAction SilentlyContinue) {
# Get available contexts
$contexts = Get-GitHubContext -ListAvailable

Write-Host "Available GitHub contexts: $($contexts | ConvertTo-Json -Depth 3)"

# Verify that no contexts are available (should be null or empty)
if ($null -eq $contexts -or $contexts.Count -eq 0) {
Write-Host "✅ SUCCESS: No GitHub contexts found after cleanup"
} else {
Write-Host "❌ FAILURE: Found $($contexts.Count) GitHub context(s) after cleanup"
$contexts | Format-Table -AutoSize | Out-String | Write-Host
exit 1
}
} else {
Write-Host "⚠️ WARNING: Get-GitHubContext command not available"
}
} catch {
Write-Host "❌ FAILURE: Error checking GitHub contexts: $($_.Exception.Message)"
exit 1
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ To get started with your own GitHub PowerShell based action, [create a new repos
| `ShowInit` | Show information about the initialization. | false | `'false'` |
| `ShowOutput` | Show the script's output. | false | `'false'` |
| `WorkingDirectory` | The working directory where the script runs. | false | `'.'` |
| `PreserveCredentials` | Preserve credentials after script execution. If false, disconnects GitHub contexts and CLI using Disconnect-GitHubAccount. | false | `'true'` |

### Outputs

Expand Down Expand Up @@ -199,3 +200,17 @@ Runs a script that uses the GitHub PowerShell module and outputs the result.
Set-GitHubStepSummary -Summary $result.WISECAT
Write-GitHubNotice -Message $result.Zen -Title 'GitHub Zen'
```

#### Example 6: Run a script with credential cleanup

Runs a script with `PreserveCredentials` set to `false` to automatically disconnect GitHub credentials after execution.

```yaml
- name: Run script with credential cleanup
uses: PSModule/GitHub-Script@v1
with:
PreserveCredentials: false
Script: |
Get-GitHubUser
# Credentials will be disconnected after this step
```
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ inputs:
description: The working directory where the script will run from.
required: false
default: '.'
PreserveCredentials:
description: Preserve credentials after script execution. If false, disconnects GitHub contexts and CLI using Disconnect-GitHubAccount.
required: false
default: 'true'

outputs:
result:
Expand All @@ -84,6 +88,7 @@ runs:
PSMODULE_GITHUB_SCRIPT_INPUT_ShowOutput: ${{ inputs.ShowOutput }}
PSMODULE_GITHUB_SCRIPT_INPUT_Prerelease: ${{ inputs.Prerelease }}
PSMODULE_GITHUB_SCRIPT_INPUT_ErrorView: ${{ inputs.ErrorView }}
PSMODULE_GITHUB_SCRIPT_INPUT_PreserveCredentials: ${{ inputs.PreserveCredentials }}
run: |
# ${{ inputs.Name }}
try {
Expand Down
25 changes: 25 additions & 0 deletions scripts/clean.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
Write-Debug "Cleaning up..."
Write-Debug "LASTEXITCODE: $LASTEXITCODE"
Write-Debug "PSMODULE_GITHUB_SCRIPT: $env:PSMODULE_GITHUB_SCRIPT"

# Check if credentials should be preserved
$preserveCredentials = $env:PSMODULE_GITHUB_SCRIPT_INPUT_PreserveCredentials -eq 'true'
Write-Debug "PreserveCredentials: $preserveCredentials"

if (-not $preserveCredentials) {
Write-Debug "Disconnecting GitHub contexts and CLI..."
try {
# Import GitHub module if not already imported
if (-not (Get-Module -Name GitHub -ErrorAction SilentlyContinue)) {
Import-Module -Name GitHub -ErrorAction SilentlyContinue
}

# Disconnect GitHub account if the module and function are available
if (Get-Command Disconnect-GitHubAccount -ErrorAction SilentlyContinue) {
Disconnect-GitHubAccount
Write-Debug "Successfully disconnected GitHub account"
} else {
Write-Debug "Disconnect-GitHubAccount command not available"
}
} catch {
Write-Warning "Failed to disconnect GitHub account: $($_.Exception.Message)"
}
}

$env:PSMODULE_GITHUB_SCRIPT = $false
Write-Debug "PSMODULE_GITHUB_SCRIPT: $env:PSMODULE_GITHUB_SCRIPT"
Loading