icsul
icsul

Reputation: 683

Start an Azure WebJobs from Javascript code

Is there any node package or something else to start (Run) an Azure Webjob from JS? I'm an automation guy and I want to start an Azure Webjob to change some statuses in the app. Manually I just press "Run", but I want to start the job using code. An option is to login in UI and press the run button using automation, but is not looking professional for me to do it from UI.

I saw some infos there, in Webjob properties like webhook, user, password. Maybe I can use somehow those things to trigger using code.

Thank you!

Upvotes: 0

Views: 670

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

If you want to manage Azure WebJob with node, we can use the package azure-arm-website. The package provides the method runTriggeredWebJobWithHttpOperationResponse to run a trigger WebJob and the method startContinuousWebJobWithHttpOperationResponse to continuous WebJob.

For example

  1. Create a service pricipal
az login
az account set --subscription "SUBSCRIPTION_ID"
az ad sp create-for-rbac --role "Contributor" --scopes "/subscriptions/<subscription_id>"
  1. Code
const {
  ApplicationTokenCredentials,
  AzureEnvironment,
} = require("ms-rest-azure");
const { WebSiteManagementClient } = require("azure-arm-website");

const clientId = "<the sp appId>";
const tenant = "<you AD tenant domain>";
const clientSecret = "<the sp password>";
const subscriptionId = "";
const creds = new ApplicationTokenCredentials(clientId, tenant, clientSecret, {
  environment: AzureEnvironment.Azure,
});

const client = new WebSiteManagementClient(creds, subscriptionId);
const rgName = "rg-webapp-demo";
const webName = "demo-200925164220";

const jobName = "test";
client.webApps
  .runTriggeredWebJobWithHttpOperationResponse(rgName, webName, jobName)
  .then((res) => {
    console.log(res.response.statusMessage);
    console.log(res.response.statusCode);
  })
  .catch((err) => {
    throw err;
  });

  client.webApps.startContinuousWebJob

enter image description here enter image description here

Upvotes: 1

Related Questions