Tags trigger not working in azure pipelines

冷暖自知 提交于 2021-01-01 04:22:10

问题


I have the exact same pipeline in two different branches of the same repository, stage and prod.

I'd like the pipeline on branch stage to run when a tag starting with stage@ is created (e.g. stage@1.0.1); similarly, I'd like the pipeline on branch prod to run when a tag starting with prod@ is created (e.g. prod@1.0.1).

In branch stage, trigger is defined like this:

# .azure-pipelines.yml [refs/branches/stage]

trigger:
  tags:
    include:
      - stage@*

In branch prod, trigger is defined like this:

# .azure-pipelines.yml [refs/branches/prod]

trigger:
  tags:
    include:
      - prod@*

Now I created a pipeline on Azure DevOps named [Deploy to STAGE] from the file in the stage branch, and one named [Deploy to PROD] from the file in the prod branch.

I tried creating a tag:

$ git branch
  master
  prod
* stage

$ git tag -a stage@1.0.6 -m "stage@1.0.6"
$ git push --tags

I expected the [Deploy to STAGE] pipeline to start, but both pipelines started instead:

Am I missing something? Isn't the trigger supposed to only include tags matching the defined pattern? How do I correct the trigger to achieve the described flow?


回答1:


The two pipelines Deploy to STAGE and Deploy to Prod are two identical pipelines. Though you choose the file from branch Prod when creating pipeline Deploy to Prod, this pipeline will apply to Stage branch too. This is because both the pipelines use the same azure-pipelines.yml file and this file exists on both stage and prod branch.

From the screenshot of the build runs, You can see the two pipelines were building the exact same stage tag. If you add a tag to prod branch, you will see both the pipeline will be triggered too.

Actually you only need to create either one of the two pipelines. Both of they will apply to prod and stage branch. So you can just delete pipeline Deploy to Prod. When you push a tag to prod branch. You will notice the pipeline Deploy to STAGE is building the prod tag.

If you have to create two pipelines. You need to rename the azure-pipelines.yml file in prod branch or create a new yml file with the some contents and with a different name. And create pipeline Deploy to PROD from this new yml file.

If you choose to create a new yml file to create pipeline Deploy to PROD, you need to disable the trigger (trigger: none) in the original azure-pipelines.yml file in prod branch, so that pipeline Deploy to Stage will not be triggered to build prod tag when tag is pushed to prod branch.

In summary, if you want to create different pipelines for different branches, you will have to create different pipeline yml files.




回答2:


I tried to reproduce you case but I couldn't manage to get the same result.

So for pipeline in stage

trigger:
  tags:
    include:
      - stage@*

steps:
- powershell: Write-Host "stage"

and pipeline in prod

trigger:
  tags:
    include:
      - prod@*

steps:
- powershell: Write-Host "prod"

I got this:

However, I would recommend you different approach as this could prone to error as on merging both branches you always need to have in mind to do not touch this files. Create two separate branches one with trigger for stage tag and one with trigger for prod tag. Extract common stuff to template and if some arguments in tasks are different please make them as parameters in template. About templates you can read here.



来源:https://stackoverflow.com/questions/64569496/tags-trigger-not-working-in-azure-pipelines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!