Workspace configuration

Tier: Premium, Ultimate Offering: GitLab.com, Self-managed, GitLab Dedicated
History

You can use workspaces to create and manage isolated development environments for your GitLab projects. Each workspace includes its own set of dependencies, libraries, and tools, which you can customize to meet the specific needs of each project.

Set up workspace infrastructure

Before you create a workspace, you must set up your infrastructure only once. To set up infrastructure for workspaces:

  1. Set up a Kubernetes cluster that the GitLab agent supports. See the supported Kubernetes versions.
  2. Ensure autoscaling for the Kubernetes cluster is enabled.
  3. In the Kubernetes cluster:
    1. Verify that a default storage class is defined so that volumes can be dynamically provisioned for each workspace.
    2. Install an Ingress controller of your choice (for example, ingress-nginx).
    3. Install and configure the GitLab agent.
    4. Point dns_zone and *.<dns_zone> to the load balancer exposed by the Ingress controller. This load balancer must support WebSockets.
    5. Set up the GitLab workspaces proxy.
  4. Optional. Configure sudo access for a workspace.
  5. Optional. Configure support for private container registries.

Create a workspace

History
  • Support for private projects introduced in GitLab 16.4.
  • Git reference and Devfile location introduced in GitLab 16.10.
  • Time before automatic termination renamed to Workspace automatically terminates after in GitLab 16.10.
  • Variables introduced in GitLab 17.1.

Prerequisites:

  • You must set up workspace infrastructure.
  • You must have at least the Developer role for the workspace and agent projects.
  • In each project where you want to create a workspace, create a devfile:
    1. On the left sidebar, select Search or go to and find your project.
    2. In the root directory of your project, create a file named devfile. You can use one of the example configurations.
  • Ensure the container images used in the devfile support arbitrary user IDs.

To create a workspace:

  1. On the left sidebar, select Search or go to and find your project.
  2. Select Edit > New workspace.
  3. From the Cluster agent dropdown list, select a cluster agent owned by the group the project belongs to.
  4. From the Git reference dropdown list, select the branch, tag, or commit hash GitLab uses to create the workspace.
  5. In Devfile location, enter the path to the devfile you use to configure the workspace. If your devfile is not in the root directory of your project, specify a relative path.
  6. In Workspace automatically terminates after, enter the number of hours until the workspace automatically terminates. This timeout is a safety measure to prevent a workspace from consuming excessive resources or running indefinitely.
  7. In Variables, enter the keys and values of the environment variables you want to inject into the workspace. To add a new variable, select Add variable.
  8. Select Create workspace.

The workspace might take a few minutes to start. To open the workspace, under Preview, select the workspace. You also have access to the terminal and can install any necessary dependencies.

Configure support for private container registries

History

To use images from private container registries:

  1. Create an image pull secret in Kubernetes.
  2. Add the name and namespace of this secret to the GitLab agent configuration.

Configure sudo access for a workspace

History

Prerequisites:

  • Ensure the container images used in the devfile support arbitrary user IDs. Sudo access for a workspace does not mean that the container image used in a devfile can run with a user ID of 0.

A development environment often requires sudo permissions to install, configure, and use dependencies during runtime. You can configure secure sudo access for a workspace with:

With Sysbox

Sysbox is a container runtime that improves container isolation and enables containers to run the same workloads as virtual machines.

To configure sudo access for a workspace with Sysbox:

  1. In the Kubernetes cluster, install Sysbox.
  2. In the GitLab agent for workspaces:

With Kata Containers

Kata Containers is a standard implementation of lightweight virtual machines that perform like containers but provide the workload isolation and security of virtual machines.

To configure sudo access for a workspace with Kata Containers:

  1. In the Kubernetes cluster, install Kata Containers.
  2. In the GitLab agent for workspaces:

With user namespaces

User namespaces isolate the user running inside the container from the user on the host.

To configure sudo access for a workspace with user namespaces:

  1. In the Kubernetes cluster, configure user namespaces.
  2. In the GitLab agent for workspaces, set use_kubernetes_user_namespaces and allow_privilege_escalation to true.

Connect to a workspace with SSH

History

Prerequisites:

To connect to a workspace with an SSH client:

  1. Get the external IP address of your gitlab-workspaces-proxy-ssh service:

    kubectl -n gitlab-workspaces get service gitlab-workspaces-proxy-ssh
    
  2. Get the name of the workspace:

    1. On the left sidebar, select Search or go to.
    2. Select Your work.
    3. Select Workspaces.
    4. Copy the name of the workspace you want to connect to.
  3. Run this command:

    ssh <workspace_name>@<ssh_proxy_IP_address>
    
  4. For the password, enter your personal access token with at least the read_api scope.

When you connect to gitlab-workspaces-proxy through the TCP load balancer, gitlab-workspaces-proxy examines the username (workspace name) and interacts with GitLab to verify:

  • The personal access token
  • User access to the workspace

Update your workspace container image

To update your runtime images for SSH connections:

  1. Install sshd in your runtime images.
  2. Create a user named gitlab-workspaces to allow access to your container without a password.
FROM golang:1.20.5-bullseye

# Install `openssh-server` and other dependencies
RUN apt update \
    && apt upgrade -y \
    && apt install  openssh-server sudo curl git wget software-properties-common apt-transport-https --yes \
    && rm -rf /var/lib/apt/lists/*

# Permit empty passwords
RUN sed -i 's/nullok_secure/nullok/' /etc/pam.d/common-auth
RUN echo "PermitEmptyPasswords yes" >> /etc/ssh/sshd_config

# Generate a workspace host key
RUN ssh-keygen -A
RUN chmod 775 /etc/ssh/ssh_host_rsa_key && \
    chmod 775 /etc/ssh/ssh_host_ecdsa_key && \
    chmod 775 /etc/ssh/ssh_host_ed25519_key

# Create a `gitlab-workspaces` user
RUN useradd -l -u 5001 -G sudo -md /home/gitlab-workspaces -s /bin/bash gitlab-workspaces
RUN passwd -d gitlab-workspaces
ENV HOME=/home/gitlab-workspaces
WORKDIR $HOME
RUN mkdir -p /home/gitlab-workspaces && chgrp -R 0 /home && chmod -R g=u /etc/passwd /etc/group /home

# Allow sign-in access to `/etc/shadow`
RUN chmod 775 /etc/shadow

USER gitlab-workspaces

Troubleshooting

When working with workspaces, you might encounter the following issues.

Error: Failed to renew lease

You might not be able to create a workspace due to a known issue in the GitLab agent for Kubernetes. The following error message might appear in the agent’s log:

{"level":"info","time":"2023-01-01T00:00:00.000Z","msg":"failed to renew lease gitlab-agent-remote-dev-dev/agent-123XX-lock: timed out waiting for the condition\n","agent_id":XXXX}

This issue occurs when an agent instance cannot renew its leadership lease, which results in the shutdown of leader-only modules including the remote_development module. To resolve this issue, restart the agent instance.

Error: No agents available to create workspaces

When you create a workspace in a project, you might get the following error:

No agents available to create workspaces. Please consult Workspaces documentation for troubleshooting.

To resolve this issue:

  • If you do not have at least the Developer role for the workspace and agent projects, contact your administrator.
  • If the ancestor groups of the project do not have an allowed agent, allow an agent for any of these groups.
  • If the remote_development module is disabled for the GitLab agent, set enabled to true.