6
votes

I've searched a bit about this, but is there documentation at all on how to schedule jobs that run with Azure CLI commands?

I want to run in the cloud, but azure automation doesn't support CLI (yet), so i am leaning towards perhaps using Azure Scheduler... but don't know the pros and cons...

Alternatively, is it possible to create a scheduled job, somehow, in the Azure Cloud Shell... i assume this is backed by some VM and therefore perhaps if it is Linux based we could whizz up a cron job... i don't really know how to do that though.

Ta!

2

2 Answers

9
votes

I tried finding some information on how to use Azure CLI in the cloud but there doesn't seem to be much available.

There are two versions of Azure CLI:

  • v1.0
    • Built as a node.js module
    • Can be installed by running npm install -g azure-cli
  • v2.0
    • Standalone package
    • Install MSI on Windows or using some package manager, e.g. apt/yum/zyper/etc, on Linux.

Azure App Service

Azure Web Apps support running applications built ontop of node.js. Technically you could then install the v1.0 module in a Web App and run your script on a schedule there.

However, recommended is to use v2.0. But this would offere one possibility of automation.

Azure Automation

Current Azure CLI is not support but it's under review at the time of writing. See this link for the uservoice suggestion and vote on it if this a desired feature.

Azure Functions

As far as I can tell, there's no way to run Azure CLI on an Azure Function.

Azure Scheduler

Azure Scheduler is a service for only invoking code hosted elsewhere. This would still mean you need to host your code somewhere else, i.e. cloud or on-premises, then have the scheduler run it for you.

Triggering mechanisms that are supported are:

  • HTTP, HTTPS
  • Azure Storage queue
  • Azure Service bus queue
  • Azure Service bus topic

Azure Cloud Shell

It's a shell that contains tools needed for running commands and scripts without the needed to locally install anything. Scheduling anything, using cron does not seem to be possible.

Suggestion

At the moment, if you want to script something, and run it in the cloud, I recommend you have a look at PowerShell. Running PowerShell scripts, with a time trigger is possible on Azure Functions and support adding your own custom modules as well.

If you need to use Azure CLI and serverless, then you could run it inside of a Docker container and host the container in the cloud, e.g. in an Azure Container Instances. See this link on how to create it.

3
votes

You can use an AzureDevOps pipeline for this:

pr: none 
trigger: none
schedules:
- cron: "0 7 * * 1-5"
  displayName: "Working-hours (7 am UTC, Monday to Friday)"
  always: true
  branches:
    include:
    - master
    
steps:
  - task: AzureCLI@2
    inputs:
      azureSubscription: 'PipelineServiceConnection'
      scriptType: 'bash'
      scriptLocation: 'inlineScript'
      inlineScript: 'az --version'

What you have to do is create a new pipeline via the AzureDevops Pipelines tab:

  • Choose the location for the pipeline script
  • Choose the branch
  • Choose the pipline template: "Starter pipeline"
  • Paste the above yaml code in the editor
  • Save and run the pipeline

The yaml script uses a service connection called 'PipelineServiceConnection', to create such service connection see: https://docs.microsoft.com/en-us/azure/devops/pipelines/library/connect-to-azure?view=azure-devops

More info about AzureCLI task: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-cli?view=azure-devops

Configure scheduled pipelines: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/scheduled-triggers?view=azure-devops&tabs=yaml