Deploy website changes from a local directory to Netlify.
Netlify CLI is nice, but it's heavy for a lot of the automation tasks I use it for. I created this tool to implement the parts that I need regularly in deployment automation.
Use Go CLI tools to install:
go get gitlab.com/lepovirta/netlify-deployer
netlify-deployer
The tool uses these environment variables:
NETLIFY_AUTH_TOKEN
: Authentication token for logging into NetlifyNETLIFY_SITE_ID
: The ID of the siteNETLIFY_DIRECTORY
: The directory of files to deployNETLIFY_DRAFT
: Settrue
to make a draft deployment. Setfalse
to make a production deployment. Default:true
.NETLIFY_DEPLOYMESSAGE
: A short message to include in the deployment logNETLIFY_LOGLEVEL
: Log level for application logs. Default:warn
NETLIFY_LOGFORMAT
: Format of the application logs. Eithertext
for text logs orjson
for JSON logs. Default:text
The tool prints out the unique URL for the deployment, which you can use to view site.
Docker images for this tool are hosted in the Gitlab Container Registry. This is the name of the image that is available
registry.gitlab.com/lepovirta/netlify-deployer
The image has the following tags available:
latest
: Latest version from the master branch with just the netlify-deployer binary included (i.e.Dockerfile.minimal
)ci
: Latest version from the master branch with the netlify-deployer binary and additional tools useful with CI platforms such as Gitlab CI (i.e.Dockerfile.ci
)
Here's how you can deploy your site from Gitlab CI to Netlify using the netlify-deployer
Docker image.
This integration does the following:
- Publish your site when running the pipeline on master branch
- Publish a draft site when running the pipeline on any other branch
- Post a link to your merge requests when the draft site is available (requires a Gitlab access token)
First, set up your Netlify access credentials in your repository's CI/CD settings:
- Go to your repository page in Gitlab
- Go to
Settings
>CI / CD
>Variables
- Add a new variable
NETLIFY_SITE_ID
and use your Netlify site's ID as the value - Add a new variable
NETLIFY_AUTH_TOKEN
and use your Netlify access token as the value - (Optional) If you want to post draft links to your merge requests, you need to also add a new variable
GITLAB_ACCESS_TOKEN
and use a Gitlab access token as the value
Next, your .gitlab-ci.yml
should be made to look something like this:
stages:
- # Other stages go here
- build-site
- publish-site
variables:
# You can use whatever directory here you want.
NETLIFY_DIRECTORY: public
build-site:
stage: build-site
script:
- # Use whatever commands you need here to generate your site.
- # Place the site to $NETLIFY_DIRECTORY
artifacts:
paths:
- $NETLIFY_DIRECTORY
# You can run this same job in both master and MR branches.
# It will automatically publish the site as a draft when run on non-master branches.
# If you want to publish your site on another branch other than master,
# set the variable NETLIFY_MAIN_BRANCH to point to some other branch.
publish-site:
stage: publish-site
image: registry.gitlab.com/lepovirta/netlify-deployer:ci
script:
- gitlab-deploy-site
For a more detailed guide, see this blog post: Website previews for custom Netlify deployments using GitLab CI
Make sure you have Go 1.13+ installed. After that, run these commands:
go mod download
go build
This should produce an executable binary called netlify-deployer
.
After you've built the app, you can build your own Docker image using the Dockerfiles in the repository:
docker build -f Dockerfile.ci -t netlify-deployer-ci .
docker build -f Dockerfile.minimal -t netlify-deployer .
MIT License
See LICENSE for more details.