Deploy Pull Requests to cluster namespaces
In the previous guide we have seen how you can handle deployments to predefined environments (QA/Staging/production).
Another type of environments that you should manage is dynamic temporary environments for each pull request. For this type of environments it is best if you create dynamically an environment when a Pull Request is created and tear it down when the Pull Request is closed.
This way each developer is working in isolation and can test their feature on its own. This pattern comes in contrast with the traditional way of reusing static preexisting environments.
With Kubernetes you don’t need to book and release specific test environments any more. Testing environments should be handled in a transient way.
Preview environments with Kubernetes
There are many ways to create temporary environments with Kubernetes, but the simplest one is to use
different namespaces, one for each pull request. So a pull request with name
be deployed to a namespace called
fix-db-query, a pull request with name
JIRA-1434 will be deployed to a namespace called
JIRA-1434 and so on.
The second aspect is exposing the environment URL so that developers and testers can actually preview the application deployment either manually or via automated tests.
The two major approaches here are with host-based URLs or path based URLs.
- In host based urls, the test environments are named
pr2.example.comand so on
- with path based URLs, the test environments are named
example.com/pr2and so on
Both approaches have advantages and disadvantages. Path based URLs are easier to setup but may not work with all applications (since they change the web context). Host based URLs are more robust but need extra DNS configuration for the full effect.
In Kubernetes clusters, both ways can be setup via an Ingress.
The example application
The application we will use can be found at https://github.com/codefresh-contrib/unlimited-test-environments-source-code. It is a standard Java/Spring boot application with the following characteristics.
- It has integration tests that can be targeted at any host/port. We will use those tests as smoke test that will verify the preview environment after it is deployed
- It comes bundled in a Helm chart
- It has an ingress configuration ready for path based URLs
We are using the Ambassador gateway as an ingress for this example, but you can use any other compliant Kubernetes Ingress.
Here is the ingress manifest
The path of the application is configurable and can be set at deploy time.
Creating preview environments for each pull request
Each time a Pull Request is created we want to perform the following tasks:
- Compile the application and run unit tests
- Run security scans, quality checks and everything else we need to decided if the Pull request is valid
- Create a namespace with the same name as the pull request branch. Deploy the pull Request and expose it as a URL that has the same name as the branch as well
Here is an example pipeline that does all these tasks:
This pipeline has the following steps
- A clone step to fetch the source code of the application
- A freestyle step that runs Maven for compilation and unit tests
- A build step to create the docker image of the application
- A step that scans the source code for security issues with Snyk
- A step that scans the container image for security issues with trivy
- A step that runs integration tests by launching the app in a service container
- A step for Sonar analysis
- A step that clones a second Git repository that has the Helm chart of the app
- A step that deploys the source code to a new namespace.
- A step that adds a comment on the Pull Request with the URL of the temporary environment
- A step that runs smoke tests against the temporary test environment
Note that the integration tests and security scans are just examples of what you can do before the Pull Request is deployed. You can insert your own steps that check the contents of a Pull Request.
Here is the whole YAML definition
The end result of the pipeline is a deployment on the path that has the same name as the pull request branch. For
example if my branch is named
demo then a demo namespace is created on the cluster and the application
is exposed on the
The environment is also mentioned as a comment in the Pull Request UI in Github:
As explained it the previous guide for Pull Requests, we want to make this pipeline applicable only to PR open event and PR sync events (which capture commits that happen on an existing pull request).
Therefore you need to setup your triggers with the same checkboxes shown in the picture above.
Cleaning up temporary environments
Creating temporary environments is very convenient for developers but can be very costly for your infrastructure if you use a cloud provider for your cluster. For cost reasons and better resource utilization it is best if temporary environments are destroyed if they are no longer used.
While you can run a batch job, that automatically deletes old temporary environments, the optimal approach is to delete them as soon as the respective Pull Request is closed.
We can do that with a very simple pipeline that has only one step:
Here is the pipeline definition:
The pipeline just uninstalls the Helm release for that namespace and then deletes the namespace itself.
To have this pipeline run only when a Pull Request is closed here is how your trigger should look:
Notice that with this setup the pipeline will run when the pull request was closed regardless of whether it was merged or not (which is exactly what you want as in both cases the test environment is not needed anymore).
Seeing all environments in the Codefresh GUI
You can combine the pipeline above with any Codefresh GUI dashboard if you want to see all your temporary environments in a single view.