部署项目创建跟其他的部署相同,主要就是部署脚本

前提准备

  1. 登录VPN
    1. image.png
  2. powershll 安装 Install-Module -Name Posh-SSH
    1. 参考 《PowerShell 脚本知识.md》

      Build Steps

      打包

      1. clean package -DskipTests -Dmaven.javadoc.skip=true
      image.png

      上传JAR到服务器

      1. function getCred ($username, $password) {
      2. $pass = ConvertTo-SecureString $password -AsPlainText -Force
      3. $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $pass
      4. return $cred
      5. }
      6. $server = '192.168.1.11'
      7. $port = '11'
      8. $username = '11'
      9. $password = '1111'
      10. $localFile = '%teamcity.build.workingDir%/target/detabes-file.jar'
      11. $remotePath = '/peter/cqepc'
      12. $cred = getCred -username $username -password $password
      13. $session = New-SFTPSession -ComputerName $server -Port $port -Credential $cred -Force
      14. Set-SFTPItem -SFTPSession $session -Destination $remotePath -Path $localFile -Force
      15. Remove-SFTPSession -SFTPSession $session
      image.png

运行jar

  1. function getCred ($username, $password) {
  2. $pass = ConvertTo-SecureString $password -AsPlainText -Force
  3. $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $pass
  4. return $cred
  5. }
  6. $server = '192.168.1.11'
  7. $port = '11'
  8. $username = '11'
  9. $password = '11'
  10. $cred = getCred -username $username -password $password
  11. $session = New-SSHSession -ComputerName $server -Port $port -Credential $cred -Force
  12. $javaPath = '/usr/local/java/'
  13. ## $cmd = 'uname -a'
  14. ## Invoke-SSHCommand -Command $cmd -SSHSession $session
  15. Invoke-SSHCommand -Command 'cd /peter/cqepc/ && ./file-start-stop-auto.sh test' -SSHSession $session
  16. #Start-Sleep -s 60
  17. Remove-SSHSession -SSHSession $session

image.png

备注

  1. file-start-stop-auto.sh test ```shell

    !/bin/bash

PROJECT_PATH=/doc/program/smart/soul-admin.jar

read -p “:” PROJECT_PATH

echo “你输入的数是:$PROJECT_PATH”

export JAVA_HOME=/usr/local/java export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

PROJECT_PATH=/peter/cqepc/detabes-file.jar PROJECT_PROPERTIES=${1} PROJECT_PORT=9002

设置环境参数

JAVA_OPTS=” -Xms512m “

-XX:+UseConcMarkSweepGC -XX:+UseParNewGC || -XX:+UseParallelGC -XX:+UseParallelOldGC

JAVA_OPTS=”${JAVA_OPTS} -Xmx1024m -Xmn512m -XX:+UseParallelGC -XX:+UseParallelOldGC “ JAVA_OPTS=”${JAVA_OPTS} -Dfile.encoding=UTF-8”

设置默认值

if [ ! $PROJECT_PATH ]; then PROJECT_PATH=”detabes-file.jar” fi

if [ ! $PROJECT_PORT ]; then SERVER_PORT=” —server.port=9002” else SERVER_PORT=” —server.port=${PROJECT_PORT}” fi

if [ ! $PROJECT_PROPERTIES ]; then SERVER_PROPERTIES=”—spring.profiles.active=prod” else SERVER_PROPERTIES=”—spring.profiles.active=${PROJECT_PROPERTIES}” fi

shutdown

pid=ps -ef|grep $PROJECT_PATH | grep -v grep | awk '{print $2}' kill -9 $pid echo “$pid进程终止成功” sleep 1

startUp

sleep 2

判断jar包文件是否存在,如果存在启动jar包,并时时查看启动日志

if test -e $PROJECT_PATH then echo ‘文件存在,开始启动此程序…’

启动jar包,指向日志文件,2>&1 & 表示打开或指向同一个日志文件

echo “nohup java -jar $JAVA_OPTS “$PROJECT_PATH” $SERVER_PORT $SERVER_PROPERTIES >/dev/null 2>&1 &” nohup java -jar $JAVA_OPTS $PROJECT_PATH $SERVER_PORT $SERVER_PROPERTIES >/dev/null 2>&1 &

echo “$PROJECT_PATH 启动成功…”

else echo “$PROJECT_PATH 文件不存在,请检查。” fi ```