# How to prevent concurrent jobs in Gitlab pipelines

Today I learned how to prevent Gitlab from running jobs concurrently for multiple commits. For example, to prevent multiple deploys for a service from happening at the same time.

You can use a resource group (opens new window) for this purpose. See the introduction docs from 2020 here (opens new window).

To create a resource group, you'll need to add it to your job's definition under the resource_group key:

deploy_job:
  stage: deploy
  script: echo "Your deployment script"
  resource_group: production
1
2
3
4

To specify the order in which the jobs should run, can also set the "process mode" (opens new window) of the resource group. The oldest_first is the more obvious one and probably the one you would want. It forces jobs to be run in the order in which they've been created (oldest to newest).

You'll need to use the API to update the process mode of the resource group after first creating it. For example:

curl --location --request PUT 'https://gitlab.com/api/v4/projects/:project_id/resource_groups/production' \
	--header 'Authorization: Bearer <your token>' \
	--header 'Content-Type: application/x-www-form-urlencoded' \
	--data-urlencode 'process_mode=oldest_first'
1
2
3
4

Newsletter

If you'd like to subscribe to my blog, please enter your details below. You can unsubscribe at any time.

Powered by Buttondown.

Last Updated: 11/20/2023, 10:04:51 AM