From 1e4938326bfaa21ad67077deb8e1b5c2f651ba4b Mon Sep 17 00:00:00 2001 From: MoChilia Date: Tue, 16 Jan 2024 17:51:00 +0800 Subject: [PATCH 1/2] fix #403 --- src/Cli/AzureCliLogin.ts | 3 +++ src/PowerShell/AzPSUtils.ts | 3 +++ src/common/Utils.ts | 6 ++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Cli/AzureCliLogin.ts b/src/Cli/AzureCliLogin.ts index ec557fe62..fc67fbffc 100644 --- a/src/Cli/AzureCliLogin.ts +++ b/src/Cli/AzureCliLogin.ts @@ -17,6 +17,9 @@ export class AzureCliLogin { async login() { core.info(`Running Azure CLI Login.`); this.azPath = await io.which("az", true); + if (!this.azPath) { + throw new Error("Azure CLI is not found in the runner."); + } core.debug(`Azure CLI path: ${this.azPath}`); let output: string = ""; diff --git a/src/PowerShell/AzPSUtils.ts b/src/PowerShell/AzPSUtils.ts index 4b287cfd7..b9f378849 100644 --- a/src/PowerShell/AzPSUtils.ts +++ b/src/PowerShell/AzPSUtils.ts @@ -71,6 +71,9 @@ export class AzPSUtils { }; let psPath: string = await io.which(AzPSConstants.PowerShell_CmdName, true); + if (!psPath) { + throw new Error("PowerShell is not found in the runner."); + } await exec.exec(`"${psPath}"`, ["-Command", psScript], options) if (commandStdErr) { throw new Error('Azure PowerShell login failed with errors.'); diff --git a/src/common/Utils.ts b/src/common/Utils.ts index 9a2734be8..adb96d3b4 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -14,7 +14,8 @@ export function setUserAgent(): void { export async function cleanupAzCLIAccounts(): Promise { let azPath = await io.which("az", true); if (!azPath) { - throw new Error("Azure CLI is not found in the runner."); + core.warning("Azure CLI is not found in the runner."); + return; } core.debug(`Azure CLI path: ${azPath}`); core.info("Clearing azure cli accounts from the local cache."); @@ -24,7 +25,8 @@ export async function cleanupAzCLIAccounts(): Promise { export async function cleanupAzPSAccounts(): Promise { let psPath: string = await io.which(AzPSConstants.PowerShell_CmdName, true); if (!psPath) { - throw new Error("PowerShell is not found in the runner."); + core.warning("PowerShell is not found in the runner."); + return; } core.debug(`PowerShell path: ${psPath}`); core.debug("Importing Azure PowerShell module."); From 345643a937ea99209b6830e6a7f3ec18f6063948 Mon Sep 17 00:00:00 2001 From: MoChilia Date: Wed, 17 Jan 2024 14:46:27 +0800 Subject: [PATCH 2/2] modify error handling --- src/Cli/AzureCliLogin.ts | 3 --- src/PowerShell/AzPSUtils.ts | 3 --- src/cleanup.ts | 2 +- src/common/Utils.ts | 8 -------- 4 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/Cli/AzureCliLogin.ts b/src/Cli/AzureCliLogin.ts index fc67fbffc..ec557fe62 100644 --- a/src/Cli/AzureCliLogin.ts +++ b/src/Cli/AzureCliLogin.ts @@ -17,9 +17,6 @@ export class AzureCliLogin { async login() { core.info(`Running Azure CLI Login.`); this.azPath = await io.which("az", true); - if (!this.azPath) { - throw new Error("Azure CLI is not found in the runner."); - } core.debug(`Azure CLI path: ${this.azPath}`); let output: string = ""; diff --git a/src/PowerShell/AzPSUtils.ts b/src/PowerShell/AzPSUtils.ts index b9f378849..4b287cfd7 100644 --- a/src/PowerShell/AzPSUtils.ts +++ b/src/PowerShell/AzPSUtils.ts @@ -71,9 +71,6 @@ export class AzPSUtils { }; let psPath: string = await io.which(AzPSConstants.PowerShell_CmdName, true); - if (!psPath) { - throw new Error("PowerShell is not found in the runner."); - } await exec.exec(`"${psPath}"`, ["-Command", psScript], options) if (commandStdErr) { throw new Error('Azure PowerShell login failed with errors.'); diff --git a/src/cleanup.ts b/src/cleanup.ts index d31822633..d307920d2 100644 --- a/src/cleanup.ts +++ b/src/cleanup.ts @@ -10,7 +10,7 @@ async function cleanup() { } } catch (error) { - core.setFailed(`Login cleanup 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.`); + core.warning(`Login cleanup failed with ${error}. Cleanup will be skipped.`); core.debug(error.stack); } } diff --git a/src/common/Utils.ts b/src/common/Utils.ts index adb96d3b4..b8a089d2c 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -13,10 +13,6 @@ export function setUserAgent(): void { export async function cleanupAzCLIAccounts(): Promise { let azPath = await io.which("az", true); - if (!azPath) { - core.warning("Azure CLI is not found in the runner."); - return; - } core.debug(`Azure CLI path: ${azPath}`); core.info("Clearing azure cli accounts from the local cache."); await exec.exec(`"${azPath}"`, ["account", "clear"]); @@ -24,10 +20,6 @@ export async function cleanupAzCLIAccounts(): Promise { export async function cleanupAzPSAccounts(): Promise { let psPath: string = await io.which(AzPSConstants.PowerShell_CmdName, true); - if (!psPath) { - core.warning("PowerShell is not found in the runner."); - return; - } core.debug(`PowerShell path: ${psPath}`); core.debug("Importing Azure PowerShell module."); AzPSUtils.setPSModulePathForGitHubRunner();