Running parallel testing jobs with GitLab Runner for faster feedback
TypeScriptTo run parallel testing jobs with GitLab Runner, you'll want to automate the setup of GitLab Runners which are responsible for executing the jobs in your CI/CD pipeline. For that, we will use Pulumi to define the infrastructure as code, specifically we will define a GitLab Runner. This will allow you to deploy and manage your infrastructure in a repeatable and predictable manner.
First, we need to install the GitLab Pulumi provider by running
npm install @pulumi/gitlab
. This will allow us to interact with the GitLab API through Pulumi.We'll then create a GitLab Runner resource and configure it to run parallel jobs. Each runner can be tagged to handle specific jobs or types of jobs, and you can adjust the concurrency settings to allow multiple jobs to be run in parallel. When setting up the runner, you should ensure that it has sufficient resources (CPU, memory) to handle the workload.
Here is a TypeScript program that sets up a GitLab Runner with Pulumi:
import * as pulumi from "@pulumi/pulumi"; import * as gitlab from "@pulumi/gitlab"; // Create a new Pulumi project for your GitLab Runner const gitlabRunner = new gitlab.Runner("example-runner", { description: "Example GitLab Runner", active: true, // The runner is active and able to run jobs protected: false, // The runner is not protected tagList: ["docker", "linux"], // List of tags for selecting runner for jobs locked: false, // The runner is not locked to current project runUntagged: true, // The runner can execute untagged jobs accessLevel: "not_protected", // The runner is not protected, which means all projects can use it if allowed by the tags maximumTimeout: 3600, // Maximum timeout set for jobs run by this runner }); // Export the runner's token for further configuration (eg. setup as a Docker container) export const runnerToken = gitlabRunner.token;
Please note that to create a runner, you need to have a token. This token is used to register the runner with your GitLab instance. In the example above, the token is exported so it can be used outside Pulumi for the actual runner registration. The registration itself involves running the
gitlab-runner register
command on the host where the runner will live which is out of the scope of this code block but should be done post-provisioning.Make sure to adjust the
tagList
andmaximumTimeout
as per your testing jobs needs. Tags are crucial when you want to assign specific runners to handle specific types of jobs. ThemaximumTimeout
defines how long a job can run before it is considered as hung and is stopped by GitLab.Each instance of
gitlab.Runner
represents a potential GitLab CI/CD runner capable of executing multiple jobs in parallel, depending on the available resources and its configuration. By default, GitLab Runners pick up jobs using a FIFO queue. To optimize for parallel job execution, you might consider enabling autoscaling runners or implementing a scheduler that decides which job to pick up next based on your specific logic or priority (e.g., shorter jobs first to reduce feedback time).GitLab Runner documentation could be referred for more details and configuration options available for a Runner.
Before running this code with Pulumi, ensure you have:
- A GitLab instance that you can access and have sufficient permissions.
- Setup Pulumi with the required credentials to access your GitLab instance.
After running the Pulumi program, you should proceed with the GitLab Runner registration process as outlined in GitLab's documentation. This involves executing a command on the machine you intend to use as a runner and providing it with the registration token you received from the Pulumi program.