Warm tip: This article is reproduced from stackoverflow.com, please click
azure azure-devops azure-pipelines-build-task

Azure Pipeline File-based Trigger and Tags

发布于 2020-03-27 15:42:51

Is it possible to make a build Pipeline with a file-based trigger?

Let's say I have the following Directory structure.

Microservices/
    |_Service A
        |_Test_Stage
            |_Testing_Config
        |_QA_Stage
            |_QA_Config
        |_Prod_stage
            |_Prod_Config
    |_Service B
        |_Test_Stage
            |_Testing_Config
        |_QA_Stage
            |_QA_Config
        |_Prod_stage
            |_Prod_Config

I want to have just one single YAML Build Pipeline File. Based on the Variables $(Project) & $(Stage) different builds are created. Is it possible to check what directory/file initiated the Trigger and set the variables accordingly?

Additionally it would be great if its possible to use those variables to set the tags to the artifact after the run.

Thanks KR

Questioner
Radeonx
Viewed
96
Merlin Liang - MSFT 2020-01-31 16:42

Is it possible to check what directory/file initiated the Trigger and set the variables accordingly?

Of course yes. But there's no direct way since we do not provide any pre-defined variables to store such message, so you need additional complex work around to get that.

#1:

Though there's no variable can direct stores the message like which folder and which file is modified, but you could get it by tracking the commit message Build.SourceVersion via api.

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=5.1

From its response body, you can directly know its path and file:

enter image description here

Since the response body is JSON format, you could make use of some JSON function to parsing this path value. See this similar script as a reference.

Then use powershell script to set these value as pipeline variable which the next agent jobs/tasks could use them.

Also, in your scenario, all of these should be finished before all next job started. So, you could consider to create a simple extension with pipeline decorator. Define all above steps in decorator, so that it can be finished in the pre-job of every pipeline.


#2

Think you should feel above method is little complex. So I'd rather suggest you could make use of commit messge. For example, specify project name and file name in commit message, get them by using variable Build.SourceVersionMessage.

Then use the powershell script (I mentioned above) to set them as variable.

This is more convenient than using api to parse commits body.

Hope one of them could help.