rstats-wtf / what-they-forgot

"What They Forgot to Teach You About R" website / eBook
https://rstats.wtf
Other
422 stars 82 forks source link

Notes from github/netlify/travis adventure #25

Closed jennybc closed 1 year ago

jennybc commented 6 years ago

Setup for automatic build and deploy of this site.

jennybc commented 6 years ago

Hard to relate this in a linear fashion, because there was a lot of "two steps forward, one step backwards".

Turn off GitHub pages, which I turned on for some early success.

Stop tracking the directory with the rendered book in Git. I started with output_dir set to docs/ and I did commit and push a few versions, so I backed out via git rm -r --cached docs. Deleted the config of output_dir at this point, to accept the default of _book/.

Installed the beta Netlify command line tool:

https://www.netlify.com/docs/cli/

brew install npm
npm install netlify-cli@next -g

Authenticated myself (via GitHub, in the browser):

netlify login

Create Netlify site from Git from https://app.netlify.com/account/sites. This seems to create a webhook associated with the GitHub repo, connecting it to the netlify site. I eventually needed to de-Active-ate this (but NOT delete it). Specify the directory where the website content will be, i.e. _book/.

Link local Git repo to that netlify site (takes you to the browser):

netlify link

This produced .netlify/state.json, which holds the (non-secret) site id. Apparently this can also be provided via a travis env var named NETLIFY_SITE_ID (ok to reveal in the log). In hindsight, I think that implies there's no need for the netlify tool locally. Live and learn.

Get a netlify PAT from here https://app.netlify.com/account/applications and store as a secret (don't reveal in the log) env var named NETLIFY_AUTH_TOKEN on travis: https://travis-ci.org/jennybc/whattheyforgot/settings.

Here's .travis.yml:

language: r
sudo: false
cache:
  packages: true
  directories:
  - $HOME/.npm

pandoc_version: 2.1.1

before_install:
- npm install netlify-cli@next -g

script:
- Rscript -e 'bookdown::render_book("index.Rmd")'

deploy:
  provider: script
  script: netlify deploy --prod
  skip_cleanup: true

I had problems with each push to master creating two netlify deploy attempts:

  1. An automatic, immediate attempt, presumably due to the web hook. Always fails because the _book/ directory doesn't even exist yet.
  2. An explicitly initiated netlify deploy from .travis.yml. Succeeds.

I tried to eliminate the first attempt by deleting the web hook, but then the site completely broke. I re-connected the netlify site to the GitHub repo and unchecked "Active" on the web hook. This seems OK.

Not really described: the domain setup. Purchased via hover then basically followed my nose there and on netlify.

Got some good ideas from @lorenzwalthert https://lorenzwalthert.netlify.com/posts/getting-up-and-running-with-blogdown-netlify-and-travis/.

lorenzwalthert commented 6 years ago

@jennybc glad to hear that. I had some trouble with the approach outlined first (mainly because of PRs creating two travis builds), and I updated the blog post at some point. There are still some caveats that are not described in detail in the blog post but I think the script (deploy.sh) works fine now. Just let me know if there are any questions.

maelle commented 5 years ago

Thanks for making these notes public!

In case someone else finds this whilst googling the topic, a quicker solution by @stephlocke, used for e.g. https://github.com/lockedata/cransays/, and other Locke Data packages. 0) Find a way to build the thing, in our case a package, and for that we use tic set up by travis::use_tic() 1) Create a build webhook via https://app.netlify.com/sites//settings/deploys 2) Change .travis.yml by adding these lines (updated for only triggering a website build after success)

notifications:
  webhooks:
    urls:
      - https://api.netlify.com/build_hooks/<hook_id>
    on_success: always
    on_failure: never
    on_start: never
    on_cancel: never
    on_error: never

This means we've made the webhook URL public, although we tried setting it via an environment variable first.

jennybc commented 5 years ago

Thanks @maelle for sharing an alternative. I'm using this thread to record my "notes to self" and our in-house workflow, which doesn't use tic, so will hide this exchange as resolved.