Skip to content

Commit 12e8962

Browse files
committed
Added getlatestazmodule version
1 parent 7882b87 commit 12e8962

2 files changed

Lines changed: 53 additions & 14 deletions

File tree

lib/loginAzurePowerShell.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,51 @@ var __importStar = (this && this.__importStar) || function (mod) {
1616
return result;
1717
};
1818
Object.defineProperty(exports, "__esModule", { value: true });
19+
const core = __importStar(require("@actions/core"));
1920
const exec = __importStar(require("@actions/exec"));
2021
const io = __importStar(require("@actions/io"));
2122
var psPath;
2223
exports.initializeAz = (servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId) => __awaiter(void 0, void 0, void 0, function* () {
2324
psPath = yield io.which("pwsh", true);
2425
setPSModulePath();
26+
setPSModulePath(yield getLatestAzModule());
2527
yield loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId);
2628
});
27-
function setPSModulePath() {
28-
// TODO: get latest module/setup action
29-
let azPSVersion = "3.5.0";
29+
function setPSModulePath(azPSVersion = "") {
3030
let modulePath = "";
3131
switch (process.env.RUNNER_OS) {
3232
case "Linux":
33-
modulePath = `/usr/share/az_${azPSVersion}:`;
33+
modulePath = `/usr/share/${azPSVersion}:`;
3434
break;
3535
case "Windows":
36-
modulePath = `C:\\Modules\\az_${azPSVersion};`;
36+
modulePath = `C:\\Modules\\${azPSVersion};`;
3737
break;
3838
case "macOS":
3939
// TODO: add modulepath
4040
break;
4141
}
4242
process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`;
4343
}
44+
function getLatestAzModule() {
45+
return __awaiter(this, void 0, void 0, function* () {
46+
const moduleName = "Az.Accounts";
47+
let output = "";
48+
let error = "";
49+
let options = {
50+
listeners: {
51+
stdout: (data) => {
52+
output += data.toString();
53+
},
54+
stderr: (data) => {
55+
error += data.toString();
56+
}
57+
}
58+
};
59+
yield executePowerShellCommand(`(Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1).Version.ToString()`, options);
60+
core.debug(`Az Module version used: ${output}`);
61+
return `az_${output}`;
62+
});
63+
}
4464
function loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId) {
4565
return __awaiter(this, void 0, void 0, function* () {
4666
const environment = "AzureCloud";
@@ -53,10 +73,10 @@ function loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscri
5373
yield executePowerShellCommand(`Get-AzContext`);
5474
});
5575
}
56-
function executePowerShellCommand(command) {
76+
function executePowerShellCommand(command, options = {}) {
5777
return __awaiter(this, void 0, void 0, function* () {
5878
try {
59-
yield exec.exec(`"${psPath}" -Command "${command}"`, [], {});
79+
yield exec.exec(`"${psPath}" -Command "${command}"`, [], options);
6080
}
6181
catch (error) {
6282
throw new Error(error);

src/loginAzurePowerShell.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as core from '@actions/core';
12
import * as exec from '@actions/exec';
23
import * as io from '@actions/io';
34

@@ -6,19 +7,18 @@ var psPath: string;
67
export const initializeAz = async (servicePrincipalId: string, servicePrincipalKey: string, tenantId: string, subscriptionId: string) => {
78
psPath = await io.which("pwsh", true);
89
setPSModulePath();
10+
setPSModulePath(await getLatestAzModule());
911
await loginToAzure(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId);
1012
}
1113

12-
function setPSModulePath() {
13-
// TODO: get latest module/setup action
14-
let azPSVersion: string = "3.5.0";
14+
function setPSModulePath(azPSVersion = "") {
1515
let modulePath: string = "";
1616
switch (process.env.RUNNER_OS) {
1717
case "Linux":
18-
modulePath = `/usr/share/az_${azPSVersion}:`;
18+
modulePath = `/usr/share/${azPSVersion}:`;
1919
break;
2020
case "Windows":
21-
modulePath = `C:\\Modules\\az_${azPSVersion};`;
21+
modulePath = `C:\\Modules\\${azPSVersion};`;
2222
break;
2323
case "macOS":
2424
// TODO: add modulepath
@@ -27,6 +27,25 @@ function setPSModulePath() {
2727
process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`;
2828
}
2929

30+
async function getLatestAzModule() {
31+
const moduleName = "Az.Accounts";
32+
let output: string = "";
33+
let error: string = "";
34+
let options: any = {
35+
listeners: {
36+
stdout: (data: Buffer) => {
37+
output += data.toString();
38+
},
39+
stderr: (data: Buffer) => {
40+
error += data.toString();
41+
}
42+
}
43+
};
44+
await executePowerShellCommand(`(Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1).Version.ToString()`, options);
45+
core.debug(`Az Module version used: ${output}`);
46+
return `az_${output}`;
47+
}
48+
3049
async function loginToAzure(servicePrincipalId: string, servicePrincipalKey: string, tenantId: string, subscriptionId: string) {
3150
const environment: string = "AzureCloud";
3251
await executePowerShellCommand(`Clear-AzContext -Scope Process`);
@@ -38,9 +57,9 @@ async function loginToAzure(servicePrincipalId: string, servicePrincipalKey: str
3857
await executePowerShellCommand(`Get-AzContext`);
3958
}
4059

41-
async function executePowerShellCommand(command: string) {
60+
async function executePowerShellCommand(command: string, options: any = {}) {
4261
try {
43-
await exec.exec(`"${psPath}" -Command "${command}"`, [], {})
62+
await exec.exec(`"${psPath}" -Command "${command}"`, [], options);
4463
} catch (error) {
4564
throw new Error(error);
4665
}

0 commit comments

Comments
 (0)