Run only one build pipeline task on a schedule

Is it possible to run only one build pipeline task on a schedule and not the whole pipeline on a schedule? I have a task in a build pipeline to generate some report about the pipeline. I would want the task to run once every month.


Solution 1:

Yes.. it is possible in several ways:

  • you can set previously to the task the condition:

     ${{ if eq(variables['isBuild'], true) }}:
    
  • You can configure conditions to run the tasks that you want, depending of whatever, for example in this case using the variable isBuild, defined previously:

     task: PublishBuildArtifacts@1
        displayName: 'Publish artifact: drop'
        inputs:
          PathtoPublish: 'whatever'
          ArtifactName: 'drop'
          publishLocation: 'Container'
        condition: eq(variables['isBuild'], true)
    
  • You can configure conditions to run the stages that you want, so previously you can group your tasks in stages, depending of whatever, for example using in this case in stage the variable isBuild, defined previously:

    stage: Build   
    displayName: 'Build'   
    condition: eq(variables['isBuild'], true)
    

In every example in case of IsBuild be different to true will not be run.

You can find more info in https://github.com/MicrosoftDocs/azure-devops-docs/blob/main/docs/pipelines/process/conditions.md

Also If you want to schedule your task to be executed only 1st day of every month at 07:00:

trigger:

  • master #This is the trigger for other stages. It is not needed for the scheduled stage.

    schedules: cron: '0 7 1 * *'

    displayName: 'Deploy every 1st day of every month at 07:00Z'
    
    branches:
    
      include:
    
       main
    
      always: true
    

Then to ensure that task runs as part of schedule, use the condition:

- stage: 'Test'
  
  displayName: 'Deploy to the test environment'

  dependsOn: Dev

  condition: eq(variables['Build.Reason'], 'Schedule')

For more detail you can to to:

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