Untitled

mail@pastecode.io avatar
unknown
yaml
a year ago
2.4 kB
28
Indexable
image: docker:git

stages:
  - build
  - deploy

variables:
  PROJECT: thichlab
  APP_NAME: thichlab-backend-docs-v1
  IMAGE_TAG: $CI_REGISTRY_IMAGE/$PROJECT/$APP_NAME:dev
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""

services:
  - name: docker:dind
    entrypoint: ["env", "-u", "DOCKER_HOST"]
    command: ["dockerd-entrypoint.sh"]

build:
  stage: build
  allow_failure: false
  before_script:
    - docker rm -f $IMAGE_TAG
  script:
    - echo "Building image..."
    - docker build -t $IMAGE_TAG .
    - docker pull $IMAGE_TAG || echo "Building runtime from scratch"
    - >
      docker build
      --cache-from $IMAGE_TAG
      -t $IMAGE_TAG .
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin
    - docker push $IMAGE_TAG
  only:
    - develop
    - /^dev-.*$/

build_prod:
  stage: build
  when: manual
  allow_failure: false
  environment:
    name: production
  script:
    - echo "Building image..."
    - docker build -t $IMAGE_TAG .
    - docker pull $IMAGE_TAG || echo "Building runtime from scratch"
    - >
      docker build
      --cache-from $IMAGE_TAG
      -t $IMAGE_TAG .
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker push $IMAGE_TAG
  only:
    - /^release-/

.deploy_template:
  image: ubuntu
  stage: deploy
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - chmod 400 ~/.ssh
    - ssh-keyscan $SSH_IP_SERVER >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - > 
      ssh $SSH_USER@$SSH_IP_SERVER
      "sudo docker login -u $MASTER_DEPLOY_USER -p $MASTER_DEPLOY_TOKEN $CI_REGISTRY 
      & sudo docker pull $IMAGE_TAG && sudo docker container rm -f $APP_NAME && sudo docker run --rm -it --name $APP_NAME -dp 9091:9091 $IMAGE_TAG"
deploy_dev:
  extends: .deploy_template
  environment:
    name: development
  variables:
    DEPLOY_BRANCH: develop
    APP_IMAGE_TAG: dev
    VERSION: v1.0
  only:
    - develop
    - /^dev-.*$/

deploy_production:
  extends: .deploy_template
  environment:
    name: production
  variables:
    DEPLOY_BRANCH: production
    APP_IMAGE_TAG: production
    VERSION: v1.0
  only:
    - production
    - /^release-.*$/