Skip to content

Commit

Permalink
devide cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MoChilia committed Dec 28, 2023
1 parent 166904c commit 2ae179b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as core from '@actions/core';
import { setUserAgent } from './common/Utils';
import { cleanupAccounts } from './common/Utils';
import { cleanupAzCLIAccounts, cleanupAzPSAccounts } from './common/Utils';

async function cleanup() {
try {
setUserAgent();
cleanupAccounts();
await cleanupAzCLIAccounts();
if(core.getInput('enable-AzPSSession').toLowerCase() === "true"){
await cleanupAzPSAccounts();
}
}
catch (error) {
core.setFailed(`Login cleanup failed with ${error}.`);
Expand Down
29 changes: 15 additions & 14 deletions src/common/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@ export function setUserAgent(): void {
process.env.AZUREPS_HOST_ENVIRONMENT = (!!process.env.AZUREPS_HOST_ENVIRONMENT ? `${process.env.AZUREPS_HOST_ENVIRONMENT} ` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`;
}

export async function cleanupAccounts(): Promise<void> {
export async function cleanupAzCLIAccounts(): Promise<void> {
let azPath = await io.which("az", true);
if (!azPath) {
throw new Error("Azure CLI is not found in the runner.");
}
core.debug(`Azure CLI path: ${azPath}`);
core.info("Clearing azure cli accounts from the local cache.");
await exec.exec(`"${azPath}"`, ["account", "clear"]);

}

if(core.getInput('enable-AzPSSession').toLowerCase() === "true"){
let psPath: string = await io.which(AzPSConstants.PowerShell_CmdName, true);
if (!psPath) {
throw new Error("PowerShell is not found in the runner.");
}
core.debug(`PowerShell path: ${psPath}`);
core.debug("Importing Azure PowerShell module.");
setPSModulePathForGitHubRunner();
await importLatestAzAccounts();
core.info("Clearing azure powershell accounts from the local cache.");
await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "Process"]);
await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "CurrentUser", "-Force", "-ErrorAction", "SilentlyContinue"]);
export async function cleanupAzPSAccounts(): Promise<void> {
let psPath: string = await io.which(AzPSConstants.PowerShell_CmdName, true);
if (!psPath) {
throw new Error("PowerShell is not found in the runner.");
}
}
core.debug(`PowerShell path: ${psPath}`);
core.debug("Importing Azure PowerShell module.");
setPSModulePathForGitHubRunner();
await importLatestAzAccounts();
core.info("Clearing azure powershell accounts from the local cache.");
await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "Process"]);
await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "CurrentUser", "-Force", "-ErrorAction", "SilentlyContinue"]);
}
7 changes: 5 additions & 2 deletions src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as core from '@actions/core';
import { setUserAgent } from './common/Utils';
import { cleanupAccounts } from './common/Utils';
import { cleanupAzCLIAccounts, cleanupAzPSAccounts } from './common/Utils';

async function setup() {
try {
setUserAgent();
cleanupAccounts();
await cleanupAzCLIAccounts();
if(core.getInput('enable-AzPSSession').toLowerCase() === "true"){
await cleanupAzPSAccounts();
}
}
catch (error) {
core.setFailed(`Login setup failed with ${error}. Make sure 'az' is installed on the runner. If 'enable-AzPSSession' is true, make sure 'pwsh' is installed on the runner together with Azure PowerShell module.`);
Expand Down

0 comments on commit 2ae179b

Please sign in to comment.