Skip to content

Warning

You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?

Dynamic Preview Environments using Humanitec

Actions
Dynamically create deleted preview environments using Humanitec
v1.1.0
Star (2)

Dynamic Preview Environments using Humanitec

This GitHub action allows you to dynamically create and deleted preview environments using the Humanitec Platform Orchestrator.

Inputs

Note
The preview environment is cloned from the base-env specified in the action inputs (defaults to development if not specified). Ensure the source environment id, base-env, is up-to-date before creating preview environments, otherwise they may be cloned from an old version.

  • humanitec-token (required), Humanitec API token
  • humanitec-org (required), The name of the Humanitec org
  • humanitec-app (required), The name of the Humanitec app
  • action (required), The action to be performed (create, notify or delete)
  • base-env (optional), The source environment id, defaults to development if not specified.
  • image (optional), The image of the workload that should be deployed, registry.humanitec.io/${humanitec-org}/${GITHUB_REPOSITORY} by default.
  • environment-url-template (optional), Provide a custom mustache template for the environment url, https://app.humanitec.io/orgs/{{orgId}}/apps/{{appId}}/envs/{{envId}} by default.
  • humanitec-api (optional), Use a different Humanitec API host.
  • github-token (optional), GitHub token used for commenting inside the PR.

Outputs

  • environment-url : Rendered URL of the preview environment.

Example usage

Create Preview Environment

Create preview environments when a PR is created and remove it again once the PR is closed.

.github/workflows/create-preview-env.yml

name: Create Preview environment

on:
  pull_request_target:
    types: [ opened, reopened ]

jobs:
  create-preview-env:
    runs-on: ubuntu-latest

    steps:
      - uses: humanitec/preview-envs-action@v1
        with:
          humanitec-token: ${{ secrets.HUMANITEC_TOKEN }}
          humanitec-org: my-org
          humanitec-app: my-app
          action: create
          github-token: ${{ secrets.GITHUB_TOKEN }}

Delete Preview Environment

.github/workflows/delete-preview-env.yml

name: Delete Preview environment

on:
  pull_request_target:
    types: [ closed ]

jobs:
  delete-preview-env:
    runs-on: ubuntu-latest

    steps:
      - uses: humanitec/preview-envs-action@v1
        with:
          humanitec-token: ${{ secrets.HUMANITEC_TOKEN }}
          humanitec-org: my-org
          humanitec-app: my-app
          action: delete
          github-token: ${{ secrets.GITHUB_TOKEN }}

Notify Humanitec

Add the following snipped after your CI step notifying Humanitec about the newly pushed image (commonly the build-push-to-humanitec step):

- uses: humanitec/preview-envs-action@v1
  with:
    humanitec-org: my-org
    humanitec-app: my-app
    action: notify
    github-token: ${{ secrets.GITHUB_TOKEN }}
    environment-url-template: https://dev-{{envId}}.my-domain.app

Get Preview Environment URL

All actions above return the output parameter environment-url. If you just want to retrieve the environment url you can use this step:

- name: Get Preview Environment
  id: preview-env
  uses: humanitec/preview-envs-action@mst-wal-7302-test-on-pr
  with:
    humanitec-org: humanitec-labs
    humanitec-app: humanitec-frontend
    action: get-environment-url
    github-token: ${{ secrets.GITHUB_TOKEN }}
    environment-url-template: "https://{{envId}}-app.humanitec.io"
- name: Print preview environment url
  run: |
  echo "This is the preview environment url: ${{ steps.preview-env.outputs.environment-url }}"

Development

Running the tests requires an Humanitec account. Once this is created, the following environment variables need to be configure:

  • HUMANITEC_ORG
  • HUMANITEC_TOKEN
  • HUMANITEC_APP

Dynamic Preview Environments using Humanitec is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.

About

Dynamically create deleted preview environments using Humanitec
v1.1.0

Dynamic Preview Environments using Humanitec is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.