i don't find any clean solution, in this thread(https://developercommunity.visualstudio.com/idea/365730/prevent-parallel-execution-of-the-same-build-defin.html) there is a solution as job in powershell.
because my agent are on linux, i implemented it in python, using the environement variable on the build agent (i think the variables will be the same on windows plateform, or if it not the case you can adapt it with the "env" part of the task.
the user token of the connection to te API is hidden in a secret variable.
jobs:
- job: ParallelRunPrevention
timeoutInMinutes: 240
displayName: 'prevent parallel execution of the pipeline'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
addToPath: true
architecture: 'x64'
- script: |
pip install azure-devops
displayName: 'install dependency'
- task: PythonScript@0
displayName: 'wait for execution'
env:
TASK_TOKEN_USER: $(user_token)
inputs:
scriptSource: 'inline'
script: |
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v6_0.pipelines.pipelines_client import PipelinesClient
import time
import os
token = os.environ["TASK_TOKEN_USER"]
my_definition_id = int(os.environ["SYSTEM_DEFINITIONID"])
my_build_id = int(os.environ["BUILD_BUILDID"])
my_project = os.environ["SYSTEM_TEAMPROJECT"]
my_organization_url = os.environ["SYSTEM_TASKDEFINITIONSURI"]
credentials = BasicAuthentication('', token)
connection = Connection(base_url=my_organization_url, creds=credentials)
pipeline_c: PipelinesClient = connection.clients_v6_0.get_pipelines_client()
while not len([run for run in pipeline_c.list_runs(project=my_project, pipeline_id=my_definition_id) if
run.state == "inProgress" and run.id <= my_build_id]) == 1:
print("not free, build stack:")
print([run.id for run in pipeline_c.list_runs(project=my_project, pipeline_id=my_definition_id) if
run.state == "inProgress" and run.id <= my_build_id])
time.sleep(20)
print("lets go")