PhantomJS on CircleCI 2.0

phantomjs

#1

I’m very close to getting our seasoned rails app to run on circleci 2.0, but am having issues with PhantomJS and how to install. I see the following error in our spec output,

Cliver::Dependency::NotFound:
  Could not find an executable ["phantomjs"] on your path.

Any ideas how to get phantomjs up and running in a relatively normal rails setup?

Here’s a snippet of our circle.yml

version: 2
executorType: docker
containerInfo:
  - image: ruby:2.3.1
  - image: elasticsearch:2.4.3
  - image: mdillon/postgis:9.5
  - image: redis:3.0.7-alpine

#2

I advise building your own Docker image;


#3

You can use this phantomjs Dockerfile as a reference: https://hub.docker.com/r/wernight/phantomjs/~/dockerfile/

Note that you won’t be able to use that Docker image with your setup on CircleCI 2.0. Your Rails code is looking for a phantomjs executable, and the containers in your build only share the localhost network interface. The containers have separate filesystems and your tests are running on the Ruby:2.3.1 container.

Since that Dockerfile I linked is built off of Debian Jessie, you could build your own Dockerfile that uses Ruby:2.3.1 as the base, as the official Ruby images are also built off of Debian Jessie.


#4

@ncavig did you ever figure this out?


#5

You could download the phantomjs binary from cpmjs (don’t use the bitbucket one, so many failures from this), then cache the phantomjs binary.


#6

Here’s the steps I’ve used in our config.yml to install Phantomjs 2.1.1. Note that they sometimes get rate-limited on downloads from the Bitbucket link, but as long as it gets downloaded once by CircleCI, it should stay in your cache, and you won’t rely on Bitbucket any longer. This is on a debian-based image:

  - restore_cache:
      key: phantomjs-2-1-1
  - run:
      name: Install PhantomJS Dependencies
      command: |
        [ -f /usr/local/bin/phantomjs ] || apt-get update
        [ -f /usr/local/bin/phantomjs ] || apt-get install -y fontconfig wget
  - run:
      name: Install PhantomJS
      command: |
        [ -f /usr/local/bin/phantomjs ] || wget -O /tmp/phantomjs.tar.bz2 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
        [ -f /usr/local/bin/phantomjs ] || tar -xjf /tmp/phantomjs.tar.bz2 -C /tmp
        [ -f /usr/local/bin/phantomjs ] || mv /tmp/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
  - save_cache:
      key: phantomjs-2-1-1
      paths:
        - /usr/local/bin/phantomjs

#7

Hello @adamnbowen,

I think you may need sudo before the apt-get calls, and the cache key seems to be incorrect – is this still working for you?


#8

It’s possible wget and fontconfig were already present in the image I was using, so you may be correct that the apt-get calls wouldn’t work generally. I do know that everything in the “Install PhantomJS” section works, as that is what we used for a while, and it appeared to cache correctly. I don’t use this method anymore, however, as I built phantomjs into our docker image a few weeks ago.


#9

@zzak What is wrong with the cache key?
I’m trying to use PhantomJS myself but I get

Found a cache from build 19 at phantomjs-2-1-1
Size: 27 MB
Cached paths:

  • /usr/bin/phantomjs

Downloading cache archive…
Unarchiving cache…
Error untarring cache: exit status 2

Thanks and have a nice day.


#10

That’s a permissions issue. It should be fine in /usr/local/bin, though.


#11

Thanks for your help. Once again you were right.
However, from my understanding even if the cache now works, Install PhantomJS Dependencies and Install PhantomJS are still executed (making the cache basically useless). Is there a way to skip those steps if the cache is found?

Have a good weekend.


#12

You’d have to do bash conditionals;

if [ ! -e /usr/local/bin/phantomjs ] ; then
  curl ...
  mv ...
fi

#13

Just providing this in case it helps someone else. I’m not doing anything fancy with caching just yet (trying to get my build running), but this finally did the trick for me:

- run:
  name: Install 'phantomjs'
  command: |
    sudo curl --output /tmp/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1
    sudo chmod ugo+x /tmp/phantomjs
    sudo ln -sf /tmp/phantomjs /usr/local/bin/phantomjs

On 1.0, we had been running a simpler download, but for some reason the permissions in 2.0 don’t allow executing the file. The old setup we had was a command that shows up in older support requests:

- sudo curl --output /usr/local/bin/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1

#14

I’m trying to use PhantomJS as well and am getting the same permissions issue from the cache (image circleci/ruby:2.3.3):

Found a cache from build 9344 at phantomjs-2-1-1
Size: 27 MB
Cached paths:
  * /usr/local/bin/phantomjs

Downloading cache archive...
Validating cache...

Unarchiving cache...
tar: usr/local/bin/phantomjs: Cannot open: Permission denied
tar: Exiting with failure status due to previous errors
Error untarring cache: exit status 2

Here are relevant parts of my config.yml:

  - restore_cache:
      key: phantomjs-2-1-1

 - run:
      name: Install PhantomJS
      command: |
        [ -f /usr/local/bin/phantomjs ] || wget -O /tmp/phantomjs.tar.bz2 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
        [ -f /usr/local/bin/phantomjs ] || tar -xjf /tmp/phantomjs.tar.bz2 -C /tmp
        [ -f /usr/local/bin/phantomjs ] || sudo mv /tmp/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs

  - save_cache:
      key: phantomjs-2-1-1
      paths:
        - /usr/local/bin/phantomjs

#15

You can’t cache /usr/local/bin with the user you’re using. You can store phantomjs in your pwd and symlink it to make it cacheable. Otherwise just use our browser images (where phantomjs is pre-installed) or build your own image with phantomjs installed.


#16

@rohara Which image has phantomJS pre-installed? I was using ruby-2.3.3:node-browsers and it wasn’t there.


#17

Ah sorry that image is 5 months old. Any of the browser images from the last 2 months should have it:

https://hub.docker.com/r/circleci/ruby/tags/


#18

Thanks! Unfortunately, I need ruby 2.3.3 and 2.3-browsers uses 2.3.4. Could you update the 2.3.3 image to include phantomJS also?


#19

I can’t, but you can compile your own. Our script only pulls in the latest versions from Ruby. I run into a version we have no support for on occasion and need to build my own image.

The easiest way to compile your own image for this is to take one of the Ruby Dockerfiles and change the FROM at the top of the file. We store all the Dockerfiles as artifacts on this job:

https://circleci.com/gh/circleci/circleci-images/1732#artifacts/containers/0


#20

We got this setup to work:

- restore_cache:
    key: v1-phantomjs-2.1.1    
      
- run:
    name: Install phantomjs
    command: |
      if ! [ $(which phantomjs) ]; then                
        sudo curl --output /usr/local/bin/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1
      fi
            
      sudo chmod ugo+x /usr/local/bin/phantomjs

- save_cache:
    key: v1-phantomjs-2.1.1
    paths:            
      - /usr/local/bin/phantomjs

Please not that the “v1” in the cache key can be incremented to deprecate any previous caches you may have.