gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务

1.准备好编译环境的容器,所有容器的镜像制作在gemdale-dockerfile这个代码库里面,也可以直接拉取官方镜像部署

docker run --name node1420-patternx -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t node:14.20.0

docker run --name node1615-patternx  -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t node:16.15.1

docker run --name nodejs1417-patternx -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t jelastic/nodejs:14.17.5-forever

FROM node:16.20.1-bullseye
docker run --name nodejs16 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/nodejs16:v1.1.0.0

FROM golang:1.19.6-buster
docker run --name golang -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ -v /data/etc/cron.d/:/etc/cron.d/ -v /data/etc/supervisord.d/:/etc/supervisord.d/ -v /data/var/log/supervisor/:/var/log/supervisor/ --net host --restart always -d -t golang:1.19.6-bullseye

FROM node:14.20.0-buster
docker run --name nodejs14 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/nodejs14:v1.2.0.0

FROM python:3.9.17-bullseye
docker run --name python39-testing -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/python39-testing:v1.0.0.1

FROM maven:3.8.6-openjdk-11
docker run --name maven3-openjdk11 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/maven3-openjdk11:v1.2.0.0

FROM python:3.10.13-bullseye
docker run --name ansible --privileged -v /data/root/.ssh/:/root/.ssh/ -v /data/root/.tiup/:/root/.tiup/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ -v /data/ansible/:/data/ -v /data/etc/ansible/:/etc/ansible/ --restart always -d -t linice/ansible:v1.2.0.0

FROM maven:3.8.6-openjdk-8
docker run --name maven3-openjdk8 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/maven3-openjdk8:v1.1.0.0

2.发布jenkins的shell执行脚本代码在仓库gemdale_jenkins

gemdale_jenkins
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第1张图片
This project is parameterized
Choice Parameter 参数的名称这些参数作为环境变量公开用于构建
这个是定义项目脚本里面的变量
dr 默认是deploy部署脚本 可以选rollback回滚脚本
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第2张图片
project ienjoys 项目变量
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第3张图片
env test prod 环境变量
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第4张图片
部署到服务器
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第5张图片
实际部署的服务service是服务名称变量,gemdale_jenkins是实际服务名
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第6张图片
代码分支
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第7张图片
选择变量false,true:是否更新helm chart release. 默认为false, 即不更新.第一次执行要选true
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第8张图片
回滚设置
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第9张图片
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第10张图片

gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第11张图片
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第12张图片
gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务_第13张图片
bash /data/var/www/service/gemdale_jenkins/sourcecode/sourcecode.sh

#!/bin/bash
set -e #有报错就直接退出
######## define variables -- start ########
# current shell script file's directory
workdir=$(dirname $(readlink -f $0)) #当前文件所在的目录
######## define variables -- end ########
# arg
dr=${dr:-deploy}  #-deploy等于jenkins设置的默认变量deploy
# comm_var
shell=$workdir/default/sourcecode-$dr.sh  #设置一个变量
# exec
#执行变量脚本/data/var/www/service/gemdale_jenkins/default/sourcecode-$dr.sh
source $shell     

source sourcecode-deploy.sh

#!/bin/bash

echo 'renew my_tgt:'
rm -rf my_tgt
mkdir my_tgt

######## define variables -- start ########
keep=20
######## define variables -- end ########

# comm
#可无权限执行脚本/data/var/www/service/gemdale_jenkins/default/sourcecode-comm.sh
source $workdir/default/sourcecode-comm.sh

# build_env
service_ws=$WORKSPACE
service_tgt_path=$service_ws

echo 'compress service:'
cd $service_tgt_path
tgz_file=$service-${BUILD_ID}.tar.gz
tgz_file_path=$WORKSPACE/my_tgt/$tgz_file
tar --exclude=my_tgt -czf $tgz_file_path .

# run
dest_tgz_file_path=$dest_d_dir/$service/$tgz_file
dest_d_path=$dest_d_dir/$service/$service-$BUILD_ID

source $workdir/default/sourcecode-run.sh

echo "on destination server: delete old tgz files and service directories, to keep last $keep services. -- start"
del_files=''
for ((i=$keep; i < $(($keep + 100)); i++)); do
  if (($BUILD_ID - $i <= 0)); then
    break;
  fi
  del_files="$del_files $service-$(($BUILD_ID - $i)) $service-$(($BUILD_ID - $i)).tar.gz"
done

docker exec ansible ansible $rs -m shell -a "cd $dest_d_dir/$service && rm -rf $del_files"
echo "on destination server: delete old tgz files and service directories, to keep last $keep services. -- end"

source sourcecode-comm.sh

#!/bin/bash

# comm_var
dest_dir=/data/var/www/service
dest_d_dir=/data/var/www/service.d
dest_path=$dest_dir/$service

# env_service_var
case $env in
  test | uat )
    case $service in
      patternx-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
      carpark-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdimg-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdnb-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdr-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdyk-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gemdale_charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
      gemdale_jenkins )
        rs=${rs:-10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
      gemdale-playbook )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.36.21.224}
        echo ${rs}
        ;;
      pmc-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
    esac
    ;;
  prod )
    case $service in
      patternx-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      ats-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      carpark-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdimg-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdnb-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdr-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdyk-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gemdale_charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216,10.0.103.246,10.0.103.250}
        ;;
      gemdale_jenkins )
        rs=${rs:-10.0.103.246,10.0.103.250}
        ;;
      gemdale-playbook )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216,10.36.14.174}
        ;;
      pmc-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216,10.0.103.246,10.0.103.250}
        ;;
    esac
    ;;
esac

source sourcecode-run.sh

#!/bin/bash

echo 'on destination server, create directories:'
docker exec ansible ansible $rs -m file -a "path=$dest_dir state=directory"
docker exec ansible ansible $rs -m file -a "path=$dest_d_path state=directory"

echo 'scp tgz file to destination server:'
docker exec ansible ansible $rs -m copy -a "src=$tgz_file_path dest=$dest_d_dir/$service/"

echo 'on destination server, decompress tgz file:'
docker exec ansible ansible $rs -m shell -a "tar -xzf $dest_tgz_file_path -C $dest_d_path/"

echo 'on destination server, link to dest_path:'
docker exec ansible ansible $rs -m shell -a "ln -snf $dest_d_path $dest_path"

你可能感兴趣的:(gitlab,jenkins,kubernetes)