Automatic Environment Creation from Your Git Workflow
Tags: infrastructureascode, git, cloudcomputing, cloud
Published: 2024-04-08 12:58:10

Today, we're thrilled to introduce [env0](https://www.env0.com/) latest feature: Environment Discovery. With it, our users can auto-generate new environments straight from their Git repository while preserving their current directory structure and aligning with industry best practices. 

With Environment Discovery, you can maintain your directory-based structure in your Version Control System (VCS), leveraging it as your primary source of truth. 

This enables you to continue using your existing approval workflows, take advantage of CodeOwners, ensure auditability, and stay aligned with Infrastructure as Code (IaC) best practices — all while benefiting from our platform's robust IaC automation and management capabilities.  

How it works
------------

Environment Discovery automatically creates, plans, and deploys new environments to env0 when you open pull requests in GitHub and other VCS. 

This follows Terraform/Terragrunt established best practices for maintaining a directory-based representation of your environments inside your Git repository. 

The feature works seamlessly, requiring zero changes to your current process or any specific manual action. Once a directory is added via a pull request, env0 will simply detect it and auto-generate a plan and status check, which is then posted on the PR for immediate feedback.

Once the PR has been reviewed, approved, and merged, env0 will apply the plan.  

To see it in action, check down this video below:

‍{% embed https://vimeo.com/923362955 %}

To start, access env0 and go to 'Project Settings' > 'Environment Discovery'. Here, you'll find an introduction to the feature. Click 'Get Started' to proceed.  
  

![Environment Discovery](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbac502ddf384539b25f_nrr3GglHyWV_DV0a-N0tlWDRX1j75cZsGj2eeCeX_DsU8CpuHDqPgyw-dvBedhhgVPUrZD_xm87pcfp1i0GZIc5uRz1DNTG4q3OmOsRCO8wljBL8eM4B-9hIcjSfwDtd4aKr9r9lsP80cZeMYz8ps_0.png)

Next, you'll see a screen where you can select your IaC framework. Choose one of the frameworks (OpenTofu, Terraform, Terragrunt), or select ‘Workflow’ - if you want a [flexible custom option](https://docs.env0.com/docs/workflows) or plan to use multiple frameworks simultaneously.

For the sake of this example, let’s proceed with Terragrunt.

![Terragrunt](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbabfcbc1ddf885bca4e_jOIENjnPTe2dwWcUx1GpETnrfoKyFsGqmCJUmkf9OkEsksrQBmKyEl-7GHu6uAMTAX0xLmYCdn0Dwg2Wcdr9I5rgGpaCSXqwi0tisIBQYHnbbJ6dXIZgqwMuyvscEg2c0qPvzuc-FPiX9zU6BVwLvFo.png)

After clicking ‘Next’, select the VCS you use. env0 supports GitHub, GitLab, Bitbucket, and Azure DevOps, including self-hosted versions of each. 

![Git](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbab3a97a0511b417058_NyJYNlYOwCnd5KCIyzoo1mun-p99b0f9NtgKpWwJN4yvW4r_81LrfmkhZeekpH0qZScccetLs2oM5ut6pzBPxjfM8CyvTotc1O7QKZ9Cra4apjx-XeMSrWBzERcfjmea6Dzjy9UqfU8_7n0x6WN6D0w.png)

Next, you will need to configure env0 so that environments are created based on your Git directory structure.

You can also further customize by:

*   Choosing when redeployment would trigger:
    *   If you choose the option ‘Changes on Template Directory files’, the environment will only plan/deploy changes to the directory matched by the main glob pattern.
    *   If you choose ‘Changes by file filter pattern’, the environment will plan/deploy changes matching the given multi-glob pattern.
*   The environment placement:
    *   If you choose to create the environments in this project, it will not place the environments in the sub-projects.
    *   If you choose to create the environment in the closest matching sub-project path, it will place the new environments in the sub-project that matches the directory path.
*   Workspace naming:
    *   You can choose to use 'default' as the workspace names.
    *   You can derive the workspace naming from the environment's name.

![Configuration](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbabfb65b133c0336474_KY-euJ2bj3l9MXl-lA-nk2YAiRvOq0Aj-NngJmtSSzn5DA9Tkj9xTxBeuwevHEZXQdZRz7JPtVZiXBusQvx9S_ii1k9VHhrLORC2k53ro9aB2Gtnxx2xFXMus1uMPTNtFSoiHjO3rUy7RmbviHDqfnk.png)

With the configuration complete, let’s see the feature in action.

First, let's open a pull request that adds, for example, a MySQL QA Server in the AWS **us-west-2** region for redundancy.

![Pull request](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbab95ac5fe3352a4cb8_C0EhMh9jWEFeuDH0UwuuS-aS8ns4drvWdnUxjJr9Pmq1FyeSffr9yOJcFLmOOwx9Nv40NA8MUfCcwibOvVBlPEFiiosgASnqpkanh7-fc3StcUsxWhpZ9gpDU_pEi-nvvHHWEe-ydrQqhBjU_IqUHO8.png)

env0 will detect directory changes based on the mapping configuration and add a new environment according to the change in the **terragrunt.hcl** file. Finally, it will generate a PR plan.

![PR plan](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbab36b8934197de4999_k1vfP-w4G_vSBnjrioKs-e2t9dRh1qXsW7Eat6Q9AoX1IXLXepanEBhoAa6vENHsiNm3NY6FxiDhHF0lWke7DoHPiWgC5rFwpmOwbPSIHPkN_FNuDA4UP4p3CClVlSCIoYswqlAw30Bi5l8iuVufNAw.png)

The plan is also posted as a comment directly on the PR for author/reviewer visibility:

![author reviewer visibility](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbab14054cca124e994c_yssF0mLHGGlm_3_tQk0FsFi2pwsBTBa0VzUxj8H3ImNUOBB8iehYm0pwLmPBXJQb5lSZGRJ_rMtpS2DEg01Hm6ZGIfIX8zPhAP8sC3ytmNLDHErZ87CVbfT70KtASVkYHHmqs5DfCVWdf5nfQ6jJ-mA.png)

env0 will also ensure the approver can see the plan status.  

![approver can see the plan status](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbab9e43b17f09c06c0f_FJHbLz5Ub9yXmM_bEEZxk7jOVtFntQc2AyfZavW0AsJrIeET3zdY1IvEGy9IU7HkW8A3lKpy24Ko3gkNIbj7TzWbo6Rs-rMQJyEmSNCtuDF6-aWnkyook-RmveKd6tAT1ImkTapokDN6kwNmaZHdDCM.png)

Once the PR is approved and merged, env0 will deploy the new environment.

![env0 will deploy the new environment](https://assets-global.website-files.com/63eb9bf7fa9e2724829607c1/65f9fbab52bb5d62d742a957_ja-d1ygGZzMIjcbFzxzCAmK2zqRQy0wnzl100LNbPYPkR7r_LfMuI-kQ4mokDqghJLlaMWrX9ogeCLexpyISA6eRW_LobA_pd9BUlVlxJNtZnFoyiO6-4Hl5F54OcMzBRjXvuqVHevQvGyutr0Yd-ZI.png)

The newly created environment is now readily available for management alongside your existing env0 deployments. You can now access env0's capabilities within this new environment.

Familiar Workflows and GitOps Best Practices 
---------------------------------------------

Maintaining a well-defined IaC directory structure fosters efficient file management, collaboration, and project maintainability. 

This allows you to align several common best practices, such as the separation of reusable modules from deployed resources and the segmentation by account, region, environment types (dev, staging, etc.), and specific domain (networking, databases).

Environment Discovery enables you to create a folder structure that aligns with the above recommendations, by providing the ability to create environments directly from the Git workflow. 

Moreover, it allows new users to preserve their existing familiar directory structure and continue to leverage useful Git functionalities like version control, auditing, change approvals, etc.

Lastly, this new capability streamlines the environment creation process and smoothens the learning curve of using our platform by enabling users to stay within the familiar GitOps workflow. This has the clear benefit of reducing onboarding time and reducing the likelihood of errors, while also helping expand IaC usage within the organization.

Wrapping Up
-----------

Environment Discovery simplifies IaC management by automatically creating environments directly within your existing GitOps workflow, preserving your directory structure, and adhering to best practices. 

Using it enables streamlined infrastructure creation, reduced onboarding time, and effortless IaC management. 

To learn more about this capability, our solution, and IaC at scale, schedule a [technical demo](https://www.env0.com/demo-request) and see env0 in action!