You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Azure Node.js管理API SDK部署Web应用的技术咨询

Absolutely! I’ve put together a complete, working example using the Azure Node.js SDK (specifically the @azure/arm-appservice package) that covers every operation you outlined. Let’s walk through this step by step.

Azure Node.js SDK Web App Configuration Example

Prerequisites

  • First, install the required packages:
    npm install @azure/arm-appservice @azure/identity
    
  • Ensure you have Azure credentials set up locally (run az login if testing on your machine) or use a service principal for production environments.

Full Implementation Code

This script handles creating the Web App, linking Bitbucket as the deployment source, setting environment variables, and restarting the app:

const { DefaultAzureCredential } = require("@azure/identity");
const { WebSiteManagementClient } = require("@azure/arm-appservice");

// Replace these with your Azure and Bitbucket details
const SUBSCRIPTION_ID = "YOUR_AZURE_SUBSCRIPTION_ID";
const RESOURCE_GROUP_NAME = "YOUR_RESOURCE_GROUP";
const WEB_APP_NAME = "your-unique-webapp-name";
const LOCATION = "eastus"; // Adjust to your preferred region
const BITBUCKET_REPO_URL = "https://bitbucket.org/your-username/your-repo.git";
const BITBUCKET_BRANCH = "main";

// Initialize the Azure client
const credential = new DefaultAzureCredential();
const client = new WebSiteManagementClient(credential, SUBSCRIPTION_ID);

async function createWebApp() {
  // First create an App Service Plan (required for hosting Web Apps)
  const appServicePlanName = `${WEB_APP_NAME}-host-plan`;
  await client.appServicePlans.beginCreateOrUpdateAndWait(RESOURCE_GROUP_NAME, appServicePlanName, {
    location: LOCATION,
    sku: {
      name: "B1", // Basic tier - adjust to F1 (free) or higher tiers as needed
      tier: "Basic",
      size: "B1",
      family: "B",
      capacity: 1
    }
  });

  // Create the Web App linked to the new plan
  const webApp = await client.webApps.beginCreateOrUpdateAndWait(RESOURCE_GROUP_NAME, WEB_APP_NAME, {
    location: LOCATION,
    serverFarmId: `/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP_NAME}/providers/Microsoft.Web/serverfarms/${appServicePlanName}`
  });

  console.log(`✅ Web App created: ${webApp.defaultHostName}`);
  return webApp;
}

async function setupBitbucketDeployment() {
  // Configure Bitbucket as the deployment source
  await client.webApps.beginCreateOrUpdateSourceControlAndWait(RESOURCE_GROUP_NAME, WEB_APP_NAME, {
    repoUrl: BITBUCKET_REPO_URL,
    branch: BITBUCKET_BRANCH,
    isManualIntegration: false, // Enable auto-sync on branch pushes
    deploymentRollbackEnabled: true
    // Uncomment below for private repos (use a Bitbucket App Password)
    // username: "YOUR_BITBUCKET_USERNAME",
    // password: "YOUR_BITBUCKET_APP_PASSWORD"
  });

  console.log("🔄 Bitbucket deployment source configured - triggering sync...");
  await client.webApps.syncRepository(RESOURCE_GROUP_NAME, WEB_APP_NAME);
  console.log("✅ Deployment synced successfully");
}

async function setEnvironmentVariables() {
  // Define your environment variables/app settings
  const appSettings = {
    NODE_ENV: "production",
    API_KEY: "your-api-key-here",
    DATABASE_URL: "your-db-connection-string"
  };

  // Merge with existing settings (avoids overwriting default app settings)
  const existingSettings = await client.webApps.listApplicationSettings(RESOURCE_GROUP_NAME, WEB_APP_NAME);
  const mergedSettings = { ...existingSettings.properties, ...appSettings };

  await client.webApps.updateApplicationSettings(RESOURCE_GROUP_NAME, WEB_APP_NAME, {
    properties: mergedSettings
  });

  console.log("✅ Environment variables configured");
}

async function restartWebApp() {
  await client.webApps.beginRestartAndWait(RESOURCE_GROUP_NAME, WEB_APP_NAME);
  console.log("✅ Web App restarted successfully");
}

// Run all operations in sequence
async function runAllTasks() {
  try {
    await createWebApp();
    await setupBitbucketDeployment();
    await setEnvironmentVariables();
    await restartWebApp();
    console.log("\n🎉 All operations completed successfully!");
  } catch (error) {
    console.error("\n❌ Error during execution:", error.message);
  }
}

runAllTasks();

Key Notes

  • Private Bitbucket Repos: For private repositories, generate a Bitbucket App Password (found in your Bitbucket account settings) and uncomment the username and password fields in the deployment source configuration.
  • App Service Plan: Adjust the SKU details (like switching to F1 for the free tier) based on your hosting needs. If you already have an existing plan, skip the plan creation step and use its resource ID for serverFarmId.
  • Credential Handling: DefaultAzureCredential automatically picks up credentials from your local Azure CLI login, environment variables, or managed identity (if running in Azure). For production, use a service principal with restricted permissions.
  • Deployment Sync: The syncRepository call triggers an immediate deployment. With isManualIntegration: false, future pushes to your Bitbucket branch will auto-deploy to the Web App.

内容的提问来源于stack exchange,提问作者ubreddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:23:49