1、 在项目根目录下创建目录.gitea以及子目录worlflows
2、 创建yaml文件
name: dms master actions
run-name: ${{ github.actor }} is running on master
on:
push:
branches:
- dev
paths-ignore:
- .gitea/workflows/master.yaml
- Readme.md
- Changelog.md
jobs:
# 任务名称
build:
runs-on: ubuntu-latest
# 创建输出,用于发送邮件通知
outputs:
status: ${{ steps.result.outputs.status }}
steps:
# 缓存Maven3.9安装包
- name: 缓存Maven3.9安装包
id: maven-install-cache
uses: http://192.168.2.19:3000/Actions/cache@v3
with:
path: /home/apache-maven-3.9.6
key: maven-install-cache-3.9.6
# 如果没有缓存,下载maven安装包
- name: 下载Maven3.9.6
# 判断 id = maven-install-cache 的缓存对象是否已经缓存,如果已缓存则不执行
if: steps.maven-install-cache.outputs.cache-hit != 'true'
# 已经配置好一个maven,传入gitee,直接拉取就可以使用了
run: |
cd /home
git clone http://192.168.2.19:3000/Actions/apache-maven-3.9.6.git
echo "下载 maven 完成"
# 1拉取代码
- name: Check out repository code
uses: http://192.168.2.19:3000/Actions/checkout@v4
# java环境设置
- name: Set up JDK 17
uses: http://192.168.2.19:3000/Actions/setup-java@v4
with:
java-version: '17'
distribution: 'microsoft'
cache: 'maven'
# 验证java
- name: Check Java version
run: java -version
# 打包
- name: 🖥 开始编译Java项目
# 设置Maven执行权限,使用maven绝对路径执行打包命令
run: |
chmod +x /home/apache-maven-3.9.6/bin/mvn
/home/apache-maven-3.9.6/bin/mvn clean package -Dmaven.test.skip=true
# 传输jar包
- name: copy jar to 19
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.DEV_HOST }}
username: ${{ secrets.DEV_USERNAME }}
port: ${{ secrets.DEV_PORT }}
password: ${{ secrets.DEV_PASSWORD }}
# 根据实际情况,输入jar包地址
source: "***.jar"
target: "/project/warehouse/dev/"
# 修改包
- name: executing remote ssh commands using password
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.DEV_HOST }}
username: ${{ secrets.DEV_USERNAME }}
password: ${{ secrets.DEV_PASSWORD }}
port: ${{ secrets.DEV_PORT }}
script: |
# 获取当前日期时间戳
TIMESTAMP=$(date +'%Y%m%d%H%M%S')
cd /project/warehouse/dev/
# 备份 JAR 文件并重命名为包含日期时间戳的文件
if [ -f "warehouse-application-dev.jar" ]; then
mv warehouse-application-dev.jar backup/warehouse-application-dev-$TIMESTAMP.jar
echo "Backup JAR file to warehouse-application-dev-$TIMESTAMP.jar"
else
echo "JAR file not found."
fi
rm -rf warehouse-application-dev.jar
# 根据各自情况,输入jar地址
cp ****.jar warehouse-application-dev.jar
rm -rf workspace
bash warehouse-quick.sh restart
- name: 设置项目结果(成功)
id: result
run: |
echo "::set-output name=status::success"
on-email:
needs: build
runs-on: ubuntu-latest
if: always()
steps:
- name: Determine email subject
id: determine-subject
run: |
if [ "${{ needs.build.outputs.status }}" == "success" ]; then
echo "SUBJECT=开发环境 仓库管理系统 - 构建成功" >> $GITHUB_ENV
else
echo "SUBJECT=开发环境 仓库管理系统 - 构建失败" >> $GITHUB_ENV
fi
# 发送邮件通知
- name: Send mail
id: send-email
uses: http://192.168.2.19:3000/Actions/action-send-mail@v3.12.0
with:
server_address: smtphz.qiye.163.com
server_port: 465
secure: true
username: ${{secrets.MAIL_USERNAME}}
password: ${{secrets.MAIL_PASSWORD}}
subject: ${{ env.SUBJECT }}
# 多个邮件,逗号隔开
to: ***,***
from: ***
body: |
流水线 ${{ github.repository }} 构建
${{ needs.build.outputs.status == 'success' ? '成功' : '失败' }}
ignore_cert: true
3. 远程sh脚本
重点是start方法中的java路径,写全路径,否则或出现无法启动成功问题
#!/bin/sh
# ./ry.sh start 启动 stop 停止 restart 重启 status 状态
AppName=application-test.jar
# JVM参数
JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError"
APP_HOME=`pwd`
LOG_PATH=$APP_HOME/logs/$AppName.log
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m"
exit 1
fi
if [ "$AppName" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fi
function start()
{
PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
if [ x"$PID" != x"" ]; then
echo "$AppName is running..."
else
nohup /opt/jdk-17/bin/java $JVM_OPTS -jar $AppName --spring.profiles.active=test> ./java.log 2>&1 &
echo "Start $AppName success..."
fi
}
function stop()
{
echo "Stop $AppName"
PID=""
query(){
PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
}
query
if [ x"$PID" != x"" ]; then
kill -TERM $PID
echo "$AppName (pid:$PID) exiting..."
while [ x"$PID" != x"" ]
do
sleep 1
query
done
echo "$AppName exited."
else
echo "$AppName already stopped."
fi
}
function restart()
{
stop
sleep 2
start
}
function status()
{
PID=`ps -ef |grep java|grep $AppName|grep -v grep|wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
else
echo "$AppName is not running..."
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
*)
esac