Migration to Gitlab and Gitlab Runners

Wednesday, July 25, 2018

The Linux community, and the open-source community in general, was up in arms last month as Microsoft announced a purchase of Github. For many reasons, which I am not going to detail here, I agree with the general concern and outrage over this. Due to this, I have migrated all my repos from Github (where I was a paying member) to Gitlab. The migration itself is dead easy, but with this migration you lose access to other tools. This mainly impacted me with the loss of CircleCI. Enter Gitlab runners!

One of the more interesting features of Gitlab is that they have their own continuous integration engine built into the platform. I took some time and got this working with my blog, which frankly was pretty easy. If you have worked with CircleCI before than you will instantly feel at home with the runners as the syntax is very similar. Below is the yaml for this site:

stages:
  - build
  - deploy

build:
  image: docker:latest
  services: 
    - docker:dind
  stage: build
  script:
    - docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
    - docker build -t binaryronin/website .
    - docker push binaryronin/website:latest
  only:
    - master

deploy:
  image: binaryronin/debian-openssh
  before_script:
    - 'which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)'
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh
    - echo "$DEPLOY_KEY" > ~/.ssh/id_rsa_deploy
    - chmod -R 700 ~/.ssh
  stage: deploy
  script:
    - ssh -i ~/.ssh/id_rsa_deploy -o StrictHostKeyChecking=no [email protected] /home/user/deploy.sh
  only:
    - master

You can see it has two distinct stages, build and deploy, with the build stage building and pushing the image, and the deploy stage sshing into the server and running my deploy script. Dead simple. Even has support for private variables, which I use for the Dockerhub login and the SSH key. The only thing I had to do was create a basic Debian container that had OpenSSH installed for the connection to work properly. This is available on my Docker hub if anyone else wants to use it.

devopsgithubgitlabautomation

Using Nginx in front of AWX for SSL

docker-py deprecated in favor of docker