diff --git a/src/Cli/AzureCliLogin.ts b/src/Cli/AzureCliLogin.ts index 91d52278a..3c88d3444 100644 --- a/src/Cli/AzureCliLogin.ts +++ b/src/Cli/AzureCliLogin.ts @@ -2,7 +2,6 @@ import * as exec from '@actions/exec'; import { LoginConfig } from "../common/LoginConfig"; import { ExecOptions } from '@actions/exec/lib/interfaces'; import * as core from '@actions/core'; -import * as io from '@actions/io'; export class AzureCliLogin { loginConfig: LoginConfig; @@ -16,11 +15,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 = ""; const execOptions: any = { diff --git a/src/cleanup.ts b/src/cleanup.ts new file mode 100644 index 000000000..86c6d5c54 --- /dev/null +++ b/src/cleanup.ts @@ -0,0 +1,15 @@ +import * as core from '@actions/core'; +import * as exec from '@actions/exec'; + +async function cleanup() { + try { + core.info("Cleaning subscriptions from the local cache.") + await exec.exec("az", ["account", "clear"]); + } + catch (error) { + core.setFailed(`Login cleanup failed with ${error}.`); + core.debug(error.stack); + } +} + +cleanup(); \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 95f2d6a45..605bfa9b0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,45 +1,45 @@ -import * as core from '@actions/core'; -import { AzPSLogin } from './PowerShell/AzPSLogin'; -import { LoginConfig } from './common/LoginConfig'; -import { AzureCliLogin } from './Cli/AzureCliLogin'; - -var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : ""; -var azPSHostEnv = !!process.env.AZUREPS_HOST_ENVIRONMENT ? `${process.env.AZUREPS_HOST_ENVIRONMENT}` : ""; - -async function main() { - try { - let usrAgentRepo = `${process.env.GITHUB_REPOSITORY}`; - let actionName = 'AzureLogin'; - let userAgentString = (!!prefix ? `${prefix}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`; - let azurePSHostEnv = (!!azPSHostEnv ? `${azPSHostEnv}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`; - core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString); - core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv); - - // prepare the login configuration - var loginConfig = new LoginConfig(); - await loginConfig.initialize(); - await loginConfig.validate(); - - // login to Azure CLI - var cliLogin = new AzureCliLogin(loginConfig); - await cliLogin.login(); - - //login to Azure PowerShell - if (loginConfig.enableAzPSSession) { - var psLogin: AzPSLogin = new AzPSLogin(loginConfig); - await psLogin.login(); - } - } - catch (error) { - core.setFailed(`Login 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. Double check if the 'auth-type' is correct. Refer to https://github.com/Azure/login#readme for more information.`); - core.debug(error.stack); - } - finally { - // Reset AZURE_HTTP_USER_AGENT - core.exportVariable('AZURE_HTTP_USER_AGENT', prefix); - core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv); - } -} - -main(); - +import * as core from '@actions/core'; +import { AzPSLogin } from './PowerShell/AzPSLogin'; +import { LoginConfig } from './common/LoginConfig'; +import { AzureCliLogin } from './Cli/AzureCliLogin'; + +var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : ""; +var azPSHostEnv = !!process.env.AZUREPS_HOST_ENVIRONMENT ? `${process.env.AZUREPS_HOST_ENVIRONMENT}` : ""; + +async function main() { + try { + let usrAgentRepo = `${process.env.GITHUB_REPOSITORY}`; + let actionName = 'AzureLogin'; + let userAgentString = (!!prefix ? `${prefix}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`; + let azurePSHostEnv = (!!azPSHostEnv ? `${azPSHostEnv}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`; + core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString); + core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv); + + // prepare the login configuration + var loginConfig = new LoginConfig(); + await loginConfig.initialize(); + await loginConfig.validate(); + + // login to Azure CLI + var cliLogin = new AzureCliLogin(loginConfig); + await cliLogin.login(); + + //login to Azure PowerShell + if (loginConfig.enableAzPSSession) { + var psLogin: AzPSLogin = new AzPSLogin(loginConfig); + await psLogin.login(); + } + } + catch (error) { + core.setFailed(`Login failed with ${error}. If 'enable-AzPSSession' is true, make sure 'pwsh' is installed on the runner together with Azure PowerShell module. Double check if the 'auth-type' is correct. Refer to https://github.com/Azure/login#readme for more information.`); + core.debug(error.stack); + } + finally { + // Reset AZURE_HTTP_USER_AGENT + core.exportVariable('AZURE_HTTP_USER_AGENT', prefix); + core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv); + } +} + +main(); + diff --git a/src/setup.ts b/src/setup.ts new file mode 100644 index 000000000..6b7f3f4f3 --- /dev/null +++ b/src/setup.ts @@ -0,0 +1,22 @@ +import * as core from '@actions/core'; +import * as exec from '@actions/exec'; +import * as io from '@actions/io'; + +async function setup() { + try { + 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}`); + + core.info("Cleaning subscriptions from the local cache.") + await exec.exec("az", ["account", "clear"]); + } + catch (error) { + core.setFailed(`Login setup failed with ${error}. Make sure 'az' is installed on the runner.`); + core.debug(error.stack); + } +} + +setup(); \ No newline at end of file