Skip to content

Commit

Permalink
Merge branch 'snehapar/Remove-hardcoded-location' into snehpar/test-oidc
Browse files Browse the repository at this point in the history
  • Loading branch information
snehapar9 committed Nov 14, 2023
2 parents a27cb86 + 0a40db5 commit 01a9bf5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
25 changes: 14 additions & 11 deletions azurecontainerapps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class azurecontainerapps {
// Get the resource group to deploy to if it was provided, or generate it from the Container App name
this.resourceGroup = await this.getOrCreateResourceGroup(this.containerAppName, this.location);

// Determine if the Container App currently exists
// Determine if the Container Appp currently exists
this.containerAppExists = await this.appHelper.doesContainerAppExist(this.containerAppName, this.resourceGroup);

// If the Container App doesn't exist, get/create the Container App Environment to use for the Container App
Expand Down Expand Up @@ -232,40 +232,43 @@ export class azurecontainerapps {
private static async getLocation(): Promise<string> {
// Set deployment location, if provided
let location: string = this.toolHelper.getInput('location', false);
let resourceGroup: string = this.toolHelper.getInput('resourceGroup', false);
let containerAppEnvironment: string = this.toolHelper.getInput('containerAppEnvironment', false);

if (!this.util.isNullOrEmpty(location)) {
return location;
}

// If no location was provided, attempt to discover the location of the existing Container App Environment linked to the Container App
// or Container App Environment provided in the resource group or use the default location.
// Get the resource group if it was provided
let resourceGroup: string = this.toolHelper.getInput('resourceGroup', false);

if (!this.util.isNullOrEmpty(resourceGroup)) {
// Check if Container App exists in the resource group provided and get the location from the Container App Environment linked to it
let doesContainerAppExist = await this.appHelper.doesContainerAppExist(this.containerAppName, resourceGroup);
if (doesContainerAppExist) {
let containerAppExists = await this.appHelper.doesContainerAppExist(this.containerAppName, resourceGroup);
if (containerAppExists) {
// Get the name of the Container App Environment linked to the Container App
var environmentName = await this.appHelper.getExistingContainerAppEnvironmentName(this.containerAppName, resourceGroup);

// Check if environment exists in the resource group provided and get the location
var doesContainerAppEnvironmentExistInResourceGroup = !this.util.isNullOrEmpty(environmentName) ? await this.appHelper.doesContainerAppEnvironmentExist(environmentName, resourceGroup) : false;
if (doesContainerAppEnvironmentExistInResourceGroup) {
var containerAppEnvironmentExistsInResourceGroup = !this.util.isNullOrEmpty(environmentName) ? await this.appHelper.doesContainerAppEnvironmentExist(environmentName, resourceGroup) : false;
if (containerAppEnvironmentExistsInResourceGroup) {
// Get the location of the Container App Environment linked to the Container App
location = await this.appHelper.getExistingContainerAppEnvironmentLocation(environmentName, resourceGroup);
return location;
}
}

// Get the Container App Environment name if it was provided
let containerAppEnvironment: string = this.toolHelper.getInput('containerAppEnvironment', false);

// Check if Container App Environment is provided and exits in the resource group provided and get the location
let doesContainerAppEnvironmentExist = !this.util.isNullOrEmpty(containerAppEnvironment) ? await this.appHelper.doesContainerAppEnvironmentExist(containerAppEnvironment, resourceGroup) : false;
if (doesContainerAppEnvironmentExist) {
let containerAppEnvironmentExists = !this.util.isNullOrEmpty(containerAppEnvironment) ? await this.appHelper.doesContainerAppEnvironmentExist(containerAppEnvironment, resourceGroup) : false;
if (containerAppEnvironmentExists) {
location = await this.appHelper.getExistingContainerAppEnvironmentLocation(containerAppEnvironment, resourceGroup);
return location;
}
}

// Get the default location for if the Container App or Container App Environment was not found in the resource group provided.
// Get the default location if the Container App or Container App Environment was not found in the resource group provided.
location = await this.appHelper.getDefaultContainerAppLocation();
return location;

Expand Down
35 changes: 20 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ var azurecontainerapps = /** @class */ (function () {
case 2:
// Get the resource group to deploy to if it was provided, or generate it from the Container App name
_b.resourceGroup = _e.sent();
// Determine if the Container App currently exists
// Determine if the Container Appp currently exists
_c = this;
return [4 /*yield*/, this.appHelper.doesContainerAppExist(this.containerAppName, this.resourceGroup)];
case 3:
// Determine if the Container App currently exists
// Determine if the Container Appp currently exists
_c.containerAppExists = _e.sent();
if (!!this.containerAppExists) return [3 /*break*/, 5];
_d = this;
Expand Down Expand Up @@ -256,21 +256,20 @@ var azurecontainerapps = /** @class */ (function () {
*/
azurecontainerapps.getLocation = function () {
return __awaiter(this, void 0, void 0, function () {
var location, resourceGroup, containerAppEnvironment, doesContainerAppExist, environmentName, doesContainerAppEnvironmentExistInResourceGroup, _a, doesContainerAppEnvironmentExist, _b;
var location, resourceGroup, containerAppExists, environmentName, containerAppEnvironmentExistsInResourceGroup, _a, containerAppEnvironment, containerAppEnvironmentExists, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
location = this.toolHelper.getInput('location', false);
resourceGroup = this.toolHelper.getInput('resourceGroup', false);
containerAppEnvironment = this.toolHelper.getInput('containerAppEnvironment', false);
if (!this.util.isNullOrEmpty(location)) {
return [2 /*return*/, location];
}
resourceGroup = this.toolHelper.getInput('resourceGroup', false);
if (!!this.util.isNullOrEmpty(resourceGroup)) return [3 /*break*/, 12];
return [4 /*yield*/, this.appHelper.doesContainerAppExist(this.containerAppName, resourceGroup)];
case 1:
doesContainerAppExist = _c.sent();
if (!doesContainerAppExist) return [3 /*break*/, 7];
containerAppExists = _c.sent();
if (!containerAppExists) return [3 /*break*/, 7];
return [4 /*yield*/, this.appHelper.getExistingContainerAppEnvironmentName(this.containerAppName, resourceGroup)];
case 2:
environmentName = _c.sent();
Expand All @@ -283,14 +282,15 @@ var azurecontainerapps = /** @class */ (function () {
_a = false;
_c.label = 5;
case 5:
doesContainerAppEnvironmentExistInResourceGroup = _a;
if (!doesContainerAppEnvironmentExistInResourceGroup) return [3 /*break*/, 7];
containerAppEnvironmentExistsInResourceGroup = _a;
if (!containerAppEnvironmentExistsInResourceGroup) return [3 /*break*/, 7];
return [4 /*yield*/, this.appHelper.getExistingContainerAppEnvironmentLocation(environmentName, resourceGroup)];
case 6:
// Get the location of the Container App Environment linked to the Container App
location = _c.sent();
return [2 /*return*/, location];
case 7:
containerAppEnvironment = this.toolHelper.getInput('containerAppEnvironment', false);
if (!!this.util.isNullOrEmpty(containerAppEnvironment)) return [3 /*break*/, 9];
return [4 /*yield*/, this.appHelper.doesContainerAppEnvironmentExist(containerAppEnvironment, resourceGroup)];
case 8:
Expand All @@ -300,15 +300,15 @@ var azurecontainerapps = /** @class */ (function () {
_b = false;
_c.label = 10;
case 10:
doesContainerAppEnvironmentExist = _b;
if (!doesContainerAppEnvironmentExist) return [3 /*break*/, 12];
containerAppEnvironmentExists = _b;
if (!containerAppEnvironmentExists) return [3 /*break*/, 12];
return [4 /*yield*/, this.appHelper.getExistingContainerAppEnvironmentLocation(containerAppEnvironment, resourceGroup)];
case 11:
location = _c.sent();
return [2 /*return*/, location];
case 12: return [4 /*yield*/, this.appHelper.getDefaultContainerAppLocation()];
case 13:
// Get the default location for if the Container App or Container App Environment was not found in the resource group provided.
// Get the default location if the Container App or Container App Environment was not found in the resource group provided.
location = _c.sent();
return [2 /*return*/, location];
}
Expand Down Expand Up @@ -5153,13 +5153,13 @@ var ContainerAppHelper = /** @class */ (function () {
});
};
/**
* Gets the environment Id of an existing Container App
* Gets the environment name of an existing Container App
* @param containerAppName - the name of the Container App
* @param resourceGroup - the resource group that the Container App is found in
*/
ContainerAppHelper.prototype.getExistingContainerAppEnvironmentName = function (containerAppName, resourceGroup) {
return __awaiter(this, void 0, void 0, function () {
var command, executionResult, err_14;
var command, executionResult, containerappEnvironmentId, err_14;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
Expand All @@ -5168,7 +5168,12 @@ var ContainerAppHelper = /** @class */ (function () {
return [4 /*yield*/, util.execute(command)];
case 1:
executionResult = _a.sent();
return [2 /*return*/, executionResult.exitCode === 0 ? executionResult.stdout.split("/").pop() : null];
containerappEnvironmentId = executionResult.stdout.trim();
//Remove trailing slash if it exists
if (!util.isNullOrEmpty(containerappEnvironmentId)) {
containerappEnvironmentId = containerappEnvironmentId.endsWith("/") ? containerappEnvironmentId.slice(0, -1) : containerappEnvironmentId;
}
return [2 /*return*/, executionResult.exitCode === 0 ? containerappEnvironmentId.split("/").pop().trim() : null];
case 2:
err_14 = _a.sent();
toolHelper.writeInfo(err_14.message);
Expand Down
27 changes: 17 additions & 10 deletions src/ContainerAppHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs = require('fs');
const ORYX_CLI_IMAGE: string = 'mcr.microsoft.com/oryx/cli:builder-debian-bullseye-20230926.1';
const ORYX_BULLSEYE_BUILDER_IMAGE: string = 'mcr.microsoft.com/oryx/builder:debian-bullseye-20231107.2'
const ORYX_BOOKWORM_BUILDER_IMAGE: string = 'mcr.microsoft.com/oryx/builder:debian-bookworm-20231107.2'
const ORYX_BUILDER_IMAGES: string[] = [ ORYX_BULLSEYE_BUILDER_IMAGE, ORYX_BOOKWORM_BUILDER_IMAGE ];
const ORYX_BUILDER_IMAGES: string[] = [ORYX_BULLSEYE_BUILDER_IMAGE, ORYX_BOOKWORM_BUILDER_IMAGE];
const IS_WINDOWS_AGENT: boolean = os.platform() == 'win32';
const PACK_CMD: string = IS_WINDOWS_AGENT ? path.join(os.tmpdir(), 'pack') : 'pack';
const toolHelper = new GitHubActionsToolHelper();
Expand Down Expand Up @@ -45,13 +45,13 @@ export class ContainerAppHelper {
}
}

/**
* Creates an Azure Container App.
* @param containerAppName - the name of the Container App
* @param resourceGroup - the resource group that the Container App is found in
* @param optionalCmdArgs - a set of optional command line arguments
*/
public async createOrUpdateContainerAppWithUp(
/**
* Creates an Azure Container App.
* @param containerAppName - the name of the Container App
* @param resourceGroup - the resource group that the Container App is found in
* @param optionalCmdArgs - a set of optional command line arguments
*/
public async createOrUpdateContainerAppWithUp(
containerAppName: string,
resourceGroup: string,
optionalCmdArgs: string[]) {
Expand Down Expand Up @@ -287,15 +287,22 @@ export class ContainerAppHelper {
}

/**
* Gets the environment Id of an existing Container App
* Gets the environment name of an existing Container App
* @param containerAppName - the name of the Container App
* @param resourceGroup - the resource group that the Container App is found in
*/
public async getExistingContainerAppEnvironmentName(containerAppName: string, resourceGroup: string) {
try {
let command = `az containerapp show -n ${containerAppName} -g ${resourceGroup} --query properties.environmentId`;
let executionResult = await util.execute(command);
return executionResult.exitCode === 0 ? executionResult.stdout.split("/").pop() : null;
let containerappEnvironmentId = executionResult.stdout.trim();

//Remove trailing slash if it exists
if (!util.isNullOrEmpty(containerappEnvironmentId)) {
containerappEnvironmentId = containerappEnvironmentId.endsWith("/") ? containerappEnvironmentId.slice(0, -1) : containerappEnvironmentId;
}

return executionResult.exitCode === 0 ? containerappEnvironmentId.split("/").pop().trim() : null;
} catch (err) {
toolHelper.writeInfo(err.message);
return null;
Expand Down

0 comments on commit 01a9bf5

Please sign in to comment.