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

Local Installation on Mac OS X

  • see (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

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

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

  # trust
  - mkdir ~/.ssh/
  - touch ~/.ssh/known_hosts
  - ssh-keyscan >> /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
  - ssh-agent sh -c "ssh-add ansible-project-deploy-key.pem; git clone 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/*

  - deploy

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

    - ./deploy stage 0-local.yml
    - ./deploy stage 3-deploy-site.yml
  stage: deploy
    - stage

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