Deploy Chat Application #249
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy Chat Application | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
schedule: | |
- cron: '0 * * * *' # 每小时的第 0 分钟触发一次 | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: 检出仓库代码 | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
# Step 2: 设置 Python 运行环境 | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.8' | |
# Step 3: 安装依赖 | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
# Step 4: 启动应用 | |
- name: Run app.py | |
run: | | |
nohup python app.py & | |
# Step 5: 安装 Cpolar 并启动免费隧道 | |
- name: Install and run Cpolar | |
run: | | |
wget https://www.cpolar.com/static/downloads/install-release-cpolar.sh | |
chmod +x install-release-cpolar.sh | |
sudo ./install-release-cpolar.sh | |
# 使用 GitHub Secrets 中的 Cpolar Token 来授权 | |
cpolar authtoken ${{ secrets.CPOLAR_TOKEN }} | |
# 启动 Cpolar 隧道并保存日志 | |
nohup cpolar http -region=cn_top -log=stdout 5000 > cpolar_tunnel_log.txt 2>&1 & | |
# Step 6: 实时监控日志并提取 URL | |
- name: Monitor tunnel log for URL | |
run: | | |
sleep 30 # 等待 Cpolar 隧道启动 | |
TUNNEL_URL=$(grep -o 'https://[0-9a-z]*\.[0-9a-z]*\.cpolar\.top' cpolar_tunnel_log.txt) | |
echo "$TUNNEL_URL" > tunnel_url.txt | |
echo "--------------------------------" | |
cat tunnel_url.txt | |
echo "--------------------------------" | |
# Step 7: 更新 README.md 中的 Cpolar URL | |
- name: Update README with Cpolar URL | |
run: | | |
TUNNEL_URL=$(head -n 1 tunnel_url.txt) | |
if grep -q "Application is live at:" README.md; then | |
sed -i "s#Application is live at: .*#Application is live at: $TUNNEL_URL#g" README.md | |
else | |
echo "Application is live at: $TUNNEL_URL" >> README.md | |
fi | |
# Step 8: 提交更新后的 README.md 文件 | |
- name: Commit and push changes | |
run: | | |
git config --global user.name 'GitHub Actions' | |
git config --global user.email '[email protected]' | |
git add README.md | |
git commit -m "Update README with Cpolar URL" | |
git push | |
# Step 9: 保持隧道运行 1 小时 | |
- name: Keep tunnel alive | |
run: | | |
sleep 3000 # 等待 1 小时,保持 Cpolar 隧道活跃 | |
# Step 10: 更新配置文件 | |
- name: Commit and push changes | |
run: | | |
git config --global user.name 'GitHub Actions' | |
git config --global user.email '[email protected]' | |
git add chat.db | |
git commit -m "更新配置文件" | |
git push |