{"id":31,"date":"2019-05-06T22:13:43","date_gmt":"2019-05-06T22:13:43","guid":{"rendered":"http:\/\/blog.nikster.de\/wordpress\/?p=31"},"modified":"2019-09-03T10:35:16","modified_gmt":"2019-09-03T08:35:16","slug":"how-to-install-gitlab-and-work-with-it","status":"publish","type":"post","link":"https:\/\/blog.nikster.de\/wordpress\/index.php\/2019\/05\/06\/how-to-install-gitlab-and-work-with-it\/","title":{"rendered":"How to install gitlab and work with it"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">What is git?<\/h4>\n\n\n\n<p>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.<br>It&#8217;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.<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What&#8217;s this about?<\/h4>\n\n\n\n<p>In this post I&#8217;ll cover the basics on how to install a gitlab server &#8220;manually&#8221; with the omnibus installer (this is pretty straightforward as it brings it&#8217;s own chef installer) and getting started with basic commands and workflows.<br>If you despise &#8220;manual&#8221; installations entirely, you may take a look at the puppetlabs module (which also uses omnibus, which utilizes chef).<br>However, I for one strongly believe in doing things myself at least one time,  for a better understanding of how they work.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Prerequisites<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>a VM\/Server with at least 6GB RAM and debian9<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Installation<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install -y curl openssh-server ca-certificates<\/code><\/pre>\n\n\n\n<p>especially ca-certificates are important<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo postfix postfix\/main_mailer_type select \"Internet Site\" | debconf-set-selections\necho postfix postfix\/mailname select `hostname -f` | debconf-set-selections\necho unattended-upgrades unattended-upgrades\/enable_auto_updates boolean true | debconf-set-selections<\/code><\/pre>\n\n\n\n<p>Gitlab requires postfix and postfix (even in this very basic setup) requires some input.<br>Thats what debconf is used for here, for pre-defining the needed values for the unattended (not in our case) installation. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get -y install postfix unattended-upgrades<\/code><\/pre>\n\n\n\n<p>Install postfix.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -sS https:\/\/packages.gitlab.com\/install\/repositories\/gitlab\/gitlab-ce\/script.deb.sh | bash<\/code><\/pre>\n\n\n\n<p>The above script detects your OS, adds the correct repository to your sources.list and imports the gpg key.<br>You may as well fetch the with wget and then import it with apt-key add.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install -y gitlab-ce<\/code><\/pre>\n\n\n\n<p>Let&#8217;s install gitlab. This will take some time while chef does it&#8217;s thing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/etc\/hosts\n127.0.1.1       gitlab.your.domain        gitlab\n192.168.122.123 gitlab.your.domain        gitlab<\/code><\/pre>\n\n\n\n<p>Double check that your \/etc\/hosts entries fit, gitlab relies on it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp \/home\/user\/gitlab.your.domain-key.pem \/etc\/gitlab\/ssl\/gitlab.your.domain.key\ncp \/home\/user\/gitlab.your.domain-pub.pem \/etc\/gitlab\/ssl\/gitlab.your.domain.crt\n\nSkip this step if your certs are not self signed:\n\nmv \/home\/user\/ca-root.pem \/usr\/local\/share\/ca-certificates\/ca-root.crt\nupdate-ca-certificates <\/code><\/pre>\n\n\n\n<p>Now get yourself an <a href=\"https:\/\/blog.nikster.de\/wordpress\/index.php\/2019\/05\/05\/how-to-ssl-certificates-for-your-services\/\">ssl certificate<\/a> for your domain, either self signed or maybe through a service like letsencrypt and copy it to \/etc\/gitlab\/ssl.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/etc\/gitlab\/gitlab.rb\n\nexternal_url 'https:\/\/gitlab.your.domain'\n\ngitlab-ctl reconfigure\ngitlab-ctl restart<\/code><\/pre>\n\n\n\n<p>You should now be able to access gitlab with a browser on https:\/\/gitlab.your.domain. <br>Create yourself a password for the root account and then create a user.<br><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>If something does not work<\/strong>, here are some troubleshooting tips:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gitlab-rake gitlab:check SANITIZE=true<\/code><\/pre>\n\n\n\n<p>does a config check<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gitlab-ctl status<\/code><\/pre>\n\n\n\n<p>shows the status of the various gitlab processes<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tail -f \/var\/log\/gitlab\/*<\/code><\/pre>\n\n\n\n<p>gitlab is pretty verbose about it&#8217;s actions though it can be somewhat hard to figure out which component is failing because there are lots of them.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">working with git &#8211; the basics<\/h3>\n\n\n\n<p>I recommend using git (from the commandline) with ssh, therefore you should:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Login as your user via the webinterface<\/li><li>click on the icon in the top right corner and then settings<\/li><li>on the sidebar to the left, click on SSH KEYS<\/li><li>upload an ssh key<\/li><li>test your connection by logging in via ssh (ssh git@gitlab.your.domain)<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">creating a new project<\/h4>\n\n\n\n<p>Commandline:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir myproject\nvi some_fancy_code.ending\ngit init \nInitialized empty Git repository in \/home\/user\/myproject\/.git\/\ngit add --all                                                    # No Output, tells git to track the file, in this case all of them\ngit commit -m \"initial commit\" # commits to the local repository\n[master (root-commit) ec268cc] initial commit\n 1 file changed, 1 insertion(+)\n create mode 100644 some_fancy_code.ending\ngit remote add origin git@gitlab.your.domain:user\/myproject.git # adds a remote repo on your currently created git server, no Output\ngit push origin master                                          # pushes our changes to the master branch \nEnumerating objects: 3, done.\nCounting objects: 100% (3\/3), done.\nWriting objects: 100% (3\/3), 210 bytes | 210.00 KiB\/s, done.\nTotal 3 (delta 0), reused 0 (delta 0)\nremote: \nremote: The private project user\/myproject was successfully created.\nremote: \nremote: To configure the remote, run:\nremote:   git remote add origin git@gitlab.your.domain:user\/myproject.git\nremote: \nremote: To view the project, visit:\nremote:   https:\/\/gitlab.your.domain\/user\/myproject\nremote: \nTo gitlab.user.domain:user\/myproject.git\n * [new branch]      master -> master<\/code><\/pre>\n\n\n\n<p>You may also just stay logged in with your browser and click on the &#8220;Plus&#8221; in the top right &#8220;corner&#8221; and then select project.<br>This way you will easily create a new project, but you will first have to check it out in order to work with it<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">checking out the project<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone git@gitlab.your.domain:user\/myproject.git<\/code><\/pre>\n\n\n\n<p>If you want to check out your project elsewhere or even another project, use git clone.<br>this works with https too (as do all commands described here), git clone https:\/\/gitlab.your.domain\/user\/myproject.gi<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">updating an already cloned project from the remote server<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>cd myproject\ngit pull<\/code><\/pre>\n\n\n\n<p>this pulls the changes from your remote server for the checked out branch (at the moment this should be master).<br>It&#8217;s best to do this every time you enter the local repository (your directory), if only to avoid merge conflicts as much as possible. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">using branches<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/myproject $ git checkout -b testbranch\nSwitched to a new branch 'testbranch'<\/code><\/pre>\n\n\n\n<p>Branching is a great way to work on a copy of your project and change things, without breaking your &#8220;working&#8221; or &#8220;production&#8221; branch, ideally master (but branch as much as you like).<br>It is good practice (in teams even more then when working alone but it&#8217;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&#8217;t break anything).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>~\/myproject $ vi some_fancy_code.ending # add some more text\ngit add some_fancy_code.ending\ngit commit -m \"add more text\"\ngit push --set-upstream origin testbranch\n\nEnumerating objects: 5, done.\nCounting objects: 100% (5\/5), done.\nWriting objects: 100% (3\/3), 244 bytes | 22.00 KiB\/s, done.\nTotal 3 (delta 0), reused 0 (delta 0)\nremote: \nremote: To create a merge request for testbranch, visit:\nremote:   https:\/\/gitlab.your.domain\/user\/myproject\/merge_requests\/new?merge_request%5Bsource_branch%5D=testbranch\nremote: \nTo gitlab.user.domain:user\/myproject.git\n * [new branch]      testbranch -> testbranch\nBranch 'testbranch' set up to track remote branch 'testbranch' from 'origin'.<\/code><\/pre>\n\n\n\n<p> 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).<br>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.<br><\/p>\n\n\n\n<p><strong>That&#8217;s basically it. You have a functional gitlab server and a first project to work on.<\/strong><br>(automation pipelines and runner-configuration as well as kubernetes connection will be covered in separate posts). <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Tips:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>git status\nOn branch testbranch\nYour branch is up to date with 'origin\/testbranch'.\n\nnothing to commit, working tree clean<\/code><\/pre>\n\n\n\n<p>use this often to be confident about your environment, this command tells you most things you might forget about otherwise.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source ~\/.git-completion.bash\nPS1='\\[\\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\\]'<\/code><\/pre>\n\n\n\n<p>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.<br>After installation you&#8217;ll have to manually copy the file from  \/usr\/share\/bash-completion\/completions\/git to (e.g.) ~\/.git-completion.bas<\/p>\n\n\n\n<p>This gives you autocompletion for git commands and a nice command prompt, which always show the branch you are currently in.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/blog.nikster.de\/wordpress\/index.php\/2019\/05\/08\/how-to-install-and-connect-a-gitlab-runner\/\">Set up a gitlab runner to build and test your code \/ configs<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s a full blown devops-tool nowadays and besides keeping track of your code, you can configure build- and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.nikster.de\/wordpress\/index.php\/2019\/05\/06\/how-to-install-gitlab-and-work-with-it\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to install gitlab and work with it&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[15,13,14,12],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-git","tag-debian","tag-git","tag-gitlab","tag-howto","entry"],"_links":{"self":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":4,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/31\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nikster.de\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}