Deploying Static Sites on Netlify without Github
Until today, every previous deployment of this website was initiated by pushing code into a GitHub repository.
It recently became a priority to move my git hosting to a different service (happy with Sourcehut so far) while continuing to deploy the site on Netlify. Unfortunately, Netlify's continuous deployment only supports git hosting from GitHub, GitLab, Bitbucket, and Azure DevOps.
With a bit of work, we can decouple our git hosting from our deploys. Here, I will detail the straightforward process of deploying a static site on Netlify without using GitHub.
0. Prerequisites
Install Netlify's command line tool
Netlify offers a command-line interface, which it turns out, is necessary for our goals here. It can be installed through npm with npm install -g netlify-cli
Next, we authenticate our Netlify account by running netlify login
1. Unlink the Github from the Netlify project
The major change here is we need to unlink our project from our Github repository.
Caution: we don't want to run netlify unlink from the command line, because this command unlinks our local folder from the Netlify project, which is not what we want. Although, if we accidentally do this, it's cool, we can run netlify link and relink the local folder.
We can only unlink a project's GitHub repository from the web interface at Project configuration > Build and Deploy > Manage Repository > Unlink [github-url]. Netlify warns us that we will lose our continuous deployment settings, which is expected.
Or create a new project
Alternatively, we can create a brand new project rather than risk breaking our existing deployment.
We can make a copy of our project folder and, from our new project directory, run netlify sites:create which creates a new, manually deployed Netlify project without linking any remote repository, perfect.
2. Update netlify.toml
Next, let's configure which directory to deploy.
We want to ensure that we're deploying the correct directory from our local folder, we can set this in the project's netlify.toml file, which contains our build settings. If it doesn't already exist, we create a new netlify.toml file in our project directory.
In that file, add the publish key in the [build] settings table, which stores the local path (from the root of the project directory to the public site, often the output directory of our static site generator).
[build]
publish = "_site"
3. Test the deploy
That's really everything. If we're using Netlify's build services, we'll need to set up our build command (Netlify build documentation). In this example, we're building the site locally before deploying.
If we run netlify deploy --no-build now, our site will be deployed to a randomly-generated URL for testing, Netlify calls these draft deploys.
4. Production deploy
Once we've tested, we can run netlify deploy --no-build --prod to deploy to our production URL. We can now deploy our static sites with Netlify without depending on any particular git host.
Note: Even though we're manually deploying, we can still use Netlify's deploy rollback features and we can even add custom deploy messages with the deploy command:
netlify deploy --no-build --prod --message "Production deploy 00000001"
Here's two lines from my deploy script that append the most recent git commit hash to the deploy message:
GITHASH=$(git rev-parse --short --verify HEAD)
netlify deploy --no-build --prod --message="Production deploy ($GITHASH)"