3

I am trying to deploy with Travis CI to 2 different providers(npm, firebase), my .travis.yml file looks something like this:

branches:
  only:
    - master
    - /v\d+\.\d+\.\d+/

install:
  - yarn

before_deploy:
  # first provider
  - yarn build:storybook
  # second provider
  - yarn build:library
  - cp package.json lib/
  - cd lib

deploy:
  - provider: firebase
    ...
    on:
      branch: master
  - provider: npm
    ...
    on:
      tags: true
      all_branches: true

Now I would like to trigger #first provider block inside before_deploy only when I am deploying to firebase (master).

Is there some way to have a condition inside before_deploy? Or even a only: -branch-name inside before_deploy?

Marco Daniel
  • 5,467
  • 5
  • 28
  • 36
  • Have you tried using a [build matrix](https://docs.travis-ci.com/user/build-matrix/)? – Itai Steinherz Jan 21 '19 at 10:43
  • @ItaiSteinherz, how exactly could a build matrix help my case? – Marco Daniel Jan 24 '19 at 07:08
  • Never mind, I thought using a build matrix would solve this issue, but after trying myself I saw that it doesn't. You should post this question on [Travis CI's forum](https://travis-ci.community) or [create an issue on GitHub](https://github.com/travis-ci/travis-ci/issues/new). – Itai Steinherz Jan 25 '19 at 15:40

1 Answers1

3

You can specify your condition as a bash script:

before_deploy:
  # first provider
  - |
    if [[ $TRAVIS_BRANCH != $TRAVIS_PULL_REQUEST_BRANCH && $TRAVIS_BRANCH = 'master ]]; then
      yarn build:storybook
    fi
  # second provider
  - yarn build:library
  - cp package.json lib/
  - cd lib

What this does is prevent the script from running yarn build:storybook when someone makes a new pull request to master; but only runs when the master branch is built by travis due to a push (or whatever triggers the build).

See here for more variables: https://docs.travis-ci.com/user/environment-variables/#default-environment-variables

smac89
  • 39,374
  • 15
  • 132
  • 179