How to install your own Ubuntu 20.04 Linux server | Part 8 of 10

linux hetzner cloud virtual server tutorial

In Part 8 of the tutorial we'll create a Gitlab Repository, establish ssh keys and configure a CI/CD

It's recommended that you have read the previous steps of this course. You'll find the overview at the end of this blogpost.

Course ingredients:

16. Create Gitlab Repository

First register a Gitlab account if you don't have one already.

Create a new project by clicking on the plus icon on the top of the page. Select blank project in this case. Initialize repository with a README.

You cannot clone empty repositories, so the purpose of this file is just to enable you to clone it right away if you like.

Give it a meaningful name, chose if it's public or private and confirm with create project.

17. Establish ssh keys and get started with CI/CD in Gitlab

In the sidebar to the left navigate to settings/CI/CD/Variables and click Expand.

Next click Add variable.

Create a set of variables:
SSH_PRIVATE_KEY -> Fill in your key and save it.
REMOTE_SERVER_USERNAME -> We'll create this user in the next chapter. Let's call it cicd
REMOTE_SERVER_IP -> Simply fill in your remote server IP.
REMOTE_SERVER_PATH -> Simply fill in the path to your public domain data directory.

In Gitlab navigate to your new Repository/Files view. Create a new file in the root directory and name it .gitlab-ci.yml

Copy and paste the following lines:

  - deploy

  stage: deploy
    ## Install ssh-agent if not already installed, it is required by Docker.
    ## (change apt-get to yum if you use an RPM-based image)
    - "command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )"

    ## Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)

    ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    ## We're using tr to fix line endings which makes ed25519 keys work
    ## without extra base64 encoding.
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -

    ## Create the SSH directory and give it the right permissions
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    ## Optionally, if you will be using any Git commands, set the user name and
    ## and email.
    #- git config --global ""
    #- git config --global "User name"

    - ssh "$REMOTE_SERVER_USERNAME"@"$REMOTE_SERVER_IP" "cd "$REMOTE_SERVER_PATH" && git pull && exit"

After committing this file the CI/CD pipeline will start immediatly. It will do so automatically after each upcoming commit in the future.

The Course Syllable:

22.05.2021 – Part 1

  1. Introduction & reasoning

29.05.2021 – Part 2

  1. Create Hetzner Cloud Server
  2. Connect to Server

05.06.2021 – Part 3

  1. Update and upgrade Ubuntu installation and install apache2
  2. Install PHP

12.06.2021 – Part 4

  1. Establish ssh keys
  2. Add new sudo user
  3. Deactivate root user
  4. Make new sudo user owner of ssh service

19.06.2021 – Part 5

  1. Buy Domain and point it to Hetzner Server
  2. Create Virtual Host for domain

26.06.2021 – Part 6

  1. Get free Let's Encrypt SSL certificates
  2. Request certificate, active https and force SSL

03.07.2021 – Part 7

  1. Install Git on Remote Server
  2. Create CI/CD user and ssh keys for Gitlab on Remote Server

10.07.2021 – Part 8

  1. Create Gitlab Repository
  2. Establish ssh keys and configure CI/CD

17.07.2021 – Part 9

  1. Install MySQL
  2. Install phpMyAdmin

24.07.2021 – Part 10

  1. Summary of project development workflow
  2. I'll be back – The course update log

See you next week for more


Previous Post Next Post

This site uses cookies to store information on your computer. Some of these cookies are essential, while others help us to improve your experience by providing insights into how the site is being used. ×
Necessary Cookies

Necessary cookies enable core functionality such as page navigation and access to secure areas. The website cannot function properly without these cookies, and can only be disabled by changing your browser preferences.

Analytical Cookies

Analytical cookies help us to improve our website by collecting and reporting information on its usage.

Marketing cookies

We use marketing cookies to help us improve the relevancy of advertising campaigns you receive.