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:
stages:
- deploy
deploy:
stage: deploy
before_script:
##
## 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.
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
##
- 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 user.email "user@example.com"
#- git config --global user.name "User name"
script:
- 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
- Introduction & reasoning
29.05.2021 – Part 2
- Create Hetzner Cloud Server
- Connect to Server
05.06.2021 – Part 3
- Update and upgrade Ubuntu installation and install apache2
- Install PHP
12.06.2021 – Part 4
- Establish ssh keys
- Add new sudo user
- Deactivate root user
- Make new sudo user owner of ssh service
19.06.2021 – Part 5
- Buy Domain and point it to Hetzner Server
- Create Virtual Host for domain
26.06.2021 – Part 6
- Get free Let's Encrypt SSL certificates
- Request certificate, active https and force SSL
03.07.2021 – Part 7
- Install Git on Remote Server
- Create CI/CD user and ssh keys for Gitlab on Remote Server
10.07.2021 – Part 8
- Create Gitlab Repository
- Establish ssh keys and configure CI/CD
17.07.2021 – Part 9
- Install MySQL
- Install phpMyAdmin
24.07.2021 – Part 10
- Summary of project development workflow
- I'll be back – The course update log
See you next week for more
Johnnie