记录下自动化部署脚本.gitlab-ci.yml
image: maven:latest
# 设置触发条件
workflow:
rules:
- if: '$CI_COMMIT_MESSAGE =~ /^publish/ && $CI_COMMIT_BRANCH == "pro"'
when: always
- if: '$CI_COMMIT_MESSAGE =~ /^publish/ && $CI_COMMIT_BRANCH == "pro-dalian"'
when: always
- if: '$CI_COMMIT_MESSAGE =~ /^publish/ && $CI_COMMIT_BRANCH == "test"'
when: always
- if: '$CI_COMMIT_MESSAGE =~ /^publish/ && $CI_COMMIT_BRANCH == "fac"'
when: always
- when: never
# 设置一个缓存路径
cache:
paths:
- target/
# 本次构建的阶段:build package deploy
stages:
- init
- package
- deploy
# 初始化
初始化:
stage: init
tags:
- ZZTJJARM
script:
- echo "=============== 检查必要工具 ==============="
- node -v
- npm -v
- nvm -v
- nvm ls
- git --version
# 打包
打包:
stage: package
tags:
- ZZTJJARM
script:
- echo "=============== 开始打包任务 ==============="
- rm -rf node_modules
- nvm use 11.0.0
- npm i --unsafe -perm
- npm run build
- server_name="ZZTJJARM"
- tar cvf $server_name.tar $server_name
- chmod 777 $server_name.tar
- rm -if /home/gitlab-runner/gitlabPro/$CI_COMMIT_BRANCH/$server_name.tar
- if [ ! -d "/home/gitlab-runner/gitlabPro/$CI_COMMIT_BRANCH" ];then mkdir /home/gitlab-runner/gitlabPro/$CI_COMMIT_BRANCH; echo '文件夹不存在,创建:'$CI_COMMIT_BRANCH; else echo '文件夹已存在:'$CI_COMMIT_BRANCH;fi
- cp -i $server_name.tar /home/gitlab-runner/gitlabPro/$CI_COMMIT_BRANCH/$server_name.tar
# 部署
部署:
stage: deploy
tags:
- ZZTJJARM
script:
- echo "=============== 开始部署任务 ==============="
- server_name="ZZTJJARM"
- REMOTE_IPS='172.16.1.144'
- REMOTE_PORT=22
- |+
if [[ $CI_COMMIT_BRANCH == 'pro' ]]; then
REMOTE_IPS='XX.XX.XX.XX'
fi
if [[ $CI_COMMIT_BRANCH == 'pro-dalian' ]]; then
REMOTE_IPS='XX.XX.XX.XX'
fi
if [[ $CI_COMMIT_BRANCH == 'fac' ]]; then
REMOTE_IPS='XX.XX.XX.XX'
fi
- echo $REMOTE_IPS
- IFS=',' read -ra ip_array <<< "$REMOTE_IPS"
- echo $ip_array
- |+
for REMOTE_IP in "${ip_array[@]}"
do
echo "scp -o StrictHostKeychecking=no -P $REMOTE_PORT /home/gitlab-runner/gitlabPro/$CI_COMMIT_BRANCH/$server_name.tar root@$REMOTE_IP:/usr/local/nginx/html/"
scp -o StrictHostKeychecking=no -P $REMOTE_PORT /home/gitlab-runner/gitlabPro/$CI_COMMIT_BRANCH/$server_name.tar root@$REMOTE_IP:/usr/local/nginx/html/
echo "source /etc/profile && mv /usr/local/nginx/html/$server_name /usr/local/nginx/html/bak/$server_name$(date +%Y%m%d%H%M%S)"
ssh -o StrictHostKeychecking=no root@$REMOTE_IP -p 22 "source /etc/profile && if [ ! -d "/usr/local/nginx/html/$server_name" ];then mkdir /usr/local/nginx/html/$server_name;mkdir /usr/local/nginx/html/bak; echo '文件夹不存在,创建:'$server_name $bak; else echo '文件夹已存在:'$server_name $bak;fi && mv /usr/local/nginx/html/$server_name /usr/local/nginx/html/bak/$server_name$(date +%Y%m%d%H%M%S)"
sleep 10s
ssh -o StrictHostKeychecking=no root@$REMOTE_IP -p 22 "chmod 777 /usr/local/nginx/html/${server_name}.tar && tar xvf /usr/local/nginx/html/${server_name}.tar -C /usr/local/nginx/html"
ssh -o StrictHostKeychecking=no root@$REMOTE_IP -p 22 "rm -rf /usr/local/nginx/html/${server_name}.tar"
ssh -o StrictHostKeychecking=no root@$REMOTE_IP -p 22 "/usr/local/nginx/sbin/nginx -s reload"
done
评论 (0)