gitlab之gitlab-ci和gitlab-runner<二>

1、使用

该版未接入k8s,下一篇会写接入k8s。

配置项目使用gitlab-ci和gitlab-runner,在项目的根目录中添加.gitlab-ci.yml文件,用于触发pipeline。

 

.hosts目录里面是四个文件:.personal_ips、.daily_ips、staging_ips、prod_ips,存放的是每个环境的ip列表,每行一个ip。

.scripts目录里面是要执行的脚本:deploy、start、start_prod 、test

需要提前准备基础镜像,由于要用到docker,所以这里采用的是docker作为基础镜像,如果构建java或python则需要相应的基础镜像。准备好镜像后,还需要为镜像准备免交互的秘钥对。生成一个秘钥对然后将私钥集成到基础镜像里面,制作方法如下:

这样做的原因:由于采用的是alpine,所以使用ssh-add总是失败,所以就放弃了,可能是我没有找对方法吧。

首先要确保制作的这个镜像只能用于公司内部使用,因为里面包含了私钥,所以不要上传到公网,上传到公司的私有仓库进行使用。

制作秘钥对:
#ssh-keygen
一直回车
找一台安装有docker的服务器,运行容器
# docker run -d -it docker.io/docker:stable
找到容器连进去:
# docker  ps
# docker exec -it 3e3b5d693c77 sh

安装openssh git
# apk add --no-cache openssh git && rm -rf /var/cache/apk/*

将生成的秘钥copy近容器:
#scp  10.10.1.11:~/.ssh/id_rsa  ~/.ssh

退出容器control +p +q

将制作好的容器提交为镜像:
# docker commit 3e3b5d693c77 fastesp/docker:stable

后面的名字自己定义,前面要加上自己私有仓库的地址,我这里是fastest,你的地址可能是10.10.1.11:5000.你的标签就是这样:10.10.1.11:5000/base_image/docker:stable
将之前创建的公钥加入到需要免密交互的服务器上面的authorized_keys文件即可。

基础镜像创建完成,接下来创建.gitlab-ci.yml文件:

# This file is a template, and might need editing before it works on your project.
# Official docker image.
image: fastop/docker:stable
services:
  - docker:dind

stages:
  - build
  - deploy

before_script:
  - docker info
  - docker login -u fastop -p tianyu01234

##########ENV  prepare############
.personal_pre: &personal_ssh
  before_script:
    - ssh-keyscan $PERSONAL_HOSTS > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - /bin/sh .scripts/test $CI_COMMIT_REF_SLUG

.personal_tag: &personal_env
  environment:
    name: $CI_COMMIT_REF_SLUG
    url: http://dog.dd.com
  only:
    - monkey
    - dog
  tags:
    - fuck

.daily_pre: &daily_ssh
  before_script:
    - ssh-keyscan $DAILY_HOSTS > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - /bin/sh .scripts/test daily
.daily_tag: &daily_env
  environment:
    name: dev
    url: http://daily.dd.com
  only:
    - develop
  tags:
    - fuck

.staging_pre: &staging_ssh
  before_script:
    - ssh-keyscan $STAGING_HOSTS > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - /bin/sh .scripts/test staging
.staging_tag: &staging_env
environment:
name: stagimg
url: http://staging.dd.com only: - staging tags: - staging
.prod_pre: &prod_ssh before_script: - ssh-keyscan $PROD_HOSTS > ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts - /bin/sh .scripts/test prod .prod_tag: &prod_env environment: name: production url: http://prod.dd.com only: - /v[0-9|\.]+/ except: - branches ###########buid jobs############# daily_build: stage: build script: - docker build --pull -t "fastest/spin-kube-demo:daily" . - docker push fastest/spin-kube-demo:daily <<: *daily_env staging_build: stage: build script: - docker build --pull -t "fastest/spin-kube-demo:staging" . - docker push fastest/spin-kube-demo:staging <<: *staging_env personal_build: stage: build script: - docker build --pull -t "fastest/spin-kube-demo:$CI_COMMIT_REF_SLUG" . - docker push fastest/spin-kube-demo:$CI_COMMIT_REF_SLUG <<: *personal_env prod_build: stage: build script: - export VERSION=`git tag -l| tail -1` - docker build --pull -t "fastop/spin-kube-demo:$VERSION" . - docker tag fastop/spin-kube-demo:$VERSION fastop/spin-kube-demo:latest - docker push fastop/spin-kube-demo:$VERSION - docker push fastop/spin-kube-demo:latest <<: *prod_env when: manual #########deploy job############## personal_deploy: <<: *personal_ssh stage: deploy script: - /bin/sh .scripts/deploy $CI_COMMIT_REF_SLUG dependencies: - personal_build <<: *personal_env daily_deploy: <<: *daily_ssh stage: deploy script: - /bin/sh .scripts/deploy daily dependencies: - daily_build <<: *daily_env staging_deploy: <<: *staging_ssh stage: deploy script: - /bin/sh .scripts/deploy staging dependencies: - staging_build <<: *staging_env prod_deploy: <<: *prod_ssh stage: deploy script: - /bin/sh .scripts/deploy prod dependencies: - prod_build <<: *prod_env when: manual

 

 这里分了四个环境,当然也可以根据需求删减,personal、daiy、staging、production

environment: 是配置在deploy这个stage里面的,用于后面Environments可以做版本回滚。

详细配置参考:https://docs.gitlab.com/ce/ci/yaml/README.html#special-yaml-features

gitlab-runner配置:

项目-->settings-->CI/CD-->Variables 

设置一些环境变量

 

可以在这里设置自己需要的变量,这些变量可以直接在.gitlab-ci.yml文件里面直接使用。

deploy:

 

#!/bin/bash
#script for triger deploy stage

#personal env function
personal_deploy(){
   personal_hosts=`cat .hosts/.personal_ips`
   for i in $personal_hosts
   do
     ssh root@$i 'bash -C -s' < .scripts/start $CI_COMMIT_REF_SLUG
   done
}

#daily env function
daily_deploy(){
   daily_hosts=`cat .hosts/.daily_ips`
   for i in $daily_hosts
   do
     ssh root@$i 'bash -C -s' < .scripts/start "daily"
   done
}

#staging env function
staging_deploy(){
   staging_hosts=`cat .hosts/.staging_ips`
   for i in $staging_hosts
   do
     ssh root@$i 'bash -C -s' < .scripts/start "staging"
   done
}

#prod env function
prod_deploy(){
   prod_hosts=`cat .hosts/.prod_ips`
   for i in $prod_hosts
   do
     ssh root@$i 'bash -C -s' < .scripts/start_prod $VERSION
   done
}

case $1 in

$CI_COMMIT_REF_SLUG)
  personal_deploy
  ;;
daily)
  daily_deploy
  ;;
$VERSION)
  prod_deploy
  ;;
*)
 echo "ERROR PARA"
 echo "Usage:$0 $CI_COMMIT_REF_SLUG|daily|$VERSION"
esac

 

start:

#!/bin/bash
#start script

tag=$1
start(){
  docker pull fastest/spin-kube-demo:$tag
  cd /data/project
  docker-compose  -f  docker-compose-$tag\.yml down
  docker-compose  -f  docker-compose-$tag\.yml  up -d
}
start

需要在对应的服务器上面创建/data/project目录,然后创建docker-compose-$tag.yml文件,$tag就是对应的环境,例如:daily

start_prod:

#!/bin/bash
#start script

tag=$1

start_prod(){
  docker pull fastop/spin-kube-demo:latest
  cd /data/project
  docker-compose  -f  docker-compose.yml down
  docker-compose  -f  docker-compose.yml  up -d
}
start_prod

 

test:

#!/bin/bash
#script for triger deploy stage

#personal env function
personal_deploy(){
   personal_hosts=`cat .hosts/.personal_ips`
   for i in $personal_hosts
   do
     ssh root@$i 'echo test'
   done
}

#daily env function
daily_deploy(){
   daily_hosts=`cat .hosts/.daily_ips`
   for i in $daily_hosts
   do
     ssh root@$i 'echo test'
   done
}

#staging env function
staging_deploy(){
   staging_hosts=`cat .hosts/.staging_ips`
   for i in $staging_hosts
   do
     ssh root@$i 'echo test'
   done
}

#prod env function
prod_deploy(){
   prod_hosts=`cat .hosts/.prod_ips`
   for i in $prod_hosts
   do
     ssh root@$i 'echo test'
   done
}

case $1 in

$CI_COMMIT_REF_SLUG)
  personal_deploy
  ;;
daily)
  daily_deploy
  ;;
prod)
  prod_deploy
  ;;
*)
 echo "ERROR PARA"
 echo "Usage:$0 $CI_COMMIT_REF_SLUG|daily|prod"
esac

每次提交代码就会自动触发构建并自动发布,production的构建发布需要手动点击按钮,这个是when: manual实现的。

 

 

 类似金丝雀的发布策略,是通过控制服务的ip列表实现的。比如博客中写的deploy 10%、deploy 50%,就是通过分割服务器ip列表实现的。

参考:

https://docs.gitlab.com/ce/ci/yaml/README.html#special-yaml-features

posted @ 2018-09-28 18:19  诗码者  阅读(6423)  评论(0编辑  收藏  举报