Search code examples
reactjsfirebasegithubtravis-ci

Only execute script if branch is master in TravisCI


I'm new to TravisCI and this may be a very silly question, but I'm trying to write the travis config in a way that it only deploys to Firebase when the current branch is master. That is, only when code is pushed to master or when a PR is merged with master, the firebase deploy command executes. The deploy command should be not be executed when other branches are pushed to, or when PRs are made. Here's what I have so far:

language: node_js
node_js: 12.16.1
script: echo "Running travis-ci"
install: 
  - npm install -g firebase-tools
  - npm i react-scripts
script:
  - yarn add react
  - yarn test
  - if [ "$TRAVIS_BRANCH" = "master" ]; then yarn build; fi
  - if [ "$TRAVIS_BRANCH" = "master" ]; then firebase deploy --project testproj8876 --token $FIREBASE_TOKEN; fi
branches:
  only:
    - master

Since I'm not too familiar with the conventions yet, any improvements/suggestions would also be greatly appreciated.


Solution

  • Google Firebase is supported directly by Travis. See here.

    Thereby, I recommend using the solution described in the link above.

    deploy:
      provider: firebase
      token:
       secure: "YOUR ENCRYPTED token"
    

    As for your condition, you can check one of my .travis.yml file here and the documentation there (Conditional Deployments)

    The following part is what you need:

    deploy:
      cleanup: false
      on:
        branch:
        - master
    

    If you still have questions, feel free to ask.