105

My project uses both nodejs and java

I tried starting off with a node_js build then installing java (since this is an npm module)

but the scripts to install java failed, plus I don't think there's a need to install it when there is a build with java that already exists.

should I start off with a java build then install node?

I'm trying this

language: java
  - oraclejdk8
language: node_js
node_js:
  - "0.10"

which ignores the first 2 lines it seems and builds a node_js build which has java 7 and my project uses java 8

I tried this answer for python

using

language: node_js
node_js:
  - "0.10"
java: oraclejdk8

but that didn't work

How can I add java 8?

Codiak
  • 1,832
  • 2
  • 13
  • 20
Amr Draz
  • 2,806
  • 3
  • 20
  • 26
  • Related: https://stackoverflow.com/questions/18456611/is-it-possible-to-set-up-travis-to-run-tests-for-several-languages – dmcc Jul 30 '15 at 21:49

6 Answers6

122

It seems to be possible now to run several languages in one .travis.yml file using the jobs:include feature. As an example, my Github repo is arranged as follows:

project/ - top-level github directory
project/backend - Python backend
project/backend/tests - Python tests
project/android/AppName - Android app
project/ios/AppName - iOS app

Here is the .travis.yml, which runs tests in Python, Java, and Objective-C:

jobs:
  include:
    - language: python
      python: 2.7
      before_script:
        - cd backend/tests
      script:
        - python -m unittest discover

    - language: android
      dist: trusty
      jdk: oraclejdk8
      android:
        components:
          - tools
          - android-25
          - build-tools-25.0.3
      before_script:
        - cd android/AppName
      script:
        - ./gradlew build connectedCheck

    - language: objective-c
      os: osx
      osx_image: xcode8.3
      before_script:
        - cd ios/AppName
      script:
        - xcodebuild -workspace AppName.xcworkspace -scheme AppName
          -destination 'platform=iOS Simulator,name=iPhone 7,OS=10.3' build test

notifications:
  email:
    - yourname@gmail.com

It seems you can build as many different configurations as you like using this feature, by treating each entry in the matrix as a top level config. Of course, if you have any parameters you want to set that apply to all languages, you can do that at the top level, as I do here with the notifications:email section.

When it is all set up, then on each build, you get something like this. Boom.

enter image description here

Codiak
  • 1,832
  • 2
  • 13
  • 20
  • 2
    This looked promising, but it fails the travis lint tool: http://lint.travis-ci.org – meisteg Aug 06 '17 at 23:10
  • 4
    I can't speak to the lint tool, but I can say I've been using a setup like this for several months now, and it has worked perfectly. – Codiak Aug 07 '17 at 20:09
  • I used this setup for python and nodejs. Instead of using `python: 3.5` to set the version, I used `python:\n - "3.5"` (notice these are two separate lines) as recommended in travis-ci's official page. The build is failed because travis tries to retrieve `python-["3.5"]` (notice the `[` and `"`). Do you know why that might be? The one-liner works. – CrazyFrog Oct 24 '17 at 04:56
  • Note: This DOES NOT WORK with multiple versions, e.g. `python: [3.5, 3.6]` because Travis only [takes the first array value](https://docs.travis-ci.com/user/customizing-the-build/#Explicitly-included-jobs-inherit-the-first-value-in-the-array) – ThiefMaster Apr 12 '18 at 16:30
  • This should be the accepted solution! It works without any problems for one of my repositories (c++, c, python, js and go all in one repo). – Daniel Schütte Feb 10 '19 at 17:26
  • 6
    there's something odd here: the OP asked about setting multiple languages IN THE SAME BUILD, not about setting up multiple languages, one for each build. – FuzzyAmi Feb 28 '19 at 09:01
  • To clarify: This solution does not work for running integration tests that rely on all languages? – bluenote10 Jul 17 '19 at 13:35
  • @bluenote10 I believe that is an accurate statement. – Codiak Jul 17 '19 at 19:05
  • 2
    @meisteg when using [the official travis lint tool](https://support.travis-ci.com/hc/en-us/articles/115002904174-Validating-travis-yml-files) it validates it fine. `$ travis lint ~/Desktop/test.yml Hooray, /Users/nathanf/Desktop/test.yml looks valid :)` – Nathan F. May 12 '20 at 18:13
  • It's now `jobs: include:` see the docs: https://docs.travis-ci.com/user/build-matrix/ – makeworld Aug 24 '20 at 16:27
  • @makeworld - thanks! updated accordingly – Codiak Mar 11 '21 at 22:16
28

On a Travis Java build environment, you can use nvm to manage Node.js runtimes:

.travis.yml

language: java

jdk:
  - oraclejdk8

env:
  - NODE_VERSION="0.12"

before_install:
  - nvm install $NODE_VERSION

If your Node version is very recent, you might have to update nvm too.

To update nvm, write this in your .travis.yml:

before_install:
  - wget https://raw.githubusercontent.com/creationix/nvm/v0.31.0/nvm.sh -O ~/.nvm/nvm.sh
  - source ~/.nvm/nvm.sh
  - nvm install 5 # for Node v5
  - node --version

The above example shows how to first update to nvm v0.31, to then obtain Node v5.

Benny Code
  • 51,456
  • 28
  • 233
  • 198
13

I used this .yml:

language: java
jdk:
    - oraclejdk8
node_js: "0.10"
install: "npm install"
script: "npm test"
hichris123
  • 10,145
  • 15
  • 56
  • 70
Amr Draz
  • 2,806
  • 3
  • 20
  • 26
13

My project has a Python/Django backend and a JS/Vue frontend like below:

├── backend
│   ├── api
│   │   ├── tests                               
├── daemon                                                          
│   ├── frontend
│   │   ├── test

The idea is to run each test suite in a matrix' job, one for Python/Django tests and the other for JS ones:

matrix:
  include:
    - language: python
      python: 
        - 3.4
      before_install:
        - cd backend/
      install: 
        - pip install -r requirements.txt
      script:
        - python manage.py test

    - language: node_js
      node_js:
        - 4.8
      before_install:
        - cd daemon/frontend
      install:
        - yarn install
      script:
        - yarn test

notifications:
  email: false

See also

Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
10

You can't add multiple languages, which explains the behavior you are seeing, and the node_js setting will only be recognized in a node language project. What you can do is utilize the incorporated nvm in TravisCI. For example, you can include - nvm install 0.10 - nvm use 0.10 in your before_install section to download the latest v0.10.x release of node.

kevincolten
  • 139
  • 1
  • 9
1

As per the documentation,

jobs:
  include:
    - language: python
      python: 3.8
      script:
      - python -c "print('Hi from Python!')"

    - language: node_js
      node_js: 12
      script:
      - node -e "console.log('Hi from NodeJS!')"

    - language: java
      jdk: openjdk8
      script:
      - javac -help

Here's a production example.