Skip to content

Commit

Permalink
feat: add environment support (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljtill authored Jan 17, 2025
1 parent 5b62d98 commit b4aa25b
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 24 deletions.
9 changes: 6 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ description: "GitHub Action for deploying to Azure"
author: "Microsoft"

branding:
icon: 'upload-cloud'
color: 'blue'
icon: "upload-cloud"
color: "blue"

inputs:
type:
Expand Down Expand Up @@ -54,7 +54,6 @@ inputs:
description: "Specifies the parameters to use."
required: false


what-if-exclude-change-types:
description: "Specifies the change types to exclude from the 'What If' operation."
required: false
Expand Down Expand Up @@ -96,6 +95,10 @@ inputs:
description: "Specifies output names to mask values for."
required: false

environment:
description: "Specifies the environment to use. Choose from 'azureCloud', 'azureChinaCloud', 'azureGermanCloud', 'azureUSGovernment'."
required: false

runs:
using: node20
main: dist/index.js
40 changes: 28 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59278,6 +59278,12 @@ function parseConfig() {
const description = (0, input_1.getOptionalStringInput)("description");
const tags = (0, input_1.getOptionalStringDictionaryInput)("tags");
const maskedOutputs = (0, input_1.getOptionalStringArrayInput)("masked-outputs");
const environment = (0, input_1.getOptionalEnumInput)("environment", [
"azureCloud",
"azureChinaCloud",
"azureGermanCloud",
"azureUSGovernment",
]) ?? "azureCloud";
switch (type) {
case "deployment": {
return {
Expand All @@ -59289,6 +59295,7 @@ function parseConfig() {
parameters,
tags,
maskedOutputs,
environment: environment,
operation: (0, input_1.getRequiredEnumInput)("operation", [
"create",
"validate",
Expand Down Expand Up @@ -59319,6 +59326,7 @@ function parseConfig() {
description,
tags,
maskedOutputs,
environment: environment,
operation: (0, input_1.getRequiredEnumInput)("operation", [
"create",
"validate",
Expand Down Expand Up @@ -59468,15 +59476,15 @@ function getCreateOperationOptions() {
},
};
}
function getDeploymentClient(scope) {
function getDeploymentClient(config, scope) {
const { tenantId } = scope;
const subscriptionId = "subscriptionId" in scope ? scope.subscriptionId : undefined;
return (0, azure_1.createDeploymentClient)(subscriptionId, tenantId);
return (0, azure_1.createDeploymentClient)(config, subscriptionId, tenantId);
}
function getStacksClient(scope) {
function getStacksClient(config, scope) {
const { tenantId } = scope;
const subscriptionId = "subscriptionId" in scope ? scope.subscriptionId : undefined;
return (0, azure_1.createStacksClient)(subscriptionId, tenantId);
return (0, azure_1.createStacksClient)(config, subscriptionId, tenantId);
}
async function execute(config, files) {
try {
Expand Down Expand Up @@ -59565,7 +59573,7 @@ function setCreateOutputs(config, outputs) {
async function deploymentCreate(config, files) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getDeploymentClient(scope);
const client = getDeploymentClient(config, scope);
const deployment = getDeployment(config, files);
switch (scope.type) {
case "resourceGroup":
Expand All @@ -59590,7 +59598,7 @@ async function deploymentCreate(config, files) {
async function deploymentValidate(config, files) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getDeploymentClient(scope);
const client = getDeploymentClient(config, scope);
const deployment = getDeployment(config, files);
switch (scope.type) {
case "resourceGroup":
Expand All @@ -59615,7 +59623,7 @@ async function deploymentValidate(config, files) {
async function deploymentWhatIf(config, files) {
const deploymentName = config.name ?? defaultName;
const scope = config.scope;
const client = getDeploymentClient(scope);
const client = getDeploymentClient(config, scope);
const deployment = getDeployment(config, files);
switch (scope.type) {
case "resourceGroup":
Expand Down Expand Up @@ -59660,7 +59668,7 @@ function getDeployment(config, files) {
async function stackCreate(config, files) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getStacksClient(scope);
const client = getStacksClient(config, scope);
const stack = getStack(config, files);
switch (scope.type) {
case "resourceGroup":
Expand All @@ -59680,7 +59688,7 @@ async function stackCreate(config, files) {
async function stackValidate(config, files) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getStacksClient(scope);
const client = getStacksClient(config, scope);
const stack = getStack(config, files);
switch (scope.type) {
case "resourceGroup":
Expand All @@ -59700,7 +59708,7 @@ async function stackValidate(config, files) {
async function stackDelete(config) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getStacksClient(scope);
const client = getStacksClient(config, scope);
const deletionOptions = getStackDeletionOptions(config);
switch (scope.type) {
case "resourceGroup":
Expand Down Expand Up @@ -59824,7 +59832,13 @@ const identity_1 = __nccwpck_require__(3983);
const core_1 = __nccwpck_require__(7484);
const userAgentPrefix = "gh-azure-bicep-deploy";
const dummySubscriptionId = "00000000-0000-0000-0000-000000000000";
function createDeploymentClient(subscriptionId, tenantId) {
const endpoints = {
azureCloud: "https://management.azure.com",
azureChinaCloud: "https://management.chinacloudapi.cn",
azureGermanCloud: "https://management.microsoftazure.de",
azureUSGovernment: "https://management.usgovcloudapi.net",
};
function createDeploymentClient(config, subscriptionId, tenantId) {
const credentials = new identity_1.DefaultAzureCredential({ tenantId });
return new arm_resources_1.ResourceManagementClient(credentials,
// Use a dummy subscription ID for above-subscription scope operations
Expand All @@ -59835,9 +59849,10 @@ function createDeploymentClient(subscriptionId, tenantId) {
additionalPolicies: [debugLoggingPolicy],
// Use a recent API version to take advantage of error improvements
apiVersion: "2024-03-01",
endpoint: endpoints[config.environment],
});
}
function createStacksClient(subscriptionId, tenantId) {
function createStacksClient(config, subscriptionId, tenantId) {
const credentials = new identity_1.DefaultAzureCredential({ tenantId });
return new arm_resourcesdeploymentstacks_1.DeploymentStacksClient(credentials,
// Use a dummy subscription ID for above-subscription scope operations
Expand All @@ -59846,6 +59861,7 @@ function createStacksClient(subscriptionId, tenantId) {
userAgentPrefix: userAgentPrefix,
},
additionalPolicies: [debugLoggingPolicy],
endpoint: endpoints[config.environment],
});
}
// Log request + response bodies to GitHub Actions debug output if enabled
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type CommonConfig = {
location?: string;
tags?: Record<string, string>;
maskedOutputs?: string[];
environment:
| "azureCloud"
| "azureChinaCloud"
| "azureGermanCloud"
| "azureUSGovernment";
} & FileConfig;

type WhatIfChangeType =
Expand Down Expand Up @@ -111,6 +116,13 @@ export function parseConfig(): DeploymentsConfig | DeploymentStackConfig {
const description = getOptionalStringInput("description");
const tags = getOptionalStringDictionaryInput("tags");
const maskedOutputs = getOptionalStringArrayInput("masked-outputs");
const environment =
getOptionalEnumInput("environment", [
"azureCloud",
"azureChinaCloud",
"azureGermanCloud",
"azureUSGovernment",
]) ?? "azureCloud";

switch (type) {
case "deployment": {
Expand All @@ -123,6 +135,7 @@ export function parseConfig(): DeploymentsConfig | DeploymentStackConfig {
parameters,
tags,
maskedOutputs,
environment: environment,
operation: getRequiredEnumInput("operation", [
"create",
"validate",
Expand Down Expand Up @@ -156,6 +169,7 @@ export function parseConfig(): DeploymentsConfig | DeploymentStackConfig {
description,
tags,
maskedOutputs,
environment: environment,
operation: getRequiredEnumInput("operation", [
"create",
"validate",
Expand Down
18 changes: 10 additions & 8 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function getCreateOperationOptions(): OperationOptions {
}

function getDeploymentClient(
config: ActionConfig,
scope:
| TenantScope
| ManagementGroupScope
Expand All @@ -61,10 +62,11 @@ function getDeploymentClient(
const subscriptionId =
"subscriptionId" in scope ? scope.subscriptionId : undefined;

return createDeploymentClient(subscriptionId, tenantId);
return createDeploymentClient(config, subscriptionId, tenantId);
}

function getStacksClient(
config: ActionConfig,
scope:
| TenantScope
| ManagementGroupScope
Expand All @@ -75,7 +77,7 @@ function getStacksClient(
const subscriptionId =
"subscriptionId" in scope ? scope.subscriptionId : undefined;

return createStacksClient(subscriptionId, tenantId);
return createStacksClient(config, subscriptionId, tenantId);
}

export async function execute(config: ActionConfig, files: ParsedFiles) {
Expand Down Expand Up @@ -188,7 +190,7 @@ function setCreateOutputs(
async function deploymentCreate(config: DeploymentsConfig, files: ParsedFiles) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getDeploymentClient(scope);
const client = getDeploymentClient(config, scope);
const deployment = getDeployment(config, files);

switch (scope.type) {
Expand Down Expand Up @@ -236,7 +238,7 @@ async function deploymentValidate(
) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getDeploymentClient(scope);
const client = getDeploymentClient(config, scope);
const deployment = getDeployment(config, files);

switch (scope.type) {
Expand Down Expand Up @@ -274,7 +276,7 @@ async function deploymentValidate(
async function deploymentWhatIf(config: DeploymentsConfig, files: ParsedFiles) {
const deploymentName = config.name ?? defaultName;
const scope = config.scope;
const client = getDeploymentClient(scope);
const client = getDeploymentClient(config, scope);
const deployment = getDeployment(config, files);

switch (scope.type) {
Expand Down Expand Up @@ -340,7 +342,7 @@ function getDeployment(
async function stackCreate(config: DeploymentStackConfig, files: ParsedFiles) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getStacksClient(scope);
const client = getStacksClient(config, scope);
const stack = getStack(config, files);

switch (scope.type) {
Expand Down Expand Up @@ -376,7 +378,7 @@ async function stackValidate(
) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getStacksClient(scope);
const client = getStacksClient(config, scope);
const stack = getStack(config, files);

switch (scope.type) {
Expand Down Expand Up @@ -409,7 +411,7 @@ async function stackValidate(
async function stackDelete(config: DeploymentStackConfig) {
const name = config.name ?? defaultName;
const scope = config.scope;
const client = getStacksClient(scope);
const client = getStacksClient(config, scope);
const deletionOptions = getStackDeletionOptions(config);

switch (scope.type) {
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ import { DefaultAzureCredential } from "@azure/identity";
import { AdditionalPolicyConfig } from "@azure/core-client";
import { debug, isDebug } from "@actions/core";

import { ActionConfig } from "../config";

const userAgentPrefix = "gh-azure-bicep-deploy";
const dummySubscriptionId = "00000000-0000-0000-0000-000000000000";
const endpoints = {
azureCloud: "https://management.azure.com",
azureChinaCloud: "https://management.chinacloudapi.cn",
azureGermanCloud: "https://management.microsoftazure.de",
azureUSGovernment: "https://management.usgovcloudapi.net",
};

export function createDeploymentClient(
config: ActionConfig,
subscriptionId?: string,
tenantId?: string,
): ResourceManagementClient {
Expand All @@ -26,11 +35,13 @@ export function createDeploymentClient(
additionalPolicies: [debugLoggingPolicy],
// Use a recent API version to take advantage of error improvements
apiVersion: "2024-03-01",
endpoint: endpoints[config.environment],
},
);
}

export function createStacksClient(
config: ActionConfig,
subscriptionId?: string,
tenantId?: string,
): DeploymentStacksClient {
Expand All @@ -45,6 +56,7 @@ export function createStacksClient(
userAgentPrefix: userAgentPrefix,
},
additionalPolicies: [debugLoggingPolicy],
endpoint: endpoints[config.environment],
},
);
}
Expand Down
Loading

0 comments on commit b4aa25b

Please sign in to comment.