Skip to content
Projekte

Über uns

Reusable CI script functions for Kubernetes, RabbitMQ, PostgreSQL etc

Systeme und Infrastruktur

So werden diese Zahlen gemessen

Gemessener Sternzuwachs

in 24h
0
in 7 days
Verlauf wird gesammelt
in 15 days
Verlauf wird gesammelt
in 1 month
Verlauf wird gesammelt

Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.

README.md

Original

About

Functions to create dynamic envs per $CI_COMMIT_REF_SLUG.

  • Rancher project namespaces
  • RabbitMQ virtual hosts
  • Docker Registry parallel logins
  • PostgreSQL databases

Kubectl, rancher cli, helm calls are parallel job safe.

Usage

Add this repo as Git Submodule to a project

git submodule add --name .gitlab-ci-functions -b master -- https://github.com/microdevops-com/gitlab-ci-functions.git .gitlab-ci-functions

P.S. You can directly fetch code from https://raw.githubusercontent.com each time pipeline runs, it is a quick way to start, but not a smart way to use permanently.

before_script:
  - rm -f kubernetes.sh && curl -L -s -o kubernetes.sh https://raw.githubusercontent.com/microdevops-com/gitlab-ci-functions/master/kubernetes.sh && . ./kubernetes.sh
  - ...

...

after_script:
  - . ./kubernetes.sh
  - rancher_logout
  - ...

Pipeline

Add the following to .gitlab-ci.yml depending on needs:

variables:
  GIT_SUBMODULE_STRATEGY: normal

before_script:
  - . .gitlab-ci-functions/kubernetes.sh
  - . .gitlab-ci-functions/docker.sh
  - . .gitlab-ci-functions/rabbitmq.sh
  - . .gitlab-ci-functions/postgresql.sh
  # this vars are available to script but not available to yml
  - KUBE_NAMESPACE=$(kubernetes_namespace_sanitize $RANCHER_PROJECT-$CI_COMMIT_REF_SLUG)
  - RABBITMQ_VHOST=$(rabbitmq_vhost_sanitize $RABBITMQ_VHOST_PREFIX-$CI_COMMIT_REF_SLUG)
  - registry_login
  - rancher_login
  - helm_cluster_login

after_script:
  - . .gitlab-ci-functions/kubernetes.sh
  - rancher_logout
  - helm_cluster_logout

...

prepare_rabbitmq_vhost:
  stage: prerequisites
  script:
    - rabbitmq_create_vhost $RABBITMQ_VHOST
    - rabbitmq_add_permission $RABBITMQ_VHOST $RABBITMQ_USER

prepare_postgresql_db:
  stage: prerequisites
  script:
    - postgresql_create_db $POSTGRESQL_DB_PREFIX-$CI_COMMIT_REF_SLUG
  

prepare_rancher_namespace:
  stage: prerequisites
  script:
    - rancher_namespace
    - namespace_secret_project_registry
    - namespace_secret_rabbitmq rabbitmq
    - helm_init_namespace

my-app:
  stage: app_deploy
  script:
    - helm_deploy my-app $CI_COMMIT_REF_SLUG
    #- helm_deploy my-app $CI_COMMIT_REF_SLUG "--set env=dev"

Envs

Required envs for CI:

RANCHER_PROJECT=Project

RABBITMQ_HOST=rabbitmq.example.com
RABBITMQ_PORT=5672
RABBITMQ_MANAGEMENT_PORT=15672
RABBITMQ_USER=project-user
RABBITMQ_PASS=PASS1
RABBITMQ_MANAGEMENT_USER=project-admin
RABBITMQ_MANAGEMENT_PASS=PASS2
RABBITMQ_VHOST_PREFIX=project
POSTGRESQL_HOST=postgresql.example.com
POSTGRESQL_PORT=5432
POSTGRESQL_USER=project-user
POSTGRESQL_PASS=PASS2
POSTGRESQL_DB_PREFIX=project

KUBE_SERVER=https://rancher.example.com/k8s/clusters/local
KUBE_TOKEN=kubeconfig-u-xxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[email protected]

Docker app deploy via Salt (misc.sh)

For hosts managed by the app.docker state of microdevops-formula. All functions ssh as root to the target; port comes from DOCKER_SERVER_SSH_PORT (NGINX_SERVER_SSH_PORT for ssh_copy_from_docker).

Function Purpose
ssh_salt_call_app_docker SERVER IMAGE APP [EXEC] salt-call state.apply app.docker for one app with a new image. Env: DEPLOY_PRE_START=true also runs the app's pre_start with the same image; SALT_CALL_EXTRA_ARGS (e.g. --retcode-passthrough --state-output=mixed)
ssh_copy_from_docker SERVER IMAGE SOURCE TARGET STRIP [EXCLUDES] Copy a path from the image to the host (e.g. public/ for a host nginx)
ssh_install_file SERVER SOURCE TARGET [OWNER] [MODE] Upload a file (e.g. a GitLab File variable) only if changed; prints changed/unchanged
ssh_docker_container_id SERVER CONTAINER Container ID, empty if missing
ssh_docker_restart SERVER CONTAINER [STOP_TIMEOUT] Restart a container
ssh_docker_wait_healthy SERVER CONTAINER [TIMEOUT] Wait for the image healthcheck; fail with log tail on timeout
deploy_dev:
  script:
    - . .gitlab-ci-functions/misc.sh
    - ssh_copy_from_docker "$DEPLOY_SERVER" "$IMAGE" app/public "/var/www/$DEPLOY_APP/public" 2
    - ENV_STATE=$(ssh_install_file "$DEPLOY_SERVER" "$APP_ENV_FILE" "/var/www/$DEPLOY_APP/.env.local" 1000:1000 0400)
    - OLD_ID=$(ssh_docker_container_id "$DEPLOY_SERVER" "app-$DEPLOY_APP")
    - DEPLOY_PRE_START=true SALT_CALL_EXTRA_ARGS=--retcode-passthrough ssh_salt_call_app_docker "$DEPLOY_SERVER" "$IMAGE" "$DEPLOY_APP"
    # salt does not recreate the container when only the env file changed
    - if [ "$ENV_STATE" = changed ] && [ "$OLD_ID" = "$(ssh_docker_container_id "$DEPLOY_SERVER" "app-$DEPLOY_APP")" ]; then ssh_docker_restart "$DEPLOY_SERVER" "app-$DEPLOY_APP" 60; fi
    - ssh_docker_wait_healthy "$DEPLOY_SERVER" "app-$DEPLOY_APP"

Ähnliche Projekte entdecken

Wird geladen…