gitlab ci – execute .gitlab-ci.yml locally with gitlab runner (gitlab-ci-multi-runner)

Local Installation on Mac OS X

  • see https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/install/osx.md (you dont have to register your local runner with gitlab)
  • almost certainly you want to run the runner in a docker container. Install docker.
  • add a public docker image to your .gitlab-ci.yml (for example add image: python:alpine as first line of your .gitlab-ci.yml or select another one from here https://store.docker.com)

Local Execution

  • go to your project’s root
  • execute gitlab-ci-multi-runner exec docker deploy_dev (where deploy_dev is the job defined in the projects .gitlab-ci.yml)

If you have variables in your .gitlab-ci.yml (and almost certainly you will) you can load values locally into these variables as follows:

gitlab-ci-multi-runner exec docker deploy_dev --env ANSIBLE_PROJECT_DEPLOY_KEY="$ANSIBLE_PROJECT_DEPLOY_KEY" --env SECRETS_YML="$SECRETS_YML" --env SERVER_SSH_KEY="$SERVER_SSH_KEY"

Hint: whole text file contents can be loaded into bash variables as such:

SECRETS_YML=`cat ./credentials/secrets.yml`

Sample .gitlab-ci.yml

image: python:alpine

before_script:
  # install required packages
  - apk update && apk add openssh-client python git python python-dev ansible
  - pip install --upgrade pip
  - pip install boto httplib2

  # trust gitlab.com
  - mkdir ~/.ssh/
  - touch ~/.ssh/known_hosts
  - ssh-keyscan gitlab.com >> /root/.ssh/known_hosts

  # dump ssh key
  - echo "$ANSIBLE_PROJECT_DEPLOY_KEY" > ansible-project-deploy-key.pem
  - chmod 600 ansible-project-deploy-key.pem

  # Run ssh-agent (inside the build environment)
  - eval $(ssh-agent -s)

  # clone the ansible script
  # http://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use
  - ssh-agent sh -c "ssh-add ansible-project-deploy-key.pem; git clone git@gitlab.com:what-digital/deploy-cvcube.git deploy-script"

  # get deploy script
  - cd deploy-script
  - git submodule update --recursive

  # copy credentials to their place
  - mkdir credentials
  - echo "$SERVER_SSH_KEY" > credentials/server-dev.pem
  - echo "$SERVER_SSH_KEY" > credentials/server-stage.pem
  - echo "$SERVER_SSH_KEY" > credentials/server-production.pem
  - echo "$SECRETS_YML" > credentials/secrets.yml
  - chmod 600 -R credentials/*


stages:
  - deploy

deploy_production:
  script:
    - ./deploy production 0-local.yml
    - ./deploy production 3-deploy-site.yml
  stage: deploy
  only:
    - master

deploy_stage:
  script:
    - ./deploy stage 0-local.yml
    - ./deploy stage 3-deploy-site.yml
  stage: deploy
  only:
    - stage

deploy_dev:
  script:
    - ./deploy dev 0-local.yml
    - ./deploy dev 3-deploy-site.yml
  stage: deploy
  only:
    - dev