{"id":315,"date":"2020-05-06T00:52:36","date_gmt":"2020-05-05T22:52:36","guid":{"rendered":"https:\/\/nubisoft.io\/blog\/?p=315"},"modified":"2020-05-06T12:53:37","modified_gmt":"2020-05-06T10:53:37","slug":"configuring-gitlab-runner-for-node-projects","status":"publish","type":"post","link":"https:\/\/nubisoft.io\/blog\/configuring-gitlab-runner-for-node-projects\/","title":{"rendered":"Configuring GitLab Runner for Node projects"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">If you want to build efficient CI\/CD pipelines in GitLab CI you&#8217;ll probably end up using own Runners. In this article, we will show you how to install and configure them for a Node project.<\/h2>\n\n\n\n<p>This is the first post from our <a href=\"https:\/\/nubisoft.io\/blog\/robust-ci-cd-pipelines-for-node-based-projects\/\">series<\/a> about building robust CI\/CD pipelines for Node-based projects, wherein we will focus on how to prepare the infrastructure for pipeline execution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>In <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.gitlab.com\/ee\/ci\/\" target=\"_blank\">GitLab<\/a> CI a Runner is a <a rel=\"noreferrer noopener\" href=\"https:\/\/gitlab.com\/gitlab-org\/gitlab-runner\" target=\"_blank\">dedicated component<\/a> that is responsible for executing jobs that are part of a given pipeline. So essentially, it&#8217;s a piece of code which is running on some VPS or VM and can build, test and deploy your code. As a part of a free plan, GitLab gives you Shared Runners for free &#8211; at least for first 2000 minutes per month, but as you may suspect they are not very powerful in terms of CPU and RAM (see here for detailed specification). If you want to reduce the time needed to execute CI\/CD pipeline, wise option would be to spin up a few VPS in cloud providers of your choice (we use <a rel=\"noreferrer noopener\" href=\"https:\/\/www.digitalocean.com\/products\/droplets\/\" target=\"_blank\">Digital Ocean<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/vpsdime.com\/\" target=\"_blank\">VPSDime<\/a>) and use them instead of shared machines on GitLab.<\/p>\n\n\n\n<p>Let&#8217;s see how to do that!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installation<\/h3>\n\n\n\n<p>In order to install Runner on Linux machine, first add the GitLab package repository and then install it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ curl -L https:\/\/packages.gitlab.com\/install\/repositories\/runner\/gitlab-runner\/script.deb.sh | sudo bash\n$ sudo apt-get install gitlab-runner<\/pre>\n\n\n\n<p>Because we will be using Docker please ensure it is also <a href=\"https:\/\/docs.docker.com\/engine\/install\/\" target=\"_blank\" rel=\"noreferrer noopener\">installed<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Registration<\/h3>\n\n\n\n<p>When Runner is installed on a target machine it can be now registered. Preferably, register it as a <a href=\"https:\/\/docs.gitlab.com\/ee\/ci\/runners\/#registering-a-group-runner\">group Runner<\/a> so that it can pickup jobs from any project under that group (more about project groups can be found in the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.gitlab.com\/ee\/user\/group\/\" target=\"_blank\">documentation<\/a>).<\/p>\n\n\n\n<p>Before starting the registration process, go to GitLab and open your group <a href=\"https:\/\/gitlab.com\/groups\/codinglab-team\/-\/settings\/ci_cd\">CI\/CD settings page<\/a>, expand <em>Runners<\/em> and go to <em>Set up a group Runner manually<\/em> section. Copy URL and token.<\/p>\n\n\n\n<p>Now, on a target machine <a href=\"https:\/\/docs.gitlab.com\/runner\/register\/\">register the Runner<\/a> by issuing the following command in terminal:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo gitlab-runner register<\/pre>\n\n\n\n<p>Enter the copied URL and token, optionally specify <a href=\"https:\/\/docs.gitlab.com\/ee\/ci\/runners\/#using-tags\">tags<\/a> and use <a href=\"https:\/\/docs.gitlab.com\/runner\/executors\/docker.html\">docker<\/a> as an <em>Executor<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Configuration<\/h3>\n\n\n\n<p>Before Runner can execute any job it must be first configured. To set up it&#8217;s parameters issue the following command in the terminal:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo nano \/etc\/gitlab-runner\/config.toml<\/pre>\n\n\n\n<p>Modify the following keys:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">concurrent = 4\n[[runners]]\n  environment = [\"DOCKER_TLS_CERTDIR=\"]\n  [runners.docker]\n    privileged = true<\/pre>\n\n\n\n<p>The <code>concurrent<\/code> key controls how many parallel jobs can be executed by this Runner, so the value depends on how many CPUs and RAM you have available. Setting the <code>environment<\/code> key resolves the <a href=\"https:\/\/forum.gitlab.com\/t\/docker-dind-stops-working-after-12-1-0-update\/28664\/2\">issue<\/a> that pops up if <a href=\"https:\/\/hub.docker.com\/_\/docker?tab=description\">dind<\/a> image is used in the pipeline. The <code>privileged<\/code> key set to <code>true<\/code> also resolves the <a href=\"https:\/\/gitlab.com\/gitlab-org\/gitlab-runner\/issues\/1544\">issue<\/a> with <code>dind<\/code>. Remember that enabling this flag can cause serious security risks which are well highlighted in <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.trendmicro.com\/trendlabs-security-intelligence\/why-running-a-privileged-container-in-docker-is-a-bad-idea\/\" target=\"_blank\">this<\/a> blog post &#8211; use it only if you fully control the pipelines.<\/p>\n\n\n\n<p>After configuration was changed, restart the Runner so it can take effect:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo gitlab-runner restart<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Cache<\/h4>\n\n\n\n<p>In order to speed up pipeline executions, configure the Runner to use <a href=\"https:\/\/aws.amazon.com\/s3\/\">S3<\/a> object store as a cache. Apply the following snippet to the <code>\/etc\/gitlab-runner\/config.toml<\/code> file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[[runners]]\n  [runners.cache]\n    Type = \"s3\"\n    Path = \"jobs-cache\"\n    Shared = true\n    [runners.cache.s3]\n      ServerAddress = \"s3.amazonaws.com\"\n      AccessKey = \"[ACCESS KEY]\"\n      SecretKey = \"[SECRET KEY]\"\n      BucketName = \"nubisoft-gitlab-ci-runners-cache\"\n      BucketLocation = \"eu-central-1\"\n      Insecure = false<\/pre>\n\n\n\n<p>Replace <code>ACCESS_KEY<\/code> and <code>SECRET_KEY<\/code> with values obtained in AWS console when created dedicated IAM user.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Private Docker Registry<\/h4>\n\n\n\n<p>To pull Docker images from private <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.gitlab.com\/ee\/user\/packages\/container_registry\/\" target=\"_blank\">GitLab Registry<\/a> in CI pipelines login to the registry with the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo docker login registry.gitlab.com<\/pre>\n\n\n\n<p>As a credentials use <a href=\"https:\/\/docs.gitlab.com\/ee\/user\/project\/deploy_tokens\/\">Deploy token<\/a> created for a group. Remember to assign <code>read_registry<\/code> scope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>And that&#8217;s it! Your fresh Runner is up and running, ready to accept jobs. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to build efficient CI\/CD pipelines in GitLab CI you&#8217;ll probably end up using own Runners. In this article, we will show you how to install and configure them for a Node project. This is the first post from our series about building robust CI\/CD pipelines for Node-based projects, wherein we will focus [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_case_study_excerpt":"","footnotes":""},"categories":[55,5],"tags":[48,47,57,59,60],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-devops","category-infrastructure","tag-cd","tag-ci","tag-gitlab","tag-node","tag-pipelines"],"_links":{"self":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":15,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":334,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/315\/revisions\/334"}],"wp:attachment":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}