Gitlab CI-CD

September 16, 2018 DevOps 1 minute, 36 seconds

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

More install options are available

Run in your runner host (here in the docker container)

sudo gitlab-runner register
  • Get your token from /admin/runners of your Gitlab instance
  • Set your executioner (here docker)
  • No tags, allow untagged jobs, no shared runner (without those settings the runner stuck)

add and push .gitlab-ci.yml in your project root

image: maven:latest

verify:
  stage: build
  script:
    - mvn verify

build:
  stage: build
  script:
    - mvn compile

Recommended: wildcard domain and corresponding SSL certificate

  1. Assign a runner to a group of projects in Gitlab runner
  2. Start runner docker run -d --name gitlab-runner-public --restart always -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
  3. Register runner The docker image used can be overwritten in the .gitlab-ci.yml of your pipeline

    1. interactive: docker exec -it gitlab-runner-public bash
      • gitlab-runner register
      • docker as default executioner
      • alpine as default image
    2. or non interactive
      docker run --rm -t -i -v /path/to/config:/etc/gitlab-runner --name gitlab-runner gitlab/gitlab-runner register \
                          --non-interactive \
                          --executor "docker" \
                          --docker-image alpine:3 \
                          --url "https://gitlab.com/" \
                          --registration-token "PROJECT_REGISTRATION_TOKEN" \
                          --description "docker-runner" \
                          --tag-list "docker,aws" \
                          --run-untagged \
                          --locked="false"
  4. Setup Pages (wildcard domain with TLS support) gitlab.rb
            pages_external_url "https://pages.rootknecht.net/"
            gitlab_pages['enable'] = true
            gitlab_pages['inplace_chroot'] = true # in case of gitlab-pages daemon not starting due to bind mount permission error
            pages_nginx['redirect_http_to_https'] = true
            pages_nginx['ssl_certificate'] = "/etc/gitlab/ssl/pages-nginx.crt"
            pages_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/pages-nginx.key"

    Reconfigure and restart

        gitlab-ctl reconfigure
        gitlab-ctl restart gitlab-pages

    Debugging

        # get logs (run in container)
        gitlab-ctl tail gitlab-pages
        # path of your pages (in container)
        ls /var/opt/gitlab/gitlab-rails/shared/pages
        # error log of bundled nginx
        /var/log/gitlab/nginx/gitlab_pages_error.log

Publish static html content with following .gitlab-ci.yml

image: alpine:latest
pages:
  stage: deploy
  script:
  - echo 'Nothing to do...'
  artifacts:
    paths:
    - public # this is the path in your repo to publish
  only:
  - master