7

I am using code climate for code quality and test coverage. I have added the gem and code in spec_helper.rb to start the coverage. In the next step code as per the documentation: When you run your tests on CI, set the CODECLIMATE_REPO_TOKEN environment variable: $CODECLIMATE_REPO_TOKEN=******************** bundle exec rake

I tried to set the above line in the execute shell commands of jenkins. Also tried to set the environment variables CODECLIMATE_REPO_TOKEN in the Manage jenkins -> Configure System settings. But that doesn't work. I couldn't find any documentation to setup test coverage with jenkins. Any help would be appreciable.

As per the code-climate documentation

Add the codeclimate-test-reporter gem to your Gemfile:

gem "codeclimate-test-reporter", group: :test, require: nil

Start the test reporter on the very first line of spec_helper.rb or test_helper.rb:

require "codeclimate-test-reporter" CodeClimate::TestReporter.start

When you run your tests on CI, set the CODECLIMATE_REPO_TOKEN environment variable:

$ CODECLIMATE_REPO_TOKEN=**************************** bundle exec rake

(Note: This token is specific to this repo on Code Climate.)

(Note: **As an alternative to the above, you may prefer to define this token as environment variable within your CI's user interface or configuration file**.) 

Its written here that the token environment variable is to be defined within your CI server. That is what my question is where to define it in jenkins. I tried defining the global environment variable in jenkins under "Manage jenkins"->"System configuration". But it didn't work.

enter image description here

Ajeet Khan
  • 8,582
  • 8
  • 42
  • 65
  • did you get it running locally? – phoet Sep 16 '15 at 15:39
  • @phoet I have a jenkins server setup to trigger build on github pull request – Ajeet Khan Sep 16 '15 at 17:21
  • how does that answer my question? – phoet Sep 21 '15 at 13:03
  • @phoet I didn't get your question. How can I run it locally. I am using jenkins server with which I need to integrate the code-climate – Ajeet Khan Sep 21 '15 at 18:11
  • i was asking, that when you integrated the gem into your spec_helper.rb and you run your specs locally with the token, is that working? jenkins is just another environment you are running it on. if it works locally, the only issue can be in the environment of your ci server. – phoet Sep 22 '15 at 13:51
  • @phoet I have not set the token locally in my rails environment. I was trying to set the token on jenkins server as in the documentation its written that define this token as environment variable within your CI's user interface or configuration file. See the edits – Ajeet Khan Sep 22 '15 at 18:13
  • ah, now i understand your issue. how are you invoking your test-run? via rake? – phoet Sep 24 '15 at 10:06
  • @phoet In jenkinns CI, I am executing rspec test cases using the command "rspec spec". Where do I need to add the repo token of code-climate? – Ajeet Khan Sep 25 '15 at 17:33
  • @phoet Any suggestion? – Ajeet Khan Sep 28 '15 at 05:39
  • you need to prepend that call with the environment variable, ie `CODECLIMATE_REPO_TOKEN=XXX_YOUR_TOKEN_HERE_XXX rspec spec` – phoet Oct 01 '15 at 12:56
  • @phoet, Yes I did that on the jenkins server. In project configure section, Under build->execute shell, I append the line but still there is no effect on code-climate regarding code coverage. – Ajeet Khan Oct 01 '15 at 15:04
  • can you post a screenshot of what exactly you configured? – phoet Oct 08 '15 at 11:07
  • @phoet please see the edits to see the screenshot of my commands on jenkins server – Ajeet Khan Oct 08 '15 at 22:15
  • you are executing the tests via bundle exec rake rspec AND another time via rspec spec! – phoet Oct 12 '15 at 13:50
  • @phoet I tried executing with only one rspec command 'CODECLIMATE_REPO_TOKEN=**************************** rspec spec' but still not working – Ajeet Khan Oct 13 '15 at 10:02

2 Answers2

1

If your question is how to set an environment variable in Jenkins, you may look e.g. here: How to set environment variables in Jenkins?

Community
  • 1
  • 1
The Ancient
  • 382
  • 3
  • 13
  • No, I tried to setup a environment variable thinking that it should work for me. But my question is different. Kindly read it. – Ajeet Khan Sep 30 '15 at 12:16
  • I did, and still, there are two possible interpretations: 1. you assume that setting environment variable does not work in the way it is described in the documentation and you are looking for another way to communicate the value to the climate coverage or 2. you are not sure that the environment variable is set in Jenkins process. Which is correct? – The Ancient Sep 30 '15 at 12:38
  • You are correct. Actually I didn't know how to get code-climate up and running for code coverage with my jenkins server. I was doing hit and trial with the two possible interpretation given by you. – Ajeet Khan Sep 30 '15 at 12:43
  • Do you have ssh access to the server, where Jenkins runs? – The Ancient Sep 30 '15 at 12:45
0

I'm using a karma tester and nyc with mocha to create my report.(lcov.info) I crete my report and insert it in build/reports/coverage/XXXXXXXXXX/lcov.info . This path maybe is different for you.

After that I transform the lcov.info in codeclimate.XXXXX.json Finally all is merge whit sum-coverage

stage('Code Climate'){
            environment {
                CC_TEST_REPORTER_ID = credentials('7da93b1f-3602-458c-a07c-fcf36402c499')
            }
            steps{
                sh 'wget https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64  > ./cc-test-reporter'
                sh 'chmod 777  cc-test-reporter'

                sh './cc-test-reporter --debug before-build'
                sh './cc-test-reporter --debug format-coverage build/reports/coverage/server-tests/lcov.info -t lcov -o build/reports/coverage/codeclimate.server.json'
                sh './cc-test-reporter --debug format-coverage build/reports/coverage/ng/lcov.info -t lcov -o build/reports/coverage/codeclimate.frontend.json' 
                sh './cc-test-reporter --debug format-coverage build/reports/coverage/api-tests/lcov.info -t lcov -o build/reports/coverage/codeclimate.api.json'

                sh './cc-test-reporter  sum-coverage build/reports/coverage/codeclimate.*.json -p 3' 
                sh './cc-test-reporter upload-coverage -r ${CC_TEST_REPORTER_ID}'
              
                sh 'rm cc-test-reporter'
            }
        }

with

environment {
                CC_TEST_REPORTER_ID = credentials('7da93b1f-3602-458c-a07c-fcf36402c499')
            }

i extract my CC_TEST_REPORTER_ID that I have set in Manage Jenkins > Manage credentials. You can found it in https://codeclimate.com/repos/XXXXXXXXXXXXXXXX/settings/test_reporter where in XXXXXXXXXXXXXXX you need to put your codeclimate project.