Gitlab CI - installing correct version of yarn

I’m trying to set up CI/CD using gitlab and I’m stuck at specific moment. Below is my .gitlab-ci.yml file:

image: "ruby:2.6"

before_script:
  - ruby -v
  - apt-get update -qy
  - apt-get install -y nodejs
  - apt-get install -y yarn
  - yarn --version
  - bundle install --path /cache
  - bundle exec rails webpacker:install

test:
  script:
    - bundle exec rake db:create RAILS_ENV=test
    - bundle exec rake test

The CI output progresses without an error until it hits $ bundle exec rails webpacker:install. Task is aborted with this message:

$ bundle exec rails webpacker:install
rails aborted!
ArgumentError: Malformed version number string 0.32+git
/builds/kvinklly/sample-app/bin/rails:5:in `<top (required)>'
/builds/kvinklly/sample-app/bin/spring:8:in `require'
/builds/kvinklly/sample-app/bin/spring:8:in `block in <top (required)>'
/builds/kvinklly/sample-app/bin/spring:5:in `tap'
/builds/kvinklly/sample-app/bin/spring:5:in `<top (required)>'
Tasks: TOP => webpacker:install => webpacker:check_yarn
(See full trace by running task with --trace)

I noticed that the 0.32+git value is most likely the version of yarn that gets installed, and verified that is the version:

$ yarn --version
0.32+git

Is there a way to specify a newer version or the latest version of yarn during a CI script on gitlab?

I can post the gemfile, but it’s a fairly basic rails app without much added at this point.

Hey @kvinklly ,

you can also install it e.g. via npm instead of apt-get

Greetings
Michael

Do you have to install npm first? I think I tried that and npm was not installed with nodejs. I did get it working like this:

# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/ruby/tags/
image: "ruby:2.6"

# This is a basic example for a gem or script which doesn't use
# services such as redis or postgres
before_script:
  - ruby -v  # Print out ruby version for debugging
  - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
  - echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
  - apt-get update -qy
  - apt-get install -y nodejs
  - apt-get install -y yarn
  - yarn install --check-files
  - bundle install --path /cache  # Install dependencies into ./vendor/ruby
  - bundle exec rake webpacker:install

test:
  stage: test
  script:
    - bundle exec rake db:create RAILS_ENV=test
    - bundle exec rake test

It seems like more than we should need though.

1 Like

Good to hear that you find a way.

To answer your question:
Yes, you need to install npm first via apt-get install npm -y and then just install yarn with npm install yarn -g, which installs 1.22.10 right now.