Step by step to deploy code to the server with CI/CD on Gitlab

Step by step to deploy code to the server with CI/CD on Gitlab

This post will show you how to set up a pipeline on Gitlab to deploy code to the server automatically whenever a merge request is made. I will try creating a pipeline deploy code to a sandbox environment to test before doing the code merge. Ok! let go…

Step 1: Install GitLab Runner

First, you need to access your server via ssh and then we install Gitlab runner using binary file. Please select the version that suitable for your server’s operating system. In my case, I choose to install on Linux x86-64.

1
2
3
4
5
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

Second, back to Gitlab and go to Settings > CI/CD and expand the Runners section. Note the URL and token because you will need it to register the runner.

1
sudo gitlab-runner register

Step 2: Make .gitlab-ci.yml file

This file will be used to describe for Gitlab how to pipeline flow running. Below is an basic example file I use to deploy the code to the sandbox every time a merge request is made.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
stages:
- deploy

deploy_sandbox:
stage: deploy
tags:
- demo
- sandbox
script:
- npm install
- npm run build:sandbox
- sudo cp -R dist/* /home/ozuit/mydomain.com
only:
- merge_requests

You can find more examples in here: GitLab CI/CD Examples

Step 3: Deploy with Gitlab pipeline

  • Commit your file and push it to the Gitlab repo.
  • Try to make a merge request and switch to CI/CD > Pipelines to view processing.

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×