Tearing down a test environment (Azure)

In this link there is a section about Tearing down a test environment, but it’s not very clear how this can be achieved.

I see the section about how to create a recurring deployment (scheduled trigger?) but there is only the option to deploy the latest release or deploy a new release. Where can I enter those power-shell scripts to delete the resource group (e.g. each day at 7PM)?

Hi, Thanks for getting in touch, I’m sorry for the delay in getting back to you on this one. One common way we have seen when we want to tear down a test environment is to create two additional environments. I have put together a quick guide on one way to achieve this here:

Step 1. Login to the Octopus Portal and create Two environments: “Infrastructure Create” and “Infrastructure Destroy”

Step 2. Create a new project called “Infrastructure”

Step 3. Modify your lifecycles so that you can manually deploy to each environment.

  • Navigate to Library > Lifecycles
  • Click Add Lifecycle
  • Name it something like Test Infrastructure
  • Under Phases, create a new one called Create and provide the environment Infrastructure Create, set it to manually.
  • Under Phases, create a new one called “Destroy” and provide the environment Infrastructure Destroy, set it to manually.

Here is a screenshot of how the new Lifecycle could look:

Step 4. Define your steps to create a resource group with ARM Template.

  • In my example,
    • Deployment Step 1 uses the New-AzureRmResourceGroup to create an empty resource group if it doesn’t exist.
      • Under conditions, ensure that it only runs for the Infrastructure Create environment.
    • Deployment Step 2 uses the Octopus Step template Deploy an Azure Resource Manager Template to deploy an ARM template
      • Under contitions, ensure that it only runs for the “Infrastructure Create” environment.
    • Deployment Step 3 is where you could implement your PowerShell scripts to delete the resource group. here “Remove-AzureRmResourceGroup” to simply delete the entire resource group that was created in step 1 (But only if it exists).
      • Under conditions, ensure that it only runs for the “Infrastructure Destroy” environment.
        Step 5. Finally under lifecycles, click “Change” to “Test Infrastructure” lifecycle.

Here is a screenshot of how the deployment process could look:

Step 6. Now we can proceed to automatically deploly the latest release into the Infrastructure Destroy environment at 7:00 PM every day.

  • Under the Infrastructureproject select Triggers
  • click Add Trigger dropdown and choose Scheduled trigger
  • Under Name call it something like Auto-Destroy daily at 7:00PM
  • Under the Trigger Schedule section, I’ve set this to the desired schedule, daily at 7:00 PM
  • Under the Trigger Action selection, select the desired release - I’ve chosen to deploy the lateset
  • Under both the Source environment and Destination environment choose the same environment - I’ve chosen Infrastructure Destroy.

I’m interested to hear if this has been helpful. Please feel free to keep in touch if you have any further questions!

Kind regards,
Lawrence.

This is extremely helpful, thanks for the detailed answer.

Hopefully in the future there will be an easier way to achieve this :slight_smile: