Search code examples
azure-devopsazure-pipelinesazure-pipelines-release-pipelineazure-artifactsazure-pipelines-release-task

Azure DevOps Release Pipeline - How to get the source code that was used to create the build artifact?


I have a continuously triggered Azure DevOps release definition that deploys a compiled Angular app to a web server and also runs Cypress e2e tests. The Cypress tests must run against the source code, so that means I need an artifact that is able to reference the same commit that was used to create the compiled app.

I created a GitHub artifact that gets the source code, but I can't figure out how to automatically change the branch/commit to whatever was used for the compiled app (it could be any branch and the names are not known ahead of time). Azure forces me to enter a hard-coded branch name and it does not accept wildcards or variables.

If I could simply use the variable ${Release.Artifacts.{alias}.SourceBranchName} for the default branch, I think I'd achieve my goal. Since Azure doesn't allow this, is there an alternative approach that accomplishes the same thing?

screenshot of Azure DevOps release definition

Note 1: The "Default version" dropdown has an option "Specify at the time of release creation", but that is intended for manual releases and can't be used for triggered ones, so no luck there.

Note 2: I looked into publishing the source code as an artifact, but it currently has almost 70,000 files and it adds more than an hour to the build step, so that also is not an option.


Solution

  • When you use the Release Pipeline artifacts, you are not able to set the pipeline variable in the Default branch field. This field only supports hard-coded.

    is there an alternative approach that accomplishes the same thing?

    The variable:$(Release.Artifacts.{alias}.SourceBranchName) can be used in the Release Pipeline agent job.

    Workaround:

    You can remove the Github artifacts and then add a Command Line task/PowerShell task/ Bash task to run the git command to clone the target repo.

    For example:

    git clone -b $(Release.Artifacts.{alias}.SourceBranchName) GithubRepoURL
    

    PowerShell sample:

    enter image description here

    In this case, the script will use the same branch as Build Artifacts to checkout the source code.