Preview Environments for your Kubernetes Applications

Using preview environments is a very powerful collaboration practice. At a high level, it means that you provide a live preview of your changes to the different stakeholders to validate.

This is a great way to get your broader team to review your changes, not only at the code level, but also at the design, copy, integration and even customer level.

In this tutorial, we'll show you how to automatically create a preview environments for a Kubernetes application using Okteto Cloud and Github Actions.


For this tutorial, we'll be using this application.

Step 1: Fork the Repository

Start by forking the kubernetes-preview-environment repository with your Github account.

Step 2: Create the Github Workflow

To create the preview environments, we are going to use our Github Actions for Okteto Cloud.

Creating a preview environments requires performing the following steps:

  1. Login to Okteto Cloud.
  2. Create a namespace in Okteto Cloud.
  3. Deploy the base application.
  4. Build the docker containers and update the application.
  5. Update the PR with the URL of the preview environment.

The sample repository is already configured to use the workflow described above. If you want to use this on for your own repositories, all you need to do is to create a .github/workflow folder in the root of your repo, and save your workflow file in it.

The workflow file to create the preview environments looks like this:

# file: .github/workflows/preview.yaml
- master
runs-on: ubuntu-latest
- name: checkout
uses: actions/[email protected]
- name: Login
uses: okteto/[email protected]
token: ${{ secrets.OKTETO_TOKEN }}
- name: Create namespace
uses: okteto/create-[email protected]
namespace: pr-${{ github.event.number }}-cindylopez
- name: Deploy Application
uses: okteto/[email protected]
namespace: pr-${{ github.event.number }}-cindylopez
manifest: manifests
- name: Push API Changes
uses: okteto/[email protected]
working-directory: api
- name: Push Frontend Changes
uses: okteto/[email protected]
working-directory: frontend
- name: comment PR
uses: unsplash/comment-on-[email protected]
msg: "Preview environment available at https://movies-pr-${{ github.event.number }}"

Step 3: Configure your Okteto API Token

If you noticed, the workflow uses the secrets:OKTETO_TOKEN. We do this so we don't have to commit the token into our repo. Before you run this workflow you need to create the OKTETO_TOKEN secret in your repository, with your Okteto API token as the value.

Get your Okteto API token here.

More information on repository secrets is available here.

Step 4: Open a Pull Request

Once your changes are in your repository, go ahead an open a new pull request. Github will receive the event, and it will start your workflow. You can see the status and logs of the workflow in the checks section of the pull request.

Step 5: See your changes live

After a few seconds, the workflow will update the pull request with the URL of your preview environment. Click on it to see the changes in real time.

Every time a new commit is pushed to the branch, the same workflow will run, automatically updating the preview environment.

Step 6: Cleanup

The sample repo also includes a workflow to cleanup the preview environments once the pull request is closed. We recommend you follow this pattern to cleanup resources after you no longer need them.


Having automatic preview environments launched along with your pull requests is a great way to do end-to-end testing, to get feedback from your entire team on any code changes. But that's not all. This is also a fantastic way to enable your sales or support team to spin up demo environments without having to depend on the dev team.

Interested in creating preview environments for your team on demand? Try Okteto Cloud for free today. Interested in running this in your own infrastructure? Okteto Enterprise might just be what you are looking for.