How to configure Jenkins with Nginx to avoid "Cannot allocate memory" issue

July 5, 2017 316 views
Deployment Nginx Ubuntu 16.04

I have Jenkins running behind a Nginx proxy on my Ubuntu 16.04 server. I have connected it with GitHub through webhooks, and the connection works perfectly, however every build fails, and I keep getting the same error log:

Started by user admin
Building in workspace /var/lib/jenkins/workspace/kudos.gaboratorium.com
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Done
Cloning the remote Git repository
Cloning repository https://github.com/gaboratorium/kudos.git
 > /usr/bin/git init /var/lib/jenkins/workspace/kudos.gaboratorium.com # timeout=10
Fetching upstream changes from https://github.com/gaboratorium/kudos.git
 > /usr/bin/git --version # timeout=10
 > /usr/bin/git fetch --tags --progress https://github.com/gaboratorium/kudos.git +refs/heads/*:refs/remotes/origin/* # timeout=15
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "/usr/bin/git fetch --tags --progress https://github.com/gaboratorium/kudos.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: error: cannot fork() for fetch-pack: Cannot allocate memory

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1903)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1622)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:71)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:348)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(CliGitAPIImpl.java:545)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1070)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1110)
    at hudson.scm.SCM.checkout(SCM.java:495)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1276)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:560)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:485)
    at hudson.model.Run.execute(Run.java:1735)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:405)
ERROR: Error cloning remote repo 'origin'
Finished: FAILURE

I can't figure out what is going on, because everything seems to work fine. The strange thing is that if I log in to my server as jenkins and I perform these steps manually, everything succeeds.

In addition, Jenkins breaks occasionally and from that point Nginx keeps giving me a 503 error. If I restart the Jenkins service, everything works again.

1 Answer
gaboratorium123 July 6, 2017
Accepted Answer

Turned out it was a simple OOM (out-of-memory) problem, which I could easily resolve by creating a swap file. To do so, I have followed this guide: https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04

I have also made some notes, if you are having similar issues you might find it helpful: https://gaboratorium.github.io/notes/continuous-integration-with-jenkins

by Justin Ellingwood
Swap space can be used as an "overflow" area for your system when you run out of RAM. The operating system can store data that would normally be kept in RAM on the hard drive in a specially formatted file. In this guide, we'll demonstrate how to create and use one of these files in Ubuntu 14.04.
Have another answer? Share your knowledge.