What is git?
Git is a modern distributed revision control system, in a way pretty much like svn (or cvs, rcs), which is centralized. but git is much more powerful, especially when it comes to working with branches.
It’s a full blown devops-tool nowadays and besides keeping track of your code, you can configure build- and deploy-pipelines, connect it to kubernetes and whatnot.
What’s this about?
In this post I’ll cover the basics on how to install a gitlab server “manually” with the omnibus installer (this is pretty straightforward as it brings it’s own chef installer) and getting started with basic commands and workflows.
If you despise “manual” installations entirely, you may take a look at the puppetlabs module (which also uses omnibus, which utilizes chef).
However, I for one strongly believe in doing things myself at least one time, for a better understanding of how they work.
Prerequisites
- a VM/Server with at least 6GB RAM and debian9
Installation
apt-get install -y curl openssh-server ca-certificates
especially ca-certificates are important
echo postfix postfix/main_mailer_type select "Internet Site" | debconf-set-selections
echo postfix postfix/mailname select `hostname -f` | debconf-set-selections
echo unattended-upgrades unattended-upgrades/enable_auto_updates boolean true | debconf-set-selections
Gitlab requires postfix and postfix (even in this very basic setup) requires some input.
Thats what debconf is used for here, for pre-defining the needed values for the unattended (not in our case) installation.
apt-get -y install postfix unattended-upgrades
Install postfix.
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | bash
The above script detects your OS, adds the correct repository to your sources.list and imports the gpg key.
You may as well fetch the with wget and then import it with apt-key add.
apt-get install -y gitlab-ce
Let’s install gitlab. This will take some time while chef does it’s thing.
vi /etc/hosts
127.0.1.1 gitlab.your.domain gitlab
192.168.122.123 gitlab.your.domain gitlab
Double check that your /etc/hosts entries fit, gitlab relies on it.
cp /home/user/gitlab.your.domain-key.pem /etc/gitlab/ssl/gitlab.your.domain.key
cp /home/user/gitlab.your.domain-pub.pem /etc/gitlab/ssl/gitlab.your.domain.crt
Skip this step if your certs are not self signed:
mv /home/user/ca-root.pem /usr/local/share/ca-certificates/ca-root.crt
update-ca-certificates
Now get yourself an ssl certificate for your domain, either self signed or maybe through a service like letsencrypt and copy it to /etc/gitlab/ssl.
vi /etc/gitlab/gitlab.rb
external_url 'https://gitlab.your.domain'
gitlab-ctl reconfigure
gitlab-ctl restart
You should now be able to access gitlab with a browser on https://gitlab.your.domain.
Create yourself a password for the root account and then create a user.
If something does not work, here are some troubleshooting tips:
gitlab-rake gitlab:check SANITIZE=true
does a config check
gitlab-ctl status
shows the status of the various gitlab processes
tail -f /var/log/gitlab/*
gitlab is pretty verbose about it’s actions though it can be somewhat hard to figure out which component is failing because there are lots of them.
working with git – the basics
I recommend using git (from the commandline) with ssh, therefore you should:
- Login as your user via the webinterface
- click on the icon in the top right corner and then settings
- on the sidebar to the left, click on SSH KEYS
- upload an ssh key
- test your connection by logging in via ssh (ssh git@gitlab.your.domain)
creating a new project
Commandline:
mkdir myproject
vi some_fancy_code.ending
git init
Initialized empty Git repository in /home/user/myproject/.git/
git add --all # No Output, tells git to track the file, in this case all of them
git commit -m "initial commit" # commits to the local repository
[master (root-commit) ec268cc] initial commit
1 file changed, 1 insertion(+)
create mode 100644 some_fancy_code.ending
git remote add origin git@gitlab.your.domain:user/myproject.git # adds a remote repo on your currently created git server, no Output
git push origin master # pushes our changes to the master branch
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 210 bytes | 210.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: The private project user/myproject was successfully created.
remote:
remote: To configure the remote, run:
remote: git remote add origin git@gitlab.your.domain:user/myproject.git
remote:
remote: To view the project, visit:
remote: https://gitlab.your.domain/user/myproject
remote:
To gitlab.user.domain:user/myproject.git
* [new branch] master -> master
You may also just stay logged in with your browser and click on the “Plus” in the top right “corner” and then select project.
This way you will easily create a new project, but you will first have to check it out in order to work with it
checking out the project
git clone git@gitlab.your.domain:user/myproject.git
If you want to check out your project elsewhere or even another project, use git clone.
this works with https too (as do all commands described here), git clone https://gitlab.your.domain/user/myproject.gi
updating an already cloned project from the remote server
cd myproject
git pull
this pulls the changes from your remote server for the checked out branch (at the moment this should be master).
It’s best to do this every time you enter the local repository (your directory), if only to avoid merge conflicts as much as possible.
using branches
~/myproject $ git checkout -b testbranch
Switched to a new branch 'testbranch'
Branching is a great way to work on a copy of your project and change things, without breaking your “working” or “production” branch, ideally master (but branch as much as you like).
It is good practice (in teams even more then when working alone but it’s always useful) to create a branch of your working code, work on it and then commit it, test it on (e.g.) a testsystem, let it be reviewed by others or whatever and only merge it into your stable branch when you are sure it works (or at least doesn’t break anything).
~/myproject $ vi some_fancy_code.ending # add some more text
git add some_fancy_code.ending
git commit -m "add more text"
git push --set-upstream origin testbranch
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 244 bytes | 22.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for testbranch, visit:
remote: https://gitlab.your.domain/user/myproject/merge_requests/new?merge_request%5Bsource_branch%5D=testbranch
remote:
To gitlab.user.domain:user/myproject.git
* [new branch] testbranch -> testbranch
Branch 'testbranch' set up to track remote branch 'testbranch' from 'origin'.
In your new branch you did some changes to the file, re-added it, commited it with a message thats says clearly what you just did and then pushed it to a new branch (git will complain if you just use push, because there is no upstream branch).
It then created a merge request, which you can view in your browser and merge into your master branch, after you are sure that everything works as expected.
That’s basically it. You have a functional gitlab server and a first project to work on.
(automation pipelines and runner-configuration as well as kubernetes connection will be covered in separate posts).
Tips:
git status
On branch testbranch
Your branch is up to date with 'origin/testbranch'.
nothing to commit, working tree clean
use this often to be confident about your environment, this command tells you most things you might forget about otherwise.
source ~/.git-completion.bash
PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\]@ \w\[\033[0;32m\] [$(git branch 2>/dev/null | grep "^*" | colrm 1 2)\[\033[0;32m\]]\[\033[0m\033[0;32m\]\$\[\033[0m\033[0;32m\]\[\033[0m\]'
You may even install the bash completion for git (e.g. apt-get install bash-completion, then, if installed, enter something like the above in your .bashrc.
After installation you’ll have to manually copy the file from /usr/share/bash-completion/completions/git to (e.g.) ~/.git-completion.bas
This gives you autocompletion for git commands and a nice command prompt, which always show the branch you are currently in.