Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Devops YAML Pipeline - Clean up dynamic deployments

Our current pipeline deploys a new instance of our app for every new branch created on azure repos, just like review apps on Heroku or Gitlab. The creation part went smooth, but I'm not sure what to do with the orphaned resources and deployment once the branch is deleted (hopefully by an accepted pr).

Deleting them manually is not an option and there I can't find a trigger in the documentation for branch deletes.

The only option I can see right now is to create a scheduled job for the master branch(since it always exists) with a bash script that compares the list of deployed apps and existing branches and cleans up the resources.

Is it my only option, or is there another way without a fairly complex, all-access, destroy machine?

like image 446
István Ignácz Avatar asked Sep 13 '25 10:09

István Ignácz


1 Answers

So did a little investigation dumping all enviroment vars to Notepad++ and using the compare plugin i realized that when a PR is accepted 2 env variables are different.

First of the initial variable "BUILD_REASON" during a push is set to "IndividualCI", but with the "BUILD_SOURCEBRANCH" set to "refs/heads/feature/******". When a pull request is initiated the "BUILD_REASON" changes to "pullrequest" and "BUILD_SOURCEBRANCH" to "refs/pull/***".

Finally when a PR is accepted the variables change to "BUILD_REASON" = "IndividualCI" and "BUILD_SOURCEBRANCH" = "refs/heads/master".

Once i figured out this i could create stage that have the following conditions:

- stage: CleanUp
  displayName: 'CleanUp'
  dependsOn: Test
  condition:  and(succeeded(), in(variables['Build.Reason'], 'IndividualCI'),in(variables['Build.SourceBranchName'], 'master'))

The above stage will be triggered when PR is accepted so to cleanup resources created during PR :-) havnt tested all the way but seems to do the job.

like image 164
dahund Avatar answered Sep 15 '25 01:09

dahund