Marco Daniel
Marco Daniel

Reputation: 5765

Travis CI - conditional 'before_deploy' script per each deploy provider

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?

Upvotes: 3

Views: 869

Answers (1)

smac89
smac89

Reputation: 43147

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

Upvotes: 3

Related Questions