microsoft / azure-pipelines-yaml

Azure Pipelines YAML examples, templates, and community interaction
MIT License
1.19k stars 924 forks source link

Publishing artifacts from a failed step #608

Open frerikandriessen opened 3 months ago

frerikandriessen commented 3 months ago

I have the following usecase:

I have a job that runs tests (could be unit-tests, or a vulnerability scanner, you name it) of which I want to publish the resulting report. When this job runs, and we get to the publish step, there are 3 possible options:

  1. The tests have run successfully. The report should be published
  2. The tests have run but failed. The report should be published
  3. A step somewhere before the test-running step has failed. Thus the tests have not run. There is no report to publish and thus this publish step should not run.

What is your advice/pattern in this (imo) pretty normal usecase? Or am I mistaken and is this not a normal usecase / am I using the tool wrong?

I would have expected to be able to use a condition to refer to the specific test-step, and to only run the publish-step when this test-step has been run. Or even better, to be able to check in the condition if the report exists. As far as I understand it, both of these options are not possible.

I'd be happy to hear what your suggested approach is here.

aleksandrlevochkin commented 2 months ago

Hi @frerikandriessen, thank you for your question.

I would have expected to be able to use a condition to refer to the specific test-step, and to only run the publish-step when this test-step has been run.

You can specify a condition for a step based on the variable created from another step: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml%2Cstages#use-the-pipeline-variable-created-from-a-step-in-a-condition-in-a-subsequent-step

Or even better, to be able to check in the condition if the report exists.

I am not sure if it's helpful in your particular case, but you may check out conditional expressions: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#conditional-insertion