In Azure DevOps, I have a main pipeline consisting of two stages. In the first stage, I generate a YAML file named ‘abc.yaml’ that isn’t stored in the repository. In the subsequent stage of the main pipeline, I aim to execute the generated ‘abc.yaml’. Does Azure DevOps support this scenario? Can I trigger a pipeline which is not in the repository?
2
Answers
Prior to execution, Azure Pipelines expands the templates and evaluates expressions, which is often referred to as compile-time expansion:
So, in other words, the YAML is well known after the first step. You couldn’t generate a YAML file and then have the pipeline pick that up due to the authorization and validation activities that happen in step 3. (Eg, imagine someone approves a deployment and then the pipeline decides to generate other activities that require further approvals).
However, Azure Pipelines has a very rich templating system that allows you to influence the construction of the yaml in the first step.
Consider structuring your pipelines with templates with conditional insertion.
For example:
It is also possible to nest templates:
The above is a sample, but there are many different ways you could structure your templates.
Just as @Jonathan and @bryanbcook mentioned, it is not possible to generate YAML template and execute its stages/jobs/steps in the same single pipeline, because the pipeline expansion is completed during compile time before runtime; therefore, we cannot generate the YAML file during runtime and then go back to compile time to modify the expansion.
For this, would you consider separating the workflow into 2 pipelines as a workaround? You can create 2 pipelines; the downstream
PipelineB
is created based on theabc.yml
inAnotherRepo
, the upstreamPipelineA
is used to modify theabc.yml
file and push it toAnotherRepo
. Thus, whenever new commits on theabc.yml
file are pushed toAnotherRepo
, the CI trigger ofPipelineB
will fire and execute the steps inabc.yml
.Here are the brief steps for your reference.
PipelineB
referencingabc.yml
inAnotherRepo
;PipelineA
to modifyabc.yml
and push the update toAnotherRepo
;PipelineB
will then be triggered by the commit pushed byPipelineA
and execute the step fromJobB
underStageB
inabc.yml
System.AccessToken
of the pipeline service account inPipelineA
to authenticate and push toAnotherRepo
, it is required to confirm the corresponding pipeline service account has the permission toContribute
toAnotherRepo
according to the job authorization scope;Hope the workaround can meet your expectations.