简易教程设置此项目的开机自启(服务)及对应CICD
1. 添加Teamcity、ConfBackend为系统服务,到/etc/systemd/system,添加{name}.service文件,两个文件见下面:
teamcity.service文件:
[Unit]
Description=Teamcity Service, including client and server :)
[Service]
Type=forking
User=teamcity
Environment="JAVA_HOME=/home/tando/dev/SDKs/jdk-17.0.7"
ExecStart=/home/tando/downloads/TeamCity/bin/runAll.sh start
ExecStop=/home/tando/downloads/TeamCity/bin/runAll.sh stop
User=tando
Group=sudo
Restart=on-failure
[Install]
WantedBy=multi-user.target
cb.service文件:
[Unit]
Description=The ConfBackend Service :)
[Service]
Type=simple
WorkingDirectory=/home/tando/tr_app
ExecStart=/home/tando/tr_app/ConfBackend
ExecStop=/usr/bin/pkill ConfBackend
Restart=on-failure
User=tando
Group=tando
[Install]
WantedBy=multi-user.target
2. 确保:
a (上述文件)中,User=terrain (即当前用户), Group=sudo
b 系统中,上述用户,即terrain在sudo组,可以通过 sudo adduser terrain sudo添加
c ConfBackend也要添加为service
3. 确保能使用systemd开启关闭TC
4. 修改权限,直接输入 sudo visudo,在最下面添加: terrain ALL=(ALL:ALL) NOPASSWD:ALL
可以免输密码
5. 修改CICD中的脚本,可以直接使用sudo systemctl stop/start cb,直接启停ConfBackend。
CICD脚本如下:
------------------------------------------------------------
#!/bin/bash
source /etc/profile
# 检查是否已经在运行
pwd
if ps -ax|grep "ConfBackend"; then
echo "ConfBackend is already running. Stopping current instance..."
sudo systemctl stop cb
fi
# print env
echo "env device_name $device_name"
# 编译新的 ConfBackend
echo "Compiling ConfBackend..."
go build -o ConfBackend
# 启动 ConfBackend
# echo "Starting ConfBackend..."
# nohup ./ConfBackend &
# ps -ax|grep "ConfBackend"
------------------------------------------------------------
echo "Second step in"
ps -ax|grep ConfBackend
source /etc/profile
# nohup ./ConfBackend > /dev/null 2>&1 &
sudo systemctl start cb
6. 设置web的CICD
rm -r dist
cat dist.zip.*>dist_all.zip
unzip -q dist_all.zip