My Gitlab CI

CI/CD in Gitlab using Docker for deployment

0

Votes

image: docker:latest
variables:
  CONTAINER_TEST_IMAGE: $CI_REGISTRY/$CI_PROJECT_NAMESPACE/ui_test:latest
  CONTAINER_IMAGE_PATH: $CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME
  DOCKER_DRIVER: overlay2
  PRODUCTION: jugatsu.xyz
  STACK_PROD: deploy/production/stack-ui.yml
  STACK_REVIEW: deploy/review/stack.yml
  STACK_PROD_NAME: prod
  MANAGER_IP: 104.199.29.228
  DEPLOY_USER: docker-user

before_script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY

stages:
  - test
  - build
  - review
  - release
  - production

.ssh_config: &ssh_config |
  apk add --no-cache openssh-client
  eval $(ssh-agent -s)
  echo "$SSH_PRIVATE_KEY" | ssh-add -

.swarm_config: &swarm_config |
  ssh -f -nNT -L /tmp/docker.sock:/var/run/docker.sock ${DEPLOY_USER}@${MANAGER_IP} -o StrictHostKeyChecking=no
  export DOCKER_HOST=unix:///tmp/docker.sock
  docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN ${CI_REGISTRY}

Unit tests:
  stage: test
  services:
    - docker:dind
  script:
    - docker run -v $PWD:/app $CONTAINER_TEST_IMAGE python3 -m unittest discover -s tests/

Build image:
  stage: build
  services:
    - docker:dind
  script:
    - docker pull ${CONTAINER_IMAGE_PATH}/build:${CI_COMMIT_REF_SLUG} || true
    - docker build -t ${CONTAINER_IMAGE_PATH}/build:$CI_COMMIT_REF_SLUG --cache-from ${CONTAINER_IMAGE_PATH}/build:$CI_COMMIT_REF_SLUG .
    - docker push ${CONTAINER_IMAGE_PATH}/build:$CI_COMMIT_REF_SLUG
  only:
    refs:
      - branches
  except:
    - master

Review:
  stage: review
  services:
    - docker:dind
  before_script:
    - *ssh_config
  script:
    - *swarm_config
    - export TAG=${CI_COMMIT_REF_SLUG}
    - export URL=$CI_COMMIT_REF_SLUG.jugatsu.xyz
    - docker stack deploy -c ${STACK_REVIEW} ${CI_PROJECT_NAMESPACE}-${CI_PROJECT_NAME}-${CI_BUILD_STAGE} --with-registry-auth
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    url: http://$CI_COMMIT_REF_SLUG.jugatsu.xyz
    on_stop: stop_review
  only:
    refs:
      - branches
  except:
    - master

stop_review:
  stage: review
  variables:
    GIT_STRATEGY: none
  services:
    - docker:dind
  before_script:
    - *ssh_config
  script:
    - *swarm_config
    - docker stack rm ${CI_PROJECT_NAMESPACE}-${CI_PROJECT_NAME}-${CI_BUILD_STAGE}
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    action: stop
  when: manual
  allow_failure: true
  only:
    refs:
      - branches
  except:
    - master

Release image:
  stage: release
  services:
    - docker:dind
  script:
    - docker build -t ${CONTAINER_IMAGE_PATH}:${CI_COMMIT_TAG#v} .
    - docker tag ${CONTAINER_IMAGE_PATH}:${CI_COMMIT_TAG#v} ${CONTAINER_IMAGE_PATH}:latest
    - docker push ${CONTAINER_IMAGE_PATH}:latest
    - docker push ${CONTAINER_IMAGE_PATH}:${CI_COMMIT_TAG#v}
  only:
    - tags

Deploy to prod:
  stage: production
  services:
    - docker:dind
  before_script:
    - *ssh_config
  script:
    - *swarm_config
    - export TAG=${CI_COMMIT_TAG#v}
    - export URL=${PRODUCTION}
    - docker stack deploy -c ${STACK_PROD} ${STACK_PROD_NAME} --with-registry-auth
  environment:
    name: production
    url: http://${PRODUCTION}
  only:
    - tags

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png