Skip to content

Commit

Permalink
extract azpsconfig class
Browse files Browse the repository at this point in the history
  • Loading branch information
MoChilia committed Dec 28, 2023
1 parent 2ae179b commit 1bb2dae
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 65 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ branding:
color: 'blue'
runs:
using: 'node16'
pre: 'lib/setup.js'
pre: 'lib/cleanupjs'
main: 'lib/main.js'
post: 'lib/cleanup.js'
40 changes: 40 additions & 0 deletions src/PowerShell/AzPSConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as core from '@actions/core';
import * as os from 'os';
import * as path from 'path';
import AzPSConstants from './AzPSConstants';
import AzPSScriptBuilder from './AzPSScriptBuilder';
import { AzPSLogin } from './AzPSLogin';

export default class AzPSConfig {
static async setPSModulePathForGitHubRunner() {
const runner: string = process.env.RUNNER_OS || os.type();
switch (runner.toLowerCase()) {
case "linux":
AzPSConfig.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX);
break;
case "windows":
case "windows_nt":
AzPSConfig.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS);
break;
case "macos":
case "darwin":
core.warning(`Skip setting the default PowerShell module path for OS ${runner.toLowerCase()}.`);
break;
default:
core.warning(`Skip setting the default PowerShell module path for unknown OS ${runner.toLowerCase()}.`);
break;
}
}

private static pushPSModulePath(psModulePath: string) {
process.env.PSModulePath = `${psModulePath}${path.delimiter}${process.env.PSModulePath}`;
core.debug(`Set PSModulePath as ${process.env.PSModulePath}`);
}

static async importLatestAzAccounts() {
let importLatestAccountsScript: string = AzPSScriptBuilder.getImportLatestModuleScript(AzPSConstants.AzAccounts);
core.debug(`The script to import the latest Az.Accounts: ${importLatestAccountsScript}`);
let azAccountsPath: string = await AzPSLogin.runPSScript(importLatestAccountsScript);
core.debug(`The latest Az.Accounts used: ${azAccountsPath}`);
}
}
39 changes: 3 additions & 36 deletions src/PowerShell/AzPSLogin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as os from 'os';
import * as path from 'path';

import AzPSScriptBuilder from './AzPSScriptBuilder';
import AzPSConstants from './AzPSConstants';
import AzPSConfig from './AzPSConfig';
import { LoginConfig } from '../common/LoginConfig';

interface PSResultType {
Expand All @@ -23,8 +22,8 @@ export class AzPSLogin {

async login() {
core.info(`Running Azure PowerShell Login.`);
setPSModulePathForGitHubRunner();
await importLatestAzAccounts();
AzPSConfig.setPSModulePathForGitHubRunner();
await AzPSConfig.importLatestAzAccounts();
const [loginMethod, loginScript] = await AzPSScriptBuilder.getAzPSLoginScript(this.loginConfig);
core.info(`Attempting Azure PowerShell login by using ${loginMethod}...`);
core.debug(`Azure PowerShell Login Script: ${loginScript}`);
Expand Down Expand Up @@ -64,35 +63,3 @@ export class AzPSLogin {
return result.Result;
}
}

export function setPSModulePathForGitHubRunner() {
const runner: string = process.env.RUNNER_OS || os.type();
switch (runner.toLowerCase()) {
case "linux":
pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX);
break;
case "windows":
case "windows_nt":
pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS);
break;
case "macos":
case "darwin":
core.warning(`Skip setting the default PowerShell module path for OS ${runner.toLowerCase()}.`);
break;
default:
core.warning(`Skip setting the default PowerShell module path for unknown OS ${runner.toLowerCase()}.`);
break;
}
}

async function pushPSModulePath(psModulePath: string) {
process.env.PSModulePath = `${psModulePath}${path.delimiter}${process.env.PSModulePath}`;
core.debug(`Set PSModulePath as ${process.env.PSModulePath}`);
}

export async function importLatestAzAccounts() {
let importLatestAccountsScript: string = AzPSScriptBuilder.getImportLatestModuleScript(AzPSConstants.AzAccounts);
core.debug(`The script to import the latest Az.Accounts: ${importLatestAccountsScript}`);
let azAccountsPath: string = await AzPSLogin.runPSScript(importLatestAccountsScript);
core.debug(`The latest Az.Accounts used: ${azAccountsPath}`);
}
5 changes: 2 additions & 3 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as core from '@actions/core';
import { setUserAgent } from './common/Utils';
import { cleanupAzCLIAccounts, cleanupAzPSAccounts } from './common/Utils';
import { setUserAgent, cleanupAzCLIAccounts, cleanupAzPSAccounts } from './common/Utils';

async function cleanup() {
try {
Expand All @@ -11,7 +10,7 @@ async function cleanup() {
}
}
catch (error) {
core.setFailed(`Login cleanup failed with ${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.debug(error.stack);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/common/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as crypto from 'crypto';
import { setPSModulePathForGitHubRunner, importLatestAzAccounts } from '../PowerShell/AzPSLogin';
import AzPSConstants from '../PowerShell/AzPSConstants';
import AzPSConfig from '../PowerShell/AzPSConfig';

export function setUserAgent(): void {
let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex');
Expand All @@ -19,8 +19,7 @@ export async function cleanupAzCLIAccounts(): Promise<void> {
}
core.debug(`Azure CLI path: ${azPath}`);
core.info("Clearing azure cli accounts from the local cache.");
await exec.exec(`"${azPath}"`, ["account", "clear"]);

await exec.exec(`"${azPath}"`, ["account", "clear"]);
}

export async function cleanupAzPSAccounts(): Promise<void> {
Expand All @@ -30,8 +29,8 @@ export async function cleanupAzPSAccounts(): Promise<void> {
}
core.debug(`PowerShell path: ${psPath}`);
core.debug("Importing Azure PowerShell module.");
setPSModulePathForGitHubRunner();
await importLatestAzAccounts();
AzPSConfig.setPSModulePathForGitHubRunner();
await AzPSConfig.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"]);
Expand Down
20 changes: 0 additions & 20 deletions src/setup.ts

This file was deleted.

0 comments on commit 1bb2dae

Please sign in to comment.