Skip to main content
Version: 1.5

Preview Environments for your Kubernetes Applications

In this section, we'll show you how to automatically create a preview environment for your applications using Okteto Cloud and GitHub Actions.

Pre-Requisites

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're going to use our GitHub Actions for Okteto Cloud.

Creating a preview environment requires performing the following steps:

  1. Login to Okteto Cloud.
  2. Deploy a preview environment in Okteto Cloud.
  3. Update the PR with the URL of the preview environment.

The sample repository configured to use the workflow described above. If you want to use this one 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
on:
pull_request:
branches:
- master

jobs:
preview:
runs-on: ubuntu-latest
steps:
- name: Context
uses: okteto/context@latest
with:
token: ${{ secrets.OKTETO_TOKEN }}

- name: Deploy preview environment
uses: okteto/deploy-preview@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
name: pr-${{ github.event.number }}-cindylopez
scope: personal
timeout: 15m

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.

Learn more about repository secrets in GitHub's official documentation.

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.

GitHub workflows previews

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.

Preview environment available message with URL

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 remove the preview environment after the pull request has been merged.

# preview-closed.yaml
on:
pull_request:
types:
- closed

jobs:
closed:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master

- name: Context
uses: okteto/context@latest
with:
token: ${{ secrets.OKTETO_TOKEN }}

- name: Destroy preview environment
uses: okteto/destroy-preview@latest
with:
name: pr-${{ github.event.number }}-cindylopez

Conclusions

Having automatic preview environments launched along with your pull requests is a great way to do end-to-end testing and 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.