- Have a production Netlify site that has "Deploy only the production branch and its deploy previews." enabled
- Setup a Personal Access Token here: https://app.netlify.com/account/applications
- Lock the deploys - Under Deploys, click "Stop auto publishing" - more info here: https://www.netlify.com/docs/locked-deploys/
- Have jq available -
npm install node-jq
- https://stedolan.github.io/jq/ - Have some tests! https://github.com/gjtorikian/html-proofer works great for this
====
< run tests >
< if fail, do nothing - site remains locked on working version >
< if pass && branch==master, run the following: >
# capture latest deploy id from successful builds of the master branch
deploy_id=`curl -s https://api.netlify.com/api/v1/sites/<PRODUCTION SITE ID>/deploys/deploys?page=1&per_page=15&branch=master&match=true -H "Authorization: Bearer <TOKEN>" | ./node_modules/node-jq/bin/jq -r 'map(select(.state | contains("ready"))) | .[0].id'`
# publish the latest deploy
curl -s "https://api.netlify.com/api/v1/sites/<production site id>/deploys/$deploy_id/restore" -X POST -d "" -H "Authorization: Bearer <TOKEN>"
# lock the latest deploy
curl -s "https://api.netlify.com/api/v1/deploys/$deploy_id/lock" -X POST -d "" -H "Authorization: Bearer <TOKEN>"