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

一、使用html

該版未接入k8s,下一篇會寫接入k8s。java

配置項目使用gitlab-ci和gitlab-runner,在項目的根目錄中添加.gitlab-ci.yml文件,用於觸發pipeline。python

 

.hosts目錄裏面是四個文件:.personal_ips、.daily_ips、staging_ips、prod_ips,存放的是每一個環境的ip列表,每行一個ip。git

.scripts目錄裏面是要執行的腳本:deploy、start、start_prod 、testdocker

須要提早準備基礎鏡像,因爲要用到docker,因此這裏採用的是docker做爲基礎鏡像,若是構建java或python則須要相應的基礎鏡像。準備好鏡像後,還須要爲鏡像準備免交互的祕鑰對。生成一個祕鑰對而後將私鑰集成到基礎鏡像裏面,製做方法以下:bash

這樣作的緣由:因爲採用的是alpine,因此使用ssh-add老是失敗,因此就放棄了,多是我沒有找對方法吧。服務器

首先要確保製做的這個鏡像只能用於公司內部使用,由於裏面包含了私鑰,因此不要上傳到公網,上傳到公司的私有倉庫進行使用。ssh

製做祕鑰對:
#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文件:gitlab

# 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、productionui

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

相關文章
相關標籤/搜索