Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
snehapar9 committed Sep 26, 2023
1 parent 4c32513 commit ba6abd5
Show file tree
Hide file tree
Showing 7 changed files with 644 additions and 472 deletions.
89 changes: 43 additions & 46 deletions azurecontainerapps.ts

Large diffs are not rendered by default.

708 changes: 410 additions & 298 deletions dist/index.js

Large diffs are not rendered by default.

164 changes: 80 additions & 84 deletions src/ContainerAppHelper.ts

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions src/ContainerRegistryHelper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as os from 'os';
import { Utility } from './Utility';
import { GithubActionsToolHelper } from './GithubActionsToolHelper';

const githubActionsToolHelper = new GithubActionsToolHelper();
const util = new Utility();

export class ContainerRegistryHelper {
/**
Expand All @@ -12,11 +13,11 @@ export class ContainerRegistryHelper {
* @param acrPassword - the password for authentication
*/
public async loginAcrWithUsernamePassword(acrName: string, acrUsername: string, acrPassword: string) {
core.debug(`Attempting to log in to ACR instance "${acrName}" with username and password credentials`);
githubActionsToolHelper.debug(`Attempting to log in to ACR instance "${acrName}" with username and password credentials`);
try {
await exec.exec('docker', [`login`, `--password-stdin`, `--username`, `${acrUsername}`, `${acrName}.azurecr.io`], { input: Buffer.from(acrPassword) });
await githubActionsToolHelper.exec('docker', [`login`, `--password-stdin`, `--username`, `${acrUsername}`, `${acrName}.azurecr.io`], { input: Buffer.from(acrPassword) });
} catch (err) {
core.error(`Failed to log in to ACR instance "${acrName}" with username and password credentials`);
githubActionsToolHelper.error(`Failed to log in to ACR instance "${acrName}" with username and password credentials`);
throw err;
}
}
Expand All @@ -27,13 +28,13 @@ export class ContainerRegistryHelper {
* @param acrName - the name of the ACR instance to authenticate calls to.
*/
public async loginAcrWithAccessTokenAsync(acrName: string) {
core.debug(`Attempting to log in to ACR instance "${acrName}" with access token`);
githubActionsToolHelper.debug(`Attempting to log in to ACR instance "${acrName}" with access token`);
try {
const command: string = `CA_ADO_TASK_ACR_ACCESS_TOKEN=$(az acr login --name ${acrName} --output json --expose-token --only-show-errors | jq -r '.accessToken'); docker login ${acrName}.azurecr.io -u 00000000-0000-0000-0000-000000000000 -p $CA_ADO_TASK_ACR_ACCESS_TOKEN > /dev/null 2>&1`;
const shell = os.platform() === 'win32' ? 'pwsh' : 'bash';
await exec.exec(shell, ['-c', command]);
let command: string = `CA_ADO_TASK_ACR_ACCESS_TOKEN=$(az acr login --name ${acrName} --output json --expose-token --only-show-errors | jq -r '.accessToken'); docker login ${acrName}.azurecr.io -u 00000000-0000-0000-0000-000000000000 -p $CA_ADO_TASK_ACR_ACCESS_TOKEN > /dev/null 2>&1`;
let commandLine = os.platform() === 'win32' ? 'pwsh' : 'bash';
await util.executeAndThrowIfError(commandLine, ['-c', command]);
} catch (err) {
core.error(`Failed to log in to ACR instance "${acrName}" with access token`)
githubActionsToolHelper.error(`Failed to log in to ACR instance "${acrName}" with access token`)
throw err;
}
}
Expand All @@ -43,13 +44,12 @@ export class ContainerRegistryHelper {
* @param imageToPush - the name of the image to push to ACR
*/
public async pushImageToAcr(imageToPush: string) {
core.debug(`Attempting to push image "${imageToPush}" to ACR`);
githubActionsToolHelper.debug(`Attempting to push image "${imageToPush}" to ACR`);
try {
const dockerTool: string = await io.which("docker", true);
await new Utility().executeAndThrowIfError(dockerTool, [`push`, `${imageToPush}`]);
let dockerTool: string = await githubActionsToolHelper.which("docker", true);
await util.executeAndThrowIfError(dockerTool, [`push`, `${imageToPush}`]);
} catch (err) {
core.error(`Failed to push image "${imageToPush}" to ACR. Error: ${err.message}`);
core.setFailed(err.message);
githubActionsToolHelper.error(`Failed to push image "${imageToPush}" to ACR. Error: ${err.message}`);
throw err;
}
}
Expand Down
75 changes: 75 additions & 0 deletions src/GithubActionsToolHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as exec from '@actions/exec';

export class GithubActionsToolHelper {

public getGithubRunId(): string {
return process.env['GITHUB_RUN_ID'] || '';
}

public getGithubRunNumber(): string {
return process.env['GITHUB_RUN_NUMBER'] || '';
}

public info(message: string): void {
core.info(message);
}

public error(message: string): void {
core.error(message);
}

public warning(message: string): void {
core.warning(message);
}

public debug(message: string): void {
core.debug(message);
}

public async exec(commandLine: string, args?: string[], execOptions?: exec.ExecOptions): Promise<number> {
return await exec.exec(commandLine, args, execOptions);
}

public async ExecOptions(): Promise<exec.ExecOptions> {
let stdout = '';
let stderr = '';

const options: exec.ExecOptions = {
listeners: {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
},
},
};
return options;
}

public getInput(name: string, options?: core.InputOptions): string {
return core.getInput(name, options);
}

public setFailed(message: string): void {
core.setFailed(message);
}

public which(tool: string, check?: boolean): Promise<string> {
return io.which(tool, check);
}

public getContainerAppName(containerAppName: string): string {
containerAppName = `gh-action-app-${this.getGithubRunId()}-${this.getGithubRunNumber()}`;
// Replace all '.' characters with '-' characters in the Container App name
containerAppName = containerAppName.replace(/\./gi, "-");
this.info(`Default Container App name: ${containerAppName}`);
return containerAppName
}

public getTelemetryArg(): string {
return `CALLER_ID=github-actions-v1`;
}
}
17 changes: 8 additions & 9 deletions src/TelemetryHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as core from '@actions/core';
import { Utility } from './Utility';
import * as io from '@actions/io';
import { GithubActionsToolHelper } from './GithubActionsToolHelper';

const ORYX_CLI_IMAGE: string = "mcr.microsoft.com/oryx/cli:debian-buster-20230207.2";

Expand All @@ -12,6 +11,7 @@ const DOCKERFILE_SCENARIO: string = "used-dockerfile";
const IMAGE_SCENARIO: string = "used-image";

const util = new Utility();
const githubActionsToolHelper = new GithubActionsToolHelper();

export class TelemetryHelper {
readonly disableTelemetry: boolean;
Expand Down Expand Up @@ -66,9 +66,9 @@ export class TelemetryHelper {
* If telemetry is enabled, uses the "oryx telemetry" command to log metadata about this task execution.
*/
public async sendLogs() {
const taskLengthMilliseconds: number = Date.now() - this.taskStartMilliseconds;
let taskLengthMilliseconds: number = Date.now() - this.taskStartMilliseconds;
if (!this.disableTelemetry) {
core.info(`Telemetry enabled; logging metadata about task result, length and scenario targeted.`);
githubActionsToolHelper.info(`Telemetry enabled; logging metadata about task result, length and scenario targeted.`);
try {
let resultArg: string = '';
if (!util.isNullOrEmpty(this.result)) {
Expand All @@ -86,22 +86,21 @@ export class TelemetryHelper {
}

let args: string[] = [`run`, `--rm`, `${ORYX_CLI_IMAGE}`, `/bin/bash`, `-c`, `oryx telemetry --event-name ContainerAppsGitHubActionV1 ` + `--processing-time ${taskLengthMilliseconds} ${resultArg} ${scenarioArg} ${errorMessageArg}`];

await executeDockerCommand(args, true)
} catch (err) {
core.warning(`Skipping telemetry logging due to the following exception: ${err.message}`);
githubActionsToolHelper.warning(`Skipping telemetry logging due to the following exception: ${err.message}`);
}
}
}
}

const executeDockerCommand = async (args: string[], continueOnError: boolean = false): Promise<void> => {
try {
const dockerTool: string = await io.which("docker", true);
await new Utility().executeAndThrowIfError(dockerTool, args, continueOnError);
let dockerTool: string = await githubActionsToolHelper.which("docker", true);
await util.executeAndThrowIfError(dockerTool, args, continueOnError);
}
catch (err) {
core.setFailed(`Error: ${err.message}`);
githubActionsToolHelper.error(`Error: ${err.message}`);
throw err; // Re-throw the error
}
}
31 changes: 12 additions & 19 deletions src/Utility.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
// Note: This file is used to define utility functions that can be used across the project.
import { GithubActionsToolHelper } from './GithubActionsToolHelper';

const githubActionsToolHelper = new GithubActionsToolHelper();

export class Utility {
/**
* @param commandLine - the command to execute
Expand All @@ -9,36 +12,26 @@ export class Utility {

public async executeAndThrowIfError(commandLine: string, args: string[], continueOnError: boolean = false): Promise<{ exitCode: number, stdout: string, stderr: string }> {
try {
let stdout = '';
let stderr = '';

const options: exec.ExecOptions = {
listeners: {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
},
},
};
let options = await githubActionsToolHelper.ExecOptions();
let stderr = options.listeners.stderr.toString();
let stdout = options.listeners.stdout.toString();

const exitCode = await exec.exec(commandLine, args, options);
let exitCode = await githubActionsToolHelper.exec(commandLine, args, options);

if (!continueOnError && exitCode !== 0) {
core.error(`Command failed with exit code ${exitCode}. Error stream: ${stderr}`);
githubActionsToolHelper.error(`Command failed with exit code ${exitCode}. Error stream: ${stderr}`);
throw new Error(`Command failed with exit code ${exitCode}. Error stream: ${stderr}`);
}
return new Promise((resolve, reject) => {
const executionResult = {
let executionResult = {
exitCode: exitCode,
stdout: stdout,
stderr: stderr
}
resolve(executionResult);
});
} catch (error) {
core.setFailed(`Error: ${error.message}`);
githubActionsToolHelper.error(`Error: ${error.message}`);
throw error; // Re-throw the error
}
}
Expand Down

0 comments on commit ba6abd5

Please sign in to comment.