Dan Mason
Dan Mason

Reputation: 2327

Set global environment variable from json file in travis yml config?

I am trying to get the following env variable to be set from a json file when triggered from a release tag.

.travis.yml

env:
  global:
    - PACKAGE_VERSION=$(if [ -n "$TRAVIS_TAG" ]; then node -p require('./meta.json').deploy_version; else echo "next"; fi)

But Travis is giving these errors on build:

$ export PACKAGE_VERSION=$(if [ -n "$TRAVIS_TAG" ]; then node -p require('./meta.json')
/home/travis/.travis/functions: eval: line 104: unexpected EOF while looking for matching `)'
/home/travis/.travis/functions: eval: line 105: syntax error: unexpected end of file

meta.json

{
  "version": "3.0.0",
  "deploy_version": "v3"
}

And I want PACKAGE_VERSION to be set to v3 when triggered from a tag/release otherwise set PACKAGE_VERSION to next.

Thanks in adavance for any help given!

Upvotes: 1

Views: 497

Answers (1)

Nick
Nick

Reputation: 166

I was stuck on the same issue, was able to get it to work by moving the node bit into it's own script like so:

file ver.js

const {version} = require("./package.json");
console.log(version);

In travis.yml

env:
  global:
    - PACKAGE_VERSION=$(node ver -p)

Upvotes: 1

Related Questions