swas_

/3 min read/Developer Experience

Fix Yarn error 137 on GitLab CI

How to fix Yarn exit code 137 when running commands on GitLab CI/CD through GitLab Runner.

Very recently I came across an error when running some nodejs script on GitLab CI/CD. As usual, the commands were running fine on my local machine, but on GitLab Runner it was throwing an error 137 saying…

Killed
error Command failed with exit code 137.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

A quick googling revealed that the issue was indeed related to memory. For some background, here’s my GitLab setup.

Fixing Yarn error 137 on GitLab CI

All we need to do is increase the memory size of the docker runner.

NOTE: The following commands need to be run as super user. So affix sudo before all commands.

STEP 1: Configure GitLab runner with docker executor

Make sure you are using GitLab runner with docker executor. Otherwise the guide will not work. To configure a fresh gitlab runner with docker executor, you can run the following command, optionally after deleting the file /etc/gitlab-runner/config.toml.

sudo gitlab-runner register

Make sure you select the docker executor when asked. More information can be found here.

STEP 2: Increase memory of the runner

Edit the gitlab runner config.toml file by

nano /etc/gitlab-runner/config.toml

It should look something like

[session_server]
  session_timeout = 1800

[[runners]]
  name = "runner-name"
  url = "https://gitlab.or.your.instance.com/"
  token = "SUPER_SECRET_TOKEN"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "ubuntu:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    memory = "128m"
    memory_swap = "256m"
    memory_reservation = "256m"
    cpus = "2"
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

The options we are interested in are the memory, memory_swap and memory_reservation. I would like to give double the value to memory_swap as that of memory. Since my runner was hosted on a 4GB instance, I increased the memories like below.

[[runners]]
  name = "runner-wpquark"
  url = "https://gitlab.com/"
  token = "hkP9m2zhFYAuMS5_7yxQ"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "ubuntu:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
-    memory = "128m"
+    memory = "2048m"
-    memory_swap = "256m"
+    memory_swap = "4096m"
-    memory_reservation = "256m"
+    memory_reservation = "2048m"
    cpus = "2"
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

STEP 3: Restart GitLab Runner

Finally restart gitlab runner service by running

gitlab-runner restart

Now run your CI/CD pipeline again and for most of the cases the job will not fail. If it still gives you error 137, try increasing the memory again.

<- back to writing