How to use Contexts

Last updated: June 29, 2024

Context in Spacelift is a collection of configuration elements such as environment variables, mounted files, and hooks. It can be managed separately and attached to multiple projects (stacks or modules) as needed. This ensures that configurations are not duplicated across projects, making management easier and more efficient.

For the full detailed guide, visit the Context Documentation.

Benefits of Using Contexts

  1. Centralized Management: Manage common configuration elements in one place and reuse them across multiple projects.

  2. Consistency: Ensures that all projects using the same context have consistent configurations.

  3. Security: Sensitive data can be stored as secret environment variables or mounted files, ensuring they are not exposed.

Steps to Set Up Contexts

  1. Creating a Context:

    • Navigate to the Contexts screen and click the Create context button.

    • Fill in the context details:

      • Name: Informative and concise to fit in dropdown menus.

      • Description: Optional, supports Markdown for detailed explanations.

      • Labels: Useful for organizing contexts and enabling auto-attachments.

    • Note: Spacelift generates an immutable slug based on the original name, which serves as a unique identifier.

    More details are available in the Context Creation Documentation.

  2. Attaching Contexts to Projects:

    • After creating a context, navigate to the project's Contexts tab.

    • Select the context from the dropdown and attach it.

    • Optionally, use autoattach labels to automatically attach contexts to multiple projects based on shared labels.

    Refer to the Context Attachment Documentation.

  3. Setting Up Environment Variables:

    • Navigate to the context view and click the Environment tab.

    • Add environment variables by specifying the key and value, and mark them as secret if needed.

    • Note: Changes to environment variables will affect all projects using this context.

    More details are available in the Environment Variables Documentation.

  4. Adding Mounted Files:

    • Navigate to the Mounted files tab in the context view.

    • Add new files by providing the name, description, and content. Mark them as secret if necessary.

    • Note: Secret files can only be accessed by runs and tasks, not through the web GUI or API.

    Refer to the Mounted Files Documentation.

  5. Adding Hooks:

    • Navigate to the Hooks tab in the context view.

    • Define hooks to run before or after different project phases.

    • Add commands and arrange their order using drag-and-drop.

    More details are available in the Hooks Documentation.

Terraform Provider for Context

Spacelift's Terraform provider allows you to manage contexts programmatically, offering a powerful way to automate and version control your context configurations.

To use contexts with the Terraform provider:

  1. Define Contexts in Terraform:

    • Use the spacelift_context resource to manage contexts.

    • Example:

      resource "spacelift_context" "example" {
        name        = "example-context"
        description = "Example context for shared configuration"
      }
  2. Manage Environment Variables and Mounted Files:

    • Define environment variables and mounted files within the context.

    • Example:

      resource "spacelift_context_environment_variable" "example" {
        context_id = spacelift_context.example.id
        key        = "EXAMPLE_KEY"
        value      = "example_value"
        sensitive  = true
      }
      
      resource "spacelift_context_mounted_file" "example" {
        context_id = spacelift_context.example.id
        path       = "/etc/example.conf"
        content    = "example content"
        sensitive  = true
      }

  3. Attach Contexts to Projects:

    • Use the spacelift_stack_context_attachment resource to attach contexts to stacks.

    • Example:

      resource "spacelift_stack_context_attachment" "example" {
        stack_id   = spacelift_stack.example.id
        context_id = spacelift_context.example.id
        priority   = 0
      }

For more detailed instructions and examples, refer to the Terraform Provider Documentation.

Additional Information

  • Editing Contexts: You can edit details, environment variables, mounted files, and hooks from the context view.

  • Deleting Contexts: Deleting a context will automatically detach it from all projects.

  • Priority: Contexts attached to a stack are sorted by priority, which establishes precedence in case of conflicts.

For more detailed instructions and examples, refer to the full Context Documentation.