Questions about using Preview Environments

When do Preview Environments get removed?

We have a background garbage collection job which removes Preview Environments after the Pull Request is closed/merged. You can run it any time you like via the jx preview gc command

jx preview gc

You can also view the current previews via jx preview get:

jx preview get

and delete a preview by choosing one to delete via jx preview destroy:

jx preview destroy

How do I access the preview namespace or URL?

After the jx preview create step in a pull request pipeline you can access a number of preview environment variables.

For details see how to add additional preview steps

How do I add other services into a Preview?

see how to add resources to your previews

How do I configure Secrets in a Preview?

Previews are installed via helmfile sync unlike the usual approach for promoted environments like Dev, Staging and Production as the changes have not yet been released.

This means the usual conversion from Secret resources to ExternalSecrets is not enabled for previews.

So to add Secret resources into your preview namespace try one of the following:

  • if you can use dynamically generated Secret values then just use the usual helm approach to creating Secret resources
  • if you need configured Secrets to access external services then you can copy them from the jx namespace. We copy labelled secrets by default in the preview helmfile.yaml
    • you just need to add the label: to your Secret in the jx namespace
    • see how to add a new Secret
  • you can add an ExternalSecret resource to your preview helmfile.yamlas a nested chart like this which will then use kubernetes external secrets to populate the Secret resources from the external secret store.
  • add a Job, init-container or helmfile hook in preview/helmfile.yaml to generate the Secret dynamically using whatever custom logic you prefer

How do I inject the Preview URL into other services?

The preview namespace and URL are available as environment variables after the preview has been created.

However if you want to pass in the preview URL to other charts included in your preview via the preview/helmfile.yaml file you can:

  • use the service URL rather than ingress which is much simpler and does not depend on the namespace or domain. e.g. just use http://my-app-name
  • add a values.yaml.gotmpl file for the chart to configure its values you wish to inject the URL into and pass in whatever yaml is required to configure the preview URL using the following expression. The example below uses someValue as the key to specify the URL but use whatever yaml keys your chart expects:
someValue:  "{{ requiredEnv "APP_NAME" }}-pr{{ requiredEnv "PULL_NUMBER" }}.{{ .Values.jxRequirements.ingress.domain }}"

Last modified August 18, 2021: chore: fix URL (4b4d47a7a2)