jenkins安装及项目构建发布回滚

官网部署地址:
https://pkg.jenkins.io/redhat-stable/

部署jenkins

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install epel-release # repository that provides 'daemonize'
yum install java-11-openjdk-devel
yum install jenkins

配置权限

vim /etc/sysconfig/jenkins
修改配置
$JENKINS_USER="root"

修改目录权限

chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins

启动jenkins按照提示配置
systemctl start jenkins

项目构建回滚
新建项目

配置参数化构建

配置字符参数

选择git地址

编写构建脚本

配置构建后操作需要安装Publish Over SSH

保存后执行build


构建脚本

case $status in
  Deploy)
  echo Status:$status
  BAK_PATH=${WORKSPACE}/bak
  BUILD_PATH=/tmp/${JOB_NAME}
  BUILD_SAVE=5
  if [ -d $BAK_PATH -a -d $BUILD_PATH ];
  then
  echo "The files is already exists "
  else
  mkdir -p $BAK_PATH
  mkdir -p $BUILD_PATH
  fi
  cd ${WORKSPACE}/ApplicationBusinessService
  /usr/local/bin/dotnet restore
  /usr/local/bin/dotnet build
  /usr/local/bin/dotnet publish -c release -o $BUILD_PATH
  cd $BUILD_PATH
  rm -rf appsettings.json
  tar -zcvf $BAK_PATH/${JOB_NAME}_${BUILD_NUMBER}.tar.gz ./*
  while true;
  do
  cd $BAK_PATH
  BAKFILES=$(ls -1tr|wc -l)
  if [ $BAKFILES -gt "${BUILD_SAVE}" ];then
       ls -1tr |head -1|xargs rm -rf
  else
        break;
  fi
  done
  ;;
  Rollback)
  echo Status:$status
  echo Version:$Version
  ;;
 *)
  exit
     ;;
esac

构建后发布脚本

case $status in
   Deploy)
   BAK_PATH=/tmp/AlphaSocket
   BAK_SAVE=10
   tar -zxvf  /tmp/AlphaSocket/${JOB_NAME}_${BUILD_NUMBER}.tar.gz   -C /usr/local/ApplicationBusinessService
   pm2 reload businessServer
   while true;
   do
   cd $BAK_PATH
   BAKFILES=$(ls -1tr|wc -l)
   if [ $BAKFILES -gt "${BAK_SAVE}" ];then
       ls -1tr |head -1|xargs rm -rf
   else
        break;
  fi
  done
   ;;
   Rollback)
   tar -zxvf  /tmp/AlphaSocket/${JOB_NAME}_$Version.tar.gz   -C /usr/local/ApplicationBusinessService
   pm2 reload businessServer
   ;;
  *)
  exit
     ;;
esac
posted @ 2022-01-12 15:05  王诚意  阅读(183)  评论(0编辑  收藏  举报