108
Deploy Node.js using Gitlab CI pipeline
Setup the Gitlab CI/CD pipeline to push the Node.js code to the server and deploy it whenever code is merged to master branch.
Using a deploy token ensures that we can pull the code without the need to enter the credentials manually or save the credentials in the deploy script.
We are going to use Gitlab CI/CD Variables to save a private SSH key which Gitlab will use to authenticate with the server.
We are going to use SSH keys to authenticate rather than username and password as it is more secure.
This can be configured at a repository level or at a group level.
To view all the CI/CD variables of your repository,
To view all the CI/CD variables of your repository,
You can also view the same at a group level by first navigating to the group (Menu > Groups > Your Groups) and following the same steps.
If you already have a
SSH_PRIVATE_KEY
private key variable listed, you can skip this step.To create a new variable, Select the Add Variable button and fill in the details:
SSH_PRIVATE_KEY
<ssh_private_key_details>
. (To generate a new SSH public and private key pair, follow steps from this guide. Make sure to not accidentally overwrite any existing key pairs.)Variable
Click Add Variable to add the variable

Now, we need to add SSH public key to the list of
authorized_keys
in the production server.ssh root@example.com
)Add the SSH public key to authorized_keys
nano ~/.ssh/authorized_keys
Paste the SSH public key(starts withssh-rsa
) in a new line
Save the file
Gitlab looks for
.gitlab-ci.yml
in the root folder of your repository for CI/CD pipeline configurationsAdd a new file
.gitlab-ci.yml
in the root folderbefore_script:
- apt-get update -qq
- apt-get install -qq git
# Setup SSH deploy keys
- "which ssh-agent || ( apt-get install -qq openssh-client )"
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
deploy:
stage: deploy
environment:
name: production
url: https://example.com
script:
- bash deploy/deploy.sh
only:
- master
ubuntu
docker packagegit
package and then configure it to add our SSH_PRIVATE_KEY
StrictHostKeyChecking
to no
, to ensure git doesn't show manual prompt during initial connection.deploy
with a single pipeline stage deploy
which listens to commits on master
and runs the script in deploy/deploy.sh
Add a new file
deploy.sh
in deploy
folder#!/bin/bash
DEPLOY_SERVER=$DEPLOY_SERVER
echo "Deploying to ${DEPLOY_SERVER}"
ssh root@${DEPLOY_SERVER} 'bash' < ./deploy/server.sh
DEPLOY_SERVER
with value domain.com
for the repository using step 1.deploy/server.sh
Add a new file
server.sh
in deploy
folder# Pull code
cd /var/www/html/folder-name/
git checkout master
git pull origin master
# Build and deploy
yarn install
yarn run build
pm2 restart server
folder-name
in the above script to match the folder name used in the prerequisite stepWe need a runner to run our CI/CD pipeline jobs. This step is optional if a runner is already configured for your group in Gitlab.
To setup a new Gitlab group runner
- Provide default image as
ubuntu
and empty tags
Push the
.gitlab-ci.yml
and deploy/deploy.sh
, deploy/server.sh
files to master to start the automated deployment.You can click the status icon to go to the pipeline and then to the individual job to see the command line output of your script
active
, protected
and tags
to see if any of the conditions are incorrect.