How do I configure my .travis.yaml to deploy only on a successful PR merge to master?
This is my current .travis.yaml:
language: node_js
node_js:
- 14
cache:
yarn: true
directories:
- node_modules
- .next/cache
jobs:
include :
- stage: "Build š ļø"
script: yarn cp-env:qa && yarn build
- stage: "Deploy š"
if: branch = master
script : yarn deploy-qa
But every time I open a new PR on a branch ā master, both the āBuild
ā and āDeploy
ā stages run. I want only the āBuild
ā to run on a new PR, and then to have the āDeploy
ā stage run once the PRās been merged to master.
How can I accomplish this?
You could have a tag created when a successful merge into master is completed, then when a tag is created on master, have another build start running a deploy only.
What youāre likely looking for here is a āConditional Jobā. Using the example here: Build Stages: Matrix expansion - Travis CI
Yup⦠I ended up solving this in this way:
jobs:
include :
- stage: build
name: "PR Build š ļø"
before_script : echo "build_branch => $TRAVIS_BRANCH" && yarn cp-env:qa && yarn lint && yarn prettier-check
script: yarn build
if : type = pull_request
- stage: deploy
name: "Build & Deploy to QA š"
before_script : export NODE_ENV=production && yarn cp-env:qa && yarn lint && yarn prettier-check && yarn build && firebase functions:config:set api.root_url='https://qa.web.app' && firebase functions:config:set algolia.app_id=$ALGOLIA_QA_APP_ID algolia.api_key=$ALGOLIA_QA_API_KEY
script : travis_wait firebase --token $FIREBASE_TOKEN --non-interactive deploy --only functions,hosting:app-qa
if : branch = master AND type = push
Unfortunately, I couldnāt find a way to just reuse the previous build result, so had to mash both the build steps and the deploy into one job 
Iām glad my solution/work around worked for you @RavenHursT. Iāll bring this up internally and see if thereās something that can be done.
The thought process being (in this hypothetical), letās say if you are using AWS, thereās not a great reason why you need a PR to accomplish this. In any event, the deployment is going to require say for this example of using AWS, at least S3 credentials somewhere, and allowing any PR to do it (or say in your script) is going to incur a security risk.
Let me look into this further and see if I can get a better solution than my workaround solution.
Montana Mendy
Travis CI Staff