Consider these key questions to determine if ephemeral environments would benefit your workflow:
Do you need isolated testing environments?
Ephemeral environments provide isolated testing spaces for each feature or branch, preventing conflicts between developers and ensuring clean test conditions for every change.
Are your environments complex to set up?
When environments require multiple services, databases, and configurations, ephemeral environments automate this complex process, ensuring consistent setups every time and saving hours of manual configuration.
Do you have high infrastructure costs?
By automatically creating environments when needed and destroying them when work is complete, ephemeral environments can significantly reduce cloud infrastructure costs compared to maintaining multiple permanent environments.
Do you need to test branch-specific changes?
Ephemeral environments create isolated testing spaces that exactly match your branch configuration, making it easy to validate changes in context before merging to the main codebase.
Is your team growing?
As teams scale, coordinating environment usage becomes increasingly challenging. Ephemeral environments eliminate contention by giving each developer or team their own dedicated space to work without interference.
If you answered yes to any of these questions, implementing ephemeral environments with Ctrlplane could significantly improve your development workflow and reduce operational overhead.
When using Ctrlplane for any system, you must first determine what resources represent the existence of your deployment.In this case, we’ll use GitHub pull requests to determine when ephemeral environments should be created and destroyed.This approach means Ctrlplane will follow the PR lifecycle:
Creating the environment when a PR is opened
Destroying it when the PR is merged or closed
To implement this workflow, we’ll create a GitHub workflow that tracks PR state changes and updates resources in Ctrlplane accordingly.Here is what it does:
When a pull request is opened or reopened:
Creates/updates a resource in Ctrlplane with metadata about the PR
Includes details like repository, branch, PR number, and author
Links back to the GitHub PR for reference
When a pull request is closed:
Automatically deletes the corresponding resource from Ctrlplane
Helps clean up any associated ephemeral environments
Once the pull request resource is created, we now determine what pipelines need
to be take give the state of the Pull Request. In our scenario we will create a
GKE cluster for each PR.
.github/workflows/deploy-gke-cluster.yml
Copy
name: Deploy GKE Clusterrun-name: Deploy GKE Cluster [${{ inputs.job_id }}]on: workflow_dispatch: inputs: job_id: description: "Job ID" required: truejobs: create-gke: runs-on: ubuntu-latest steps: - name: Authenticate to Google Cloud uses: google-github-actions/auth@v1 with: credentials_json: ${{ secrets.GCP_SA_KEY }} - name: Set up Cloud SDK uses: google-github-actions/setup-gcloud@v1 - id: ctrlplane uses: ctrlplanedev/ctrlplane/github/get-job-inputs@main with: job_id: ${{ inputs.job_id }} api_key: ${{ secrets.CTRLPLANE_API_KEY }} # the pull request number, this was set in the config body # when creating the pull request resource: # `--config "pullRequest=${{ github.event.pull_request.number }}"` required_outputs: | resource_config_pullRequest - name: Install Ctrlplane CLI uses: ctrlplanedev/cli@main with: workspace: ${{ secrets.CTRLPLANE_WORKSPACE_ID }} api_key: ${{ secrets.CTRLPLANE_API_KEY }} - name: Create GKE Cluster run: | # Create a unique cluster name based on PR CLUSTER_NAME="pr-${{ outputs.ctrlplane.resource_config_pullRequest }}" REGION="us-central1" # Create GKE cluster gcloud container clusters create $CLUSTER_NAME \ --region $REGION \ --num-nodes 1 \ --machine-type e2-standard-2 # Get cluster credentials gcloud container clusters get-credentials $CLUSTER_NAME --region $REGION # Create Ctrlplane resource for the cluster ctrlc api upsert resource \ --version "gke/v1" \ --kind "Cluster" \ --identifier "gke/$CLUSTER_NAME" \ --name "$CLUSTER_NAME" \ --config "project=${{ secrets.GCP_PROJECT_ID }}" \ --config "region=$REGION" \ --config "cluster=$CLUSTER_NAME" \ --metadata "pr=${{ github.event.pull_request.number }}" \ --metadata "branch=${{ github.event.pull_request.head.ref }}" \ --link "GCP Console=https://console.cloud.google.com/kubernetes/clusters/details/$REGION/$CLUSTER_NAME"
If you wanted to speed up the creation of the cluster, you could instead
deploy to a namespace in an existing cluster.