Deploying to Netlify using GitLab CI

Update The code snippet below has been updated to point to netlifyctl rather than the netlify Node CLI, as netlifyctl is now the recommended CLI interface.

Since I migrated my meetups + conference talks repo to Reveal.JS, I’ve found that I’ve been wanting to have a branch spun up with the talk’s content at a publically accessible URL, exactly how I have configured Review Apps for my personal site.

However, as I was using GitLab pages, I wasn’t able to get this functionality working (and it likely will not be configured upstream). Recently I’ve been hearing a lot about Netlify, which made this a perfect opportunity to see what everyone was raving about.

The first step I needed to take was to move from GitLab Pages to Netlify, which I detail below, followed by configuring Netlify for Review Apps / Deploy Preview. I’ll be covering that in a later article, once I’ve worked out how to do it, as at first glance, the URL structure for branch deploys doesn’t work quite as nicely out-of-the-box with GitLab Review Apps.

As building the talks require a few tools to exist on the box, I would prefer to continue using GitLab CI as my platform to build the content. Note that I’m trying to make the process better but I’d still like to retain GitLab as the owner of the build process.

While looking at how to transition, I couldn’t find anything explicitly sharing how to get this up and running. That may have been because it’s incredibly simple to bake in using the netlify-cli tool.

I simply had to change my .gitlab-ci.yml to add the following:

 stages:
   - deploy

 netlify:
   stage: deploy
   image: registry.gitlab.com/jamietanna/jvt.me/builder:master
   script:
     - "./.ci/build-reveal-site.sh chef-infrastructure-as-cake"
     - "./.ci/build-reveal-site.sh came-for-the-campus-stayed-for-the-community"
     - "mkdir public"
     - "touch public/index.html"
     - "./.ci/deploy-reveal-site.sh chef-infrastructure-as-cake 'Chef: Infrastructure as Cake'"
     - "./.ci/deploy-reveal-site.sh came-for-the-campus-stayed-for-the-community 'Came for the Campus, Stayed for the Community'"
+    - apk add --update curl
+    - curl https://github.com/netlify/netlifyctl/releases/download/v0.4.0/netlifyctl-linux-amd64-0.4.0.tar.gz -LO
+    - tar xvf netlifyctl-linux-amd64-0.4.0.tar.gz
+    - ./netlifyctl deploy -s b758cf8f-9eeb-4770-ba63-ce7defafe8f6 -P public -A $NETLIFY_ACCESS_TOKEN
   artifacts:
     paths:
       - public
   only:
     - master

Next, I needed to create a personal access token to authorize myself to the Netlify API. I did this by browsing to Account Settings, then the OAuth Applications tab and clicking New access token.

I set this as the secret variable NETLIFY_ACCESS_TOKEN in the GitLab repo’s CI/CD subsection under the Settings, and that was it, ready to go!

Then, we can see a deploy succeeds, and you can now access the site at https://talks.jvt.me, with certificates managed by Netlify, and provisioned by Let’s Encrypt.

*****

Written by Jamie Tanna on 12 April 2018, and last updated on 05 July 2018.

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under Apache License 2.0.

Tags

Categories