I needed the ability to automatically build one of several possible build types for testing. I also wanted to just change one variable in the YAML to update the build type. This is an excellent use for pipeline “conditions”.
Conditions can be used to enable a step, job, or stage. In this example, the user can select between three different build types by setting the CG_BUILD_PRODUCT
to a value between 0
and 2
corresponding to release, debug and test submission (which is a modified debug build).
A few lines down in the pipeline “variables” section, I break out the user value into three variables that will be set true if the job needs to be run. Note that for a test submission build, I need to do a debug build to generate build artifacts that are used in the later job.
The first two jobs (release
and debug
) is predicated on a condition being evaluated as true. If the individual build type variable is true (CG_BUILD_RELEASE
or CG_BUILD_DEBUG
), the job will be performed. Note that we use the pipeline expression “equals” (eq
) to test for our desired condition. See the bottom of this page for a link to the Azure Pipeline Expression page.
The final job adds a second requirement be met before the job can start – the debug job must finish successfully (without error). The default for the pipeline logic is that a job or stage runs if it does not depend on any other job or stage, or if all of the jobs or stages that it depends on have completed and succeeded. Since the test job uses the output artifacts of the debug build, we should only run it if the debug was successful.
You do this by adding the name of the dependent job to the dependsOn
parameter and testing that the debug job succeeded (succeeded()
) in the test job condition. If you needed to test multiple job conditions, you could pass the name of the job as a parameter to the succeeded()
expression.
variables: # User modifiable variables. CG_BUILD_PRODUCT: '0' # 0:Release, 1:Debug, 2:TestSubmission # ***** Do not alter these build variables ***** CG_BUILD_RELEASE: ${{ eq(variables.CG_BUILD_PRODUCT, '0') }} CG_BUILD_DEBUG: ${{ in(variables.CG_BUILD_PRODUCT, '1', '2') }} CG_BUILD_TEST: ${{ eq(variables.CG_BUILD_PRODUCT, '2') }} jobs: # Build a ProductX release package for test/QA/customer. - job: Build_ProductX_Release_Packages condition: eq(variables.CG_BUILD_RELEASE, 'true') steps: ... # Build a ProductX debug package for test/QA/customer. - job: Build_ProductX_Debug_Packages condition: eq(variables.CG_BUILD_DEBUG, 'true') steps: ... # Build ProductX Fortify submission from debug artifacts and source. # Also, this job will run only if Build_ProductX_Debug_Packages build succeeded. - job: Build_ProductX_TestSubmission dependsOn: Build_ProductX_Debug_Packages condition: and(succeeded(), eq(variables.CG_BUILD_TEST, 'true')) steps: ...