Skip to content

Commit

Permalink
test pre post action-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
MoChilia committed Dec 13, 2023
1 parent 1b07ea9 commit fabc40c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 51 deletions.
6 changes: 0 additions & 6 deletions src/Cli/AzureCliLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {
Expand Down
15 changes: 15 additions & 0 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
@@ -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();
90 changes: 45 additions & 45 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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();
22 changes: 22 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit fabc40c

Please sign in to comment.