Being a lazy programmer, I constantly think of ways to automate my development workflow so that I can catch up on my Netflix programmes. (I'm only at Season 2 of Black Mirror.)
Any toolkit or service that offers maximum returns with minimal work is a win in my book.
We will explore one such service today.
Netlify is essentially a Software As A Service (SaaS) that allows any developer to deploy their static websites effortlessly.
It is a robust all-in-one development workflow solution with built-in Continuous Deployment, Atomic deploys, 1 click HTTPS, CDN, integration with Git repositories and so on.
$ npm install gulp-cli -g
$ npm install gulp -D
$ touch gulpfile.js index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>A Netlify Gulp Demo</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col text-center p-5">
<h1>Hello World!</h1>
<p>A Simple Netlify Demo.</p>
</div>
</div>
</div>
</body>
</html>
$ npm init
$ npm install gulp-connect gulp-open
const { watch, series, parallel, src, dest, gulp } = require('gulp');
const connect = require('gulp-connect'); // Runs a local webserver
const open = require('gulp-open'); // Opens a URL in a web browser
// Launch Chrome web browser
// https://www.npmjs.com/package/gulp-open
function openBrowser(done) {
var options = {
uri: 'http://localhost:8080'
};
return src('./')
.pipe(open(options));
done();
}
// Gulp plugin to run a webserver (with LiveReload)
// https://www.npmjs.com/package/gulp-connect
function server(done) {
return connect.server({
root: './',
port: 8080,
debug: true,
});
done();
}
// Default Gulp command
exports.default = series(openBrowser, server);
This simple script starts a local development server and launches a browser to preview our website.
Let's test it out by running the following command.
$ gulp
Now that our set up is ready, it is time to push our code to GitHub.
After the site has deployed, you can preview it by visiting https://your-netlify-site-id.netlify.com from the browser.
To test the continuous deployment process, make some changes to index.html, save it and push to GitHub.
Whenever Netlify detects new code pushes to the repository, it will trigger a build process and deploy to the staging site.
In a real-world scenario, we would have additional npm dependencies for our application.
In order for Netlify to install the dependencies, we would need to give it access by creating an authentication token.
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Setting the npm token in an environment variable ensures that it remains secured and contained within our Netlify application. Exposing this value in the wild would again be a very bad idea because we are committing our codes to a public git repository.
Wouldn't it be nice if we could deploy and preview our site using the command line instead of through the Netlify Admin UI? Well, we can!
Netlify provides a CLI tool with lots of useful commands that we can run.
$ npm install netlify-cli -g
To see the commands available, run $ netlify help
$ netlify link
$ netlify deploy --prod
Replace the existing code in gulpfile.js with the following.
const { watch, series, parallel, src, dest, gulp } = require('gulp');
const connect = require('gulp-connect'); // Runs a local webserver
const open = require('gulp-open'); // Opens a URL in a web browser
const exec = require('child_process').exec; // run command-line programs from gulp
const execSync = require('child_process').execSync; // command-line reports
// Launch Chrome web browser
// https://www.npmjs.com/package/gulp-open
function openBrowser(done) {
var options = {
uri: 'http://localhost:8080'
};
return src('./')
.pipe(open(options));
done();
}
// Gulp plugin to run a webserver (with LiveReload)
// https://www.npmjs.com/package/gulp-connect
function server(done) {
return connect.server({
root: './',
port: 8080,
debug: true,
});
done();
}
// Commit and push files to Git
function git(done) {
return exec('git add . && git commit -m "netlify deploy" && git push');
done();
}
// Watch for netlify deployment
function netlify(done) {
return new Promise(function(resolve, reject) {
console.log(execSync('netlify watch').toString());
resolve();
});
}
// Preview Deployment
function netlifyOpen(done) {
return exec('netlify open:site');
done();
}
// Deploy command
exports.deploy = series(git, netlify, netlifyOpen);
// Default Gulp command
exports.default = series(openBrowser, server);
Deploy.
$ gulp deploy
With one single command, we've
This was a simple demonstration of continuous deployment using Gulp and Netlify.
There are also advance features from Netlify like Functions, Identity and Forms that you can leverage for your website as well.
Netlify’s generous free tier option is more than enough for basic static website publishing, however, you can upgrade to their paid tiers anytime which provides additional benefits.
You can grab the files for this tutorial from the GitHub repository.
In the next article, we will explore Surge; another feature packed static web publishing platform.