From 12e896239faac8cbc478449e3bbd4810ddc86f2b Mon Sep 17 00:00:00 2001 From: aksm-ms <58936966+aksm-ms@users.noreply.github.com> Date: Fri, 6 Mar 2020 12:26:11 +0530 Subject: [PATCH] Added getlatestazmodule version --- lib/loginAzurePowerShell.js | 34 +++++++++++++++++++++++++++------- src/loginAzurePowerShell.ts | 33 ++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/lib/loginAzurePowerShell.js b/lib/loginAzurePowerShell.js index b3771399..fe93acd8 100644 --- a/lib/loginAzurePowerShell.js +++ b/lib/loginAzurePowerShell.js @@ -16,24 +16,24 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); const exec = __importStar(require("@actions/exec")); const io = __importStar(require("@actions/io")); var psPath; exports.initializeAz = (servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId) => __awaiter(void 0, void 0, void 0, function* () { psPath = yield io.which("pwsh", true); setPSModulePath(); + setPSModulePath(yield getLatestAzModule()); yield loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId); }); -function setPSModulePath() { - // TODO: get latest module/setup action - let azPSVersion = "3.5.0"; +function setPSModulePath(azPSVersion = "") { let modulePath = ""; switch (process.env.RUNNER_OS) { case "Linux": - modulePath = `/usr/share/az_${azPSVersion}:`; + modulePath = `/usr/share/${azPSVersion}:`; break; case "Windows": - modulePath = `C:\\Modules\\az_${azPSVersion};`; + modulePath = `C:\\Modules\\${azPSVersion};`; break; case "macOS": // TODO: add modulepath @@ -41,6 +41,26 @@ function setPSModulePath() { } process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`; } +function getLatestAzModule() { + return __awaiter(this, void 0, void 0, function* () { + const moduleName = "Az.Accounts"; + let output = ""; + let error = ""; + let options = { + listeners: { + stdout: (data) => { + output += data.toString(); + }, + stderr: (data) => { + error += data.toString(); + } + } + }; + yield executePowerShellCommand(`(Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1).Version.ToString()`, options); + core.debug(`Az Module version used: ${output}`); + return `az_${output}`; + }); +} function loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId) { return __awaiter(this, void 0, void 0, function* () { const environment = "AzureCloud"; @@ -53,10 +73,10 @@ function loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscri yield executePowerShellCommand(`Get-AzContext`); }); } -function executePowerShellCommand(command) { +function executePowerShellCommand(command, options = {}) { return __awaiter(this, void 0, void 0, function* () { try { - yield exec.exec(`"${psPath}" -Command "${command}"`, [], {}); + yield exec.exec(`"${psPath}" -Command "${command}"`, [], options); } catch (error) { throw new Error(error); diff --git a/src/loginAzurePowerShell.ts b/src/loginAzurePowerShell.ts index fa4af8ee..11d62d66 100644 --- a/src/loginAzurePowerShell.ts +++ b/src/loginAzurePowerShell.ts @@ -1,3 +1,4 @@ +import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as io from '@actions/io'; @@ -6,19 +7,18 @@ var psPath: string; export const initializeAz = async (servicePrincipalId: string, servicePrincipalKey: string, tenantId: string, subscriptionId: string) => { psPath = await io.which("pwsh", true); setPSModulePath(); + setPSModulePath(await getLatestAzModule()); await loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId); } -function setPSModulePath() { - // TODO: get latest module/setup action - let azPSVersion: string = "3.5.0"; +function setPSModulePath(azPSVersion = "") { let modulePath: string = ""; switch (process.env.RUNNER_OS) { case "Linux": - modulePath = `/usr/share/az_${azPSVersion}:`; + modulePath = `/usr/share/${azPSVersion}:`; break; case "Windows": - modulePath = `C:\\Modules\\az_${azPSVersion};`; + modulePath = `C:\\Modules\\${azPSVersion};`; break; case "macOS": // TODO: add modulepath @@ -27,6 +27,25 @@ function setPSModulePath() { process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`; } +async function getLatestAzModule() { + const moduleName = "Az.Accounts"; + let output: string = ""; + let error: string = ""; + let options: any = { + listeners: { + stdout: (data: Buffer) => { + output += data.toString(); + }, + stderr: (data: Buffer) => { + error += data.toString(); + } + } + }; + await executePowerShellCommand(`(Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1).Version.ToString()`, options); + core.debug(`Az Module version used: ${output}`); + return `az_${output}`; +} + async function loginToAzure(servicePrincipalId: string, servicePrincipalKey: string, tenantId: string, subscriptionId: string) { const environment: string = "AzureCloud"; await executePowerShellCommand(`Clear-AzContext -Scope Process`); @@ -38,9 +57,9 @@ async function loginToAzure(servicePrincipalId: string, servicePrincipalKey: str await executePowerShellCommand(`Get-AzContext`); } -async function executePowerShellCommand(command: string) { +async function executePowerShellCommand(command: string, options: any = {}) { try { - await exec.exec(`"${psPath}" -Command "${command}"`, [], {}) + await exec.exec(`"${psPath}" -Command "${command}"`, [], options); } catch (error) { throw new Error(error); }