from Gitlab

Deployments with Gitlab #

Gitlab is GIT-based version control server. They offer a cloud solution (hosted/managed) and also allow for on-premise installations. The software is free for most cases. Here’s a Gitlab CI Pipeline to deploy the Wordpress example application via Gitlab to our platform using their managed offering on Gitlab.com.

This Pipeline has multiple stages which assume a PR-based workflow and try to validate and test as much as they can before the deploy stage is executed.

Requirements #

Gitlab Pipeline #

The following is in your .gitlab-ci.yml, which is part of your project:

# re-usable
.jobdef: &job-docker-def
  image: docker:latest
  stage: build
  services:
    - docker:dind
  # this requires the variables to be defined in project settings
  before_script:
    - docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD" $DOCKER_REGISTRY

.jobcompose: &job-compose-def
  image:
    name: docker/compose:1.24.1
    entrypoint: ["/bin/sh", "-c"]
  stage: test
  services:
    - docker:dind

# these are stages
stages:
  - build
  - test
  - predeploy
  - deploy

# inline variables :-)
variables:
  DOCKER_REGISTRY: registry.gitlab.com
  NGINX_IMAGE: $DOCKER_REGISTRY/YOUR-NAME/YOUR-PROJECT/nginx
  QUANTUM_CLI: r.planetary-quantum.com/quantum-public/cli:2
  DOCKER_SERVER: YOUR-CLUSTER
  STACK_NAME: your-cluster-wordpressdemo

# these are our jobs
test-compose:
  <<: *job-compose-def
  before_script:
    - docker-compose version
  script:
    - docker-compose config -q

test-compose-dev:
  <<: *job-compose-def
  script:
    - docker-compose -f docker-compose.dev.yml config -q
  except:
    - master

test-quantum-conf:
  image: $QUANTUM_CLI
  stage: test
  script:
    - quantum-cli validate

test-quantum-credentials:
  image: $QUANTUM_CLI
  stage: test
  script:
    - quantum-cli ep ls

build-nginx-pr:
  <<: *job-docker-def
  script:
    - docker build -t $NGINX_IMAGE:$CI_COMMIT_SHORT_SHA -f Dockerfile.nginx .
  except:
    - master

build-nginx-master:
  <<: *job-docker-def
  script:
    - docker build -t $NGINX_IMAGE:latest -f Dockerfile.nginx .
    - docker push $NGINX_IMAGE:latest
  only:
    - master

# the following requires the QUANTUM_USER and QUANTUM_PASSWORD env variables
update-website:
  image: $QUANTUM_CLI
  stage: deploy
  # list endpoints (as a test)
  before_script:
    - quantum-cli e ls
  script:
    - quantum-cli stack update --create --endpoint $DOCKER_SERVER --stack $STACK_NAME
  only:
    - master