GitLab CI/CD for GitHub — How and Why?
Tags: devops, node, gitlab, github
Published: 2018-03-30 20:14:08

---
title: GitLab CI/CD for GitHub — How and Why?
published: true
tags: devops,node,gitlab,github
canonical_url: https://anoff.io/blog/2018-03-30-gitlab-ci-for-github/
---

### GitLab CI/CD for GitHub — How and Why?

When creating a git project that you want to share with others you traditionally had the choice between GitHub with its huge community and tons of integrations, GitLab with a great overall dev experience from issues to one of the best CI/CD solutions out there and BitBucket being one of the friends you have since kindergarden. My personal decision was to host all my personal projects on [🦑 GitHub](https://github.com/anoff). For projects that need CI/CD I tinkered around with [👷‍♂️ Travis CI](https://travis-ci.org/) and [🅾️ Circle CI](https://circleci.com/) on top of GitHub.

![](https://cdn-images-1.medium.com/max/912/1*laA78JtrefVUbLNEXjw_cg.png)<figcaption>It is no longer GitHub OR GitLab</figcaption>

But recently [GitLab announced](https://about.gitlab.com/features/github/) that their amazing CI/CD solution can now be combined with git projects hosted on Github. Having used a fullstack GitLab solution at work for the last 1.5 years I was really psyched to test it for my personal projects. Now you can finally have the best of both worlds — GitHubs reach in the community and GitLabs CI tooling 💃

### How to enable a GitLab CI/CD pipeline for a GitHub project

At first glance GitLab integrates like any other CI/CD service into your project. Using webhooks that the service either adds for you automatically or you define manually. GitLab did a pretty good job on describing [how to set those up](https://docs.gitlab.com/ee/ci/ci_cd_for_external_repos/github_integration.html) so I wont repeat the steps again in detail.

![](https://cdn-images-1.medium.com/max/729/1*0yMTq5LZdnbUwp7ykpHppw.png)<figcaption>CI/CD integration into GitHub via webhook</figcaption>

To set it up you first need to have your project on GitHub. I set up a small [node.JS web app](https://github.com/anoff/node-ci-dummy) to play around with the integration. Feel free to fork it or create your own. Next you head over to [GitLab](https://gitlab.com/) sign in — I suggest using GitHub OAUTH to keep your code credentials in one place. To set up your GitLab CI/CD project simply create a new project and choose the option on the far right CI/CD for external repo and click GitHub.

![](https://cdn-images-1.medium.com/max/670/1*ZNUtH4F0iN7XtRScmcRQcw.png)

![](https://cdn-images-1.medium.com/max/1024/1*iIzoz5QkpUTdz8nMOYFmtg.png)

You will see a list of your GitHub repos and hit Connect on the one that you want to use for this integration. In my case it is the repo anoff/node-ci-dummy

![](https://cdn-images-1.medium.com/max/1024/1*1vQDUTpD07NZ-A4_44LrqA.png)

![](https://cdn-images-1.medium.com/max/1024/1*8cQIG5Jw6i_FlEfcVE-MJg.png)

After doing this you will see that GitLab actually cloned [your entire project](https://gitlab.com/anoff/node-ci-dummy). There are two settings that make sure this magic 🧙‍♀️ works. One is the previously mentioned webhook on the GitHub side that will trigger as soon as you git push onto your GitHub repository. The other is the repository settings on your newly created GitLab project that is set to PULL from a remote repository — namely the GitHub project you selected.

![](https://cdn-images-1.medium.com/max/1024/1*KJfhmNFMagGt6AxTTOj0eg.png)

![](https://cdn-images-1.medium.com/max/1024/1*2lI94T72HdmERMgxYJKr_Q.png)<figcaption>GitHub webhook (left) and GitLab repo mirror options (right)</figcaption>

### How to configure a CI pipeline

The final thing to do is to set up an actual CI pipeline in your repository. GitLab will automatically recognize the pipeline definition and run the defined jobs. If you are unfamiliar with GitLab CI you might want to start with a minimalist pipeline definition. Just create a .gitlab-ci.yml in your root directory. Example content to test a node.JS app:

```yaml
image: node:9
lint:
  script:
  - npm install
  - npm run lint

unit test:
  script:
  - npm install
  - npm test
```

In my dummy repository this will run [standard](https://standardjs.com/) linting and [ava](https://github.com/avajs/ava) unit tests in parallel. Below you can see the commit that introduced this file and the respective [pipeline that was triggered](https://gitlab.com/anoff/node-ci-dummy/pipelines/19765773).

![](https://cdn-images-1.medium.com/max/966/1*eeV-370AqhtP_u7pn4vUyw.png)<figcaption>GitHub commit view with a pending GitLab pipeline</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*kyIC64kaAg1JGARmQnKieQ.png)<figcaption>GitLab view of the pipeline in process</figcaption>

![](https://cdn-images-1.medium.com/max/272/1*Mx5QNLDV3WDJbLVQzg8DjA.png)<figcaption>Success marker on GitHub after the CI pipeline finished</figcaption>

Check out [the docs](https://docs.gitlab.com/ee/ci/yaml/) for more info writing custom pipelines. For example introducing stages might help you get a handle on more complex pipeline workflows. If you are running a public GitHub project and you want guests to see your pipeline results in detail you should make sure your GitLab project is set to  **Public**.

![](https://cdn-images-1.medium.com/max/882/1*CwOSDGb_fuuDyIEeBYzHyA.png)

### Final thoughts

This post covered how to set up a basic integration workflow between GitHub and GitLab: not a lot of work 🏋️‍♀️ for you actually.

Things to do next:

- introduce [multiple stages](https://docs.gitlab.com/ee/ci/yaml/#stages) to your pipeline
- test against various [runtimes](https://docs.gitlab.com/ee/ci/yaml/#image-and-services)
- make use of the service tag in GitLab pipelines to spawn a database to run smaller integration tests

In my opinion the only downside of the GitLab CI/CD compared to other solutions like Travis, Circle, Drone is that the fact that GitLab creates a fully featured project for you might create some confusion for people. One thing you should definitely do is follow the permission settings above and disable _issues_, _wiki_, _pull requests_ on your GitLab repositiry in this case. That way your permissions will prevent people from interacting with you via GitLab. In addition your project automatically shows up as Mirrored from which links back to your GitHub master project. If that is not enough for you, you can always add a link in your README and your contribution guidelines.

![](https://cdn-images-1.medium.com/max/989/1*_pSl5B56gb6Ft7-4IHAmjg.png)<figcaption>GitLab project showing link to the original project</figcaption>

Oh btw if you are still looking for the **Why** that the title promised: GitLabs CI/CD solution is just one of the most comprehensive out there imo.

Feel free to discuss on [twitter](https://twitter.com/an0xff) or comments 👍