Automatic Deployments with Now for GitLab

Learn how to setup ZEIT Now for GitLab and get automatic deployments and aliasing for each push.

The Now for GitLab integration provides you with automatic deployments for each push to a project, automatic aliases for pushes to the default branch, and instant rollbacks for reverts.

This guide will walk you through how to set up the Now for GitLab integration for your projects.

Step 1: Set Up a Now for GitLab Project

To deploy a project with Now for GitLab, you must first have a project on GitLab.

Create a new GitLab project with a file called index.html with the following contents:

<body>
  <h1>Hello World</h1>
</body>

Push this file to your GitLab project so you have one file inside of it.

A simple GitLab project setup.

Step 2: Connect Now to Your GitLab User and Project

For Now to make automatic deployments and aliases, it needs access to your GitLab account and its associated projects.

Connect your GitLab account through your account settings.

If you don't have a ZEIT account, visit https://zeit.co/signup and click "Sign Up with GitLab".

Link Your GitLab Projects

From the account settings section where you connected your GitLab account, or from the GitLab tab on https://zeit.co/new, select your GitLab project from the dropdown and click "link" to link it.

Linking a GitLab project.

With a project linked, no further setup is required on ZEIT.

Step 3: Create a now.json File

Every Now project has a file called now.json which holds the instructions Now follows to deploy your app as you intend.

In the root of the project directory, create a file called now.json with just the following content to start:

{
  "version": 2,
  "name": "Hello World"
}

The above now.json file sets two properties:

The version property instructs Now to deploy on the latest Now Platform Version 2.0.

The name property instructs Now to assign your deployment under a project called "Hello World". If you leave this field blank, the name assigned will be that of your GitLab project.

Step 4: Using Now for GitLab

With configuration done, all future commits from your linked projects will have automatic deployments for each push, merge requests, and automatic aliasing to production.

Deployments For Each Push

Now, visiting your commits page in GitLab, you will see something like this:

A deployed commit using Now for GitLab.

This green status symbol indicates that Now successfully deployed your commit.

Clicking the commit itself, you will see a comment that holds a unique deployment URL to test and share the specific commit build:

A unique URL for an individual commit.

Each commit will result in a unique URL just like the above.

Deployments and Staging Aliases for Merge Requests

Merge requests are a way to submit changes to a project without pushing directly to the default branch. This comes in handy when you're working on a feature that is either in progress or needs a review before being launched into production.

There are multiple ways to create a merge request, however, to test, create a merge request by editing the index.html file, edit the target branch, and then click "Start a new merge request".

Creating a merge request from GitLab's UI.

After you commit and create the merge request, you will get a new view that will start a deployment pipeline for your changes:

A merge request deployment pipeline success message.

The pipeline information shows the status as Now builds your app. In this case, it has successfully built. It will show a different icon when Now is building the app.

Below the pipeline message, a comment will present a staging alias that updates each time the merge request is pushed to, so you can share this link with others without having to worry about sharing the latest update.

A staging alias created from a merge request.

You can test this by creating another commit for the merge request and accessing the staging alias. You will notice that, once the commit is deployed, the staging alias will have been updated with the changes.

Deploying to Production

Pushing or merging to the default branch will result in a deployment, like any other branch or push. However, if you have an alias property set within your now.json file, these deployments will automatically be aliased to your configured destination.

Append the alias property to your now.json file, like the following:

{
  "version": 2,
  "name": "Hello World",
  "alias": "my-hello-world-project.now.sh"
}
Note: Make sure to choose a unique alias for your project. If you are not using a domain or a Custom Deployment Suffix, others on the Now platform may have already taken the alias.

When you push this change to the default branch, this and all subsequent commits to the branch will have the following comment, letting you know that your deployment is live in production.

A staging alias created from a merge request.

It is also possible to use your own domains or an alias using a Custom Deployment Suffix. For that, see the following documentation:

In Conclusion

Through this guide, you should now have a bulletproof deployment pipeline for all of your GitLab projects, using Now for GitLab.

With every push, an automatic deployment. With every merge request, a unique staging alias. And with every update to the default branch, your changes are aliased to production.

More Resources

For more information on working with Now for GitLab, please refer to the Now for GitLab documentation.

To configure Now further, please see these additional topics and guides:


How Was This Guide?

Written By
Written by arunodaarunoda
Written by timothytimothy
on April 1st 2019