100. Configuring GitLab CI
Now that you have a high-level understanding of CD and what's involved in setting it up, we're ready to begin creating a CD pipeline for PrintBay.
The first thing we need to do is create the GitLab YAML config file in the root of our project directory.
So let's go to the terminal and type:
$ touch .gitlab-ci.yml
Almost everything that we need to tell GitLab about how to build, test, and deploy our app will go in this file.
Let's first briefly talk about the YAML file format. Just like a JSON file, you can define properties and assign them values, which can either be primitive values, objects, or arrays.
For example, I can define a
variables property, and then go to the next line, and use tab to indent, and then assign to it a sub-property
NODE_ENV and assign to this the string "production".
This, obviously, is how we can define environment variables for our CD pipeline.
variables: NODE_ENV: "production"
In the first stage of our CD pipeline we want to automate a build of our app. To do this, we first need to tell GitLab the virtual environment it should use for this purpose.
The easiest way to do this is to is by providing a Docker image. If you aren't familiar with Docker, its a tool that can package software in a virtual environment, quite similar to Vagrant.
To build PrintBay, we'd ideally want a virtual environment with the same software as our Vagrant development environment. That means we'll need an Ubuntu operating system, and the software required to build and run our app including Node.js and NPM.
Here in the Docker Hub website, we can find freely available Docker images that meet our requirements.
The official node Docker image is a Linux-based image that will be perfect for our needs.
To tell GitLab to use this image, we can go ahead and provide an
image property in our YAML file, and put the name of the image which is
node, followed by a tag
This tag at the end of the image name denotes the version of Node, which is the same one we're using in our Vagrant environment to ensure consistency.
In addition to Node, we're also going to need Chrome installed in our virtual environment to run our end-to-end tests, as well as MongoDB because, remember, we can't run our server tests without a database.
The node image we're using here, though, doesn't include that software, so we will need to use other images as wel;. However, we can worry about that later as that software is not required in the build stage.
So, now we've elected a Docker image that will be the basis for the virtual environment where our continuous deployment pipeline can run.
Next, we can tell GitLab to run commands from inside the virtual environment. To do that, we're going to create our first "job".
Jobs are declared in the YAML file with a top-level property. So let's add a new property
build which will be the name of our first job.
Let's go to a new line, indent with thab, and give the
build job a sub-property
script, which defines a terminal command that should be run as part of this job.
In a moment, we're going to set the script so that it installs our app with NPM. But for now, we just want to test that our pipeline is working, so we're just going to put the classic test command
echo "Hello World".
As you'll see in the next video, anything that's output from the terminal will be logged in the GitLab UI, so we will get visual confirmation that this has worked.
image: node:10.15.3 build: script: echo "Hello World"