Search code examples
entity-frameworkazure-webjobscontinuous-deploymententity-framework-migrationsazure-deployment

Azure Web Job Run Once


I want to use an Azure Web Job run once, when I deploy (continuous deployment from Github). In my console application I simply print a line and exit. I chose continuous for schedule:

namespace MyApp.Deployment
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Performing EF Migrations at " + DateTime.Now.ToLongDateString());
            //run EF migrations here.
            //Can't run migrations in my web service App_Start because web service connects as a limited SQL user.
            Console.WriteLine("Migrations complete");
        }
    }
}

But azure just keeps running this app after a 1 minute interval. Is there anyway to tell azure not to restart it?

Is there a better way to run a PS1 script immediately after deployment?


Solution

  • I was able to accomplish this with the following:

    1. Create a console application as a web job and set it up to use the manual trigger. In my case, the console app simply creates a DbMigrator, sets the connection string (from an environment variable) and then calls Update().

    2. Deploy your application with the new web job. Log into the kudu console, and determine the path to your_console_app.exe. Then Create a custom kudu deploy.cmd, and make sure to call your web job after the KuduSync portion.

    3. The next time you deploy, your console application will be run after all the files have been built and deployed, and you can see the output in either Kudu or in the azure portal.

    Hope this helps someone.