Home > Back-end >  Jenkinsfile - Error when running RSpec INSIDE the container
Jenkinsfile - Error when running RSpec INSIDE the container

Time:12-12

I am trying to create a Jenkinsfile that:

  1. Builds the image of a RoR project from a Dockerfile
  2. Checks if there is a database and if there is not it creates it and migrates
  3. Does some RSpec tests and removes the containers
  4. Finally deletes workspace

This is my Jenkinsfile:

pipeline {
    agent { label 'linux_slave' }

    stages {
        stage('Build') {
            steps {
                sh 'docker-compose build'
            }
        }
        stage('DB') {
            steps {
                script {
                    DB_EXISTS = sh (
                        script: 'psql postgres -tAc "SELECT 1 FROM pg_database WHERE datname=\'Jenkins_project_test\'"',
                        returnStdout: true
                    ).trim()
                    echo "Equal 1 if DB exists: ${DB_EXISTS}"
                    if(DB_EXISTS == "1") {
                        echo 'Database already exists'
                    } else {
                        sh 'docker-compose run web rake db:create'
                        sh 'docker-compose run web rake db:migrate'
                    }
                }
            }
        }
        stage('Test') {
            steps {
                sh 'docker-compose run --rm -e "RAILS_ENV=test" web bundle exec rspec'
                sh 'docker-compose down'
            }
        }
    } 
    post {
        always {
            junit(
                allowEmptyResults: true,
                testResults: '**/test-reports/*.xml'
            )
            cleanWs()       
        }
    }
}

I am having troubles in the step 3. I get the following error:

  docker-compose run --rm -e RAILS_ENV=test web bundle exec rspec
Creating test_rails_app_web_run ... 
Creating test_rails_app_web_run ... done
Users
  GET /new
    responds successfully (FAILED - 1)

Failures:

  1) Users GET /new responds successfully
     Failure/Error: @import "bootstrap/scss/bootstrap";

     ActionView::Template::Error:
       Error: File to import not found or unreadable: bootstrap/scss/bootstrap.
               on line 7:1 of app/assets/stylesheets/application.scss
       >> @import "bootstrap/scss/bootstrap";

          ^
     # ./app/assets/stylesheets/application.scss:7

But if I try to run it locally doing the same steps it has no problem:

➜  Jenkins_project git:(master) docker-compose run --rm -e RAILS_ENV=test web bundle exec rspec
Creating network "jenkins_project_default" with the default driver
Creating jenkins_project_db_1 ... done
Creating jenkins_project_web_run ... done

Users
  GET /new
    responds successfully

Finished in 11.88 seconds (files took 23.43 seconds to load)
1 example, 0 failures

My directory layout is:

app
├── assets
│   ├── config
│   ├── images
│   └── stylesheets
        ├── components
        │   ├── _alert.scss
        │   ├── _avatar.scss
        │   ├── _index.scss
        │   └── _navbar.scss
        ├── config
        │   ├── _bootstrap_variables.scss
        │   ├── _colors.scss
        │   └── _fonts.scss      
        ├── pages
        │   ├── _home.scss
        │   └── _index.scss  
        └── application.scss

And the application.scss is as follows:

// Graphical variables
@import "config/fonts";
@import "config/colors";
@import "config/bootstrap_variables";

// External libraries
@import "bootstrap/scss/bootstrap";
@import "font-awesome-sprockets";
@import "font-awesome";

// Your CSS partials
@import "components/index";
@import "pages/index";

These are my two questions regarding the problem:

  1. Any ideas on to why this is happening on Jenkins and not locally?

  2. Is it better to run rspec inside of the container or outside the container (before building the image)

Thank you!!

CodePudding user response:

Simply enough, I ran yarn before rspec. That will take care of installing the bootstrap dependencies and another error related to webpack not being compiled:

sh 'docker-compose run -e "RAILS_ENV=test" web rake db:create db:migrate'
sh 'docker-compose run -e "RAILS_ENV=test" web yarn install --ignore-engines'
sh 'docker-compose run -e "RAILS_ENV=test" web bundle exec rspec'
  • Related