Skip to content

Commit

Permalink
feat: add cloud endpoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
ljtill committed Nov 18, 2024
1 parent bd76686 commit 923eb2e
Show file tree
Hide file tree
Showing 6 changed files with 62 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 @@ -93,6 +92,10 @@ inputs:
description: "Specifies output names to mask values for."
required: false

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

runs:
using: node20
main: dist/index.js
38 changes: 26 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59278,6 +59278,11 @@ 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 cloud = (0, input_1.getOptionalEnumInput)("cloud", [
"azureChinaCloud",
"azureUSGovernment",
"azureGermanCloud",
]);
switch (type) {
case "deployment": {
return {
Expand All @@ -59289,6 +59294,7 @@ function parseConfig() {
parameters,
tags,
maskedOutputs,
cloud,
operation: (0, input_1.getRequiredEnumInput)("operation", [
"create",
"validate",
Expand Down Expand Up @@ -59319,6 +59325,7 @@ function parseConfig() {
description,
tags,
maskedOutputs,
cloud,
operation: (0, input_1.getRequiredEnumInput)("operation", [
"create",
"validate",
Expand Down Expand Up @@ -59446,15 +59453,15 @@ const azure_1 = __nccwpck_require__(5886);
const logging_1 = __nccwpck_require__(5504);
const whatif_1 = __nccwpck_require__(5180);
const defaultName = "azure-bicep-deploy";
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 @@ -59543,7 +59550,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 @@ -59568,7 +59575,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 @@ -59593,7 +59600,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 @@ -59638,7 +59645,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 @@ -59658,7 +59665,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 @@ -59678,7 +59685,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 @@ -59793,7 +59800,12 @@ 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 cloudEndpoints = {
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 @@ -59804,9 +59816,10 @@ function createDeploymentClient(subscriptionId, tenantId) {
additionalPolicies: [debugLoggingPolicy],
// Use a recent API version to take advantage of error improvements
apiVersion: "2024-03-01",
endpoint: config.cloud ? cloudEndpoints[config.cloud] : undefined,
});
}
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 @@ -59815,6 +59828,7 @@ function createStacksClient(subscriptionId, tenantId) {
userAgentPrefix: userAgentPrefix,
},
additionalPolicies: [debugLoggingPolicy],
endpoint: config.cloud ? cloudEndpoints[config.cloud] : undefined,
});
}
// 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.

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

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

switch (type) {
case "deployment": {
Expand All @@ -122,6 +128,7 @@ export function parseConfig(): DeploymentsConfig | DeploymentStackConfig {
parameters,
tags,
maskedOutputs,
cloud,
operation: getRequiredEnumInput("operation", [
"create",
"validate",
Expand Down Expand Up @@ -155,6 +162,7 @@ export function parseConfig(): DeploymentsConfig | DeploymentStackConfig {
description,
tags,
maskedOutputs,
cloud,
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 @@ -23,6 +23,7 @@ import { formatWhatIfOperationResult } from "./helpers/whatif";
const defaultName = "azure-bicep-deploy";

function getDeploymentClient(
config: ActionConfig,
scope:
| TenantScope
| ManagementGroupScope
Expand All @@ -33,10 +34,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 @@ -47,7 +49,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 @@ -160,7 +162,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 @@ -201,7 +203,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 @@ -239,7 +241,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 @@ -305,7 +307,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 @@ -341,7 +343,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 @@ -374,7 +376,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
11 changes: 11 additions & 0 deletions src/helpers/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ 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 cloudEndpoints = {
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 +34,13 @@ export function createDeploymentClient(
additionalPolicies: [debugLoggingPolicy],
// Use a recent API version to take advantage of error improvements
apiVersion: "2024-03-01",
endpoint: config.cloud ? cloudEndpoints[config.cloud] : undefined,
},
);
}

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

0 comments on commit 923eb2e

Please sign in to comment.