Added getlatestazmodule version

This commit is contained in:
aksm-ms
2020-03-06 12:26:11 +05:30
parent 7882b87bc3
commit 12e896239f
2 changed files with 53 additions and 14 deletions

View File

@@ -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);
}