Search code examples
ruby-on-railsrubyjenkinsamazon-ec2bundle

Bundle install really slow on Jenkins Pipeline (AWS EC2 Instance)


I am running Jenkins on AWS EC2 Linux Instance and I am trying to bundle install the gems needed for my rails repo on GitHub.

It is a new project that I created for testing but the bundle takes hours, Jenkins freezes and I need to restart the server and Jenkins and it never finishes installing the gems in the end.

This is my code:

    pipeline {
    agent { docker { image 'ruby:2.6.6' } }
    stages {
        stage('Fetching Git') {

            steps {
                git credentialsId: 'user-key',
                    url: 'git@github.com:user/jenkins_project.git'
            }
        }
        stage('Build') {

            steps {
                sh 'gem install bundler'
                sh 'bundle install --jobs 4'
                sh 'RAILS_ENV=test rake db:migrate'
            }
        }
        stage('Test') {

            steps {
                sh 'RAILS_ENV=test bundle exec rspec --format RspecJunitFormatter --out result_spec.xml'
            }
        }
    }
}

What I tried doing is:

  1. Using --jobs when bundle install (it doesn't seem to work)

  2. Using http instead of https on the Gemfile when on

    source 'http://rubygems.org'

I know it is unsafe. And this doesn't seem to work either. 3. Also tried this:

echo 'export MAKE="make -j$(nproc)"' >> $home/.bash_profile
time MAKE="make --jobs 8" bundle install

From here (https://build.betterup.com/one-weird-trick-that-will-speed-up-your-bundle-install/). But it doesn't work either.

Any ideas on why is this happening and how can I solve it greatly appreciated!


Solution

  • Solved this by:

    1. Creating a master server with Java & Jenkins
    2. Creating a slave server (called 'linux_slave') with Java & Git & Docker

    The code would be something like this:

    pipeline {
        agent {
            docker {
                image 'ruby:2.6.6'
                label 'linux_slave'
            }
        }
        stages {
            stage('Fetching Git') {
                
                steps {
                    git credentialsId: 'user-key',
                        url: 'git@github.com:user/jenkins_project.git'
                }
            }
            stage('Build') {
                steps {
                    sh 'gem install bundler:2.0.1'
                    sh 'bundle install'
                    sh 'RAILS_ENV=test rake db:migrate'
                }
            }
        }
    }
    

    Hope it helps someone in need sometime!