This repository has been archived on 2024-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
proxyapi/.gitea/workflows/cicd.yaml

80 lines
2.6 KiB
YAML
Raw Permalink Normal View History

2024-01-15 11:33:52 +08:00
name: Rssky Auto Deploy
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: SSH and Execute Script
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.REMOTE_USER }}
password: ${{ secrets.SSH_PASSWORD }}
script: |
#!/bin/bash
# 发送推送通知的函数
send_msg() {
local title="Rssky Actions"
local message="$1"
curl -H "Authorization: Bearer ${{ secrets.MSG_NTFY_TOKEN }}" \
-H "Title: $title" \
-d "$message" \
"${{ vars.MSG_NTFY_URL }}"
}
# 设置变量
REPO_URL="${{ vars.REPO_URL }}"
GIT_TOKEN="${{ secrets.GIT_PUSH_PULL_TOKEN }}"
IMAGE_NAME_BASE="proxyapi"
CONTAINER_NAME="proxyapi"
REPO_DIR="/opt/proxyapi"
DATE=$(date +%Y%m%d%H%M%S)
IMAGE_TAG="${IMAGE_NAME_BASE}:${DATE}"
# 删除旧仓库目录(如果存在)
if [ -d "$REPO_DIR" ]; then
rm -rf "$REPO_DIR" || { send_msg "ProxyAPI 删除旧仓库目录失败"; exit 1; }
fi
# 克隆代码
if ! git clone https://${GIT_TOKEN}@${REPO_URL#https://} $REPO_DIR; then
send_msg "ProxyAPI 克隆失败"
exit 1
fi
cd $REPO_DIR
# 编译 Dockerfile
if ! docker build -t $IMAGE_TAG .; then
send_msg "ProxyAPI 容器构建失败"
exit 1
fi
# 更新容器
if docker ps -a | grep -q $CONTAINER_NAME; then
docker stop $CONTAINER_NAME
docker rm $CONTAINER_NAME
fi
# 创建并启动新容器
if ! docker run -d --name $CONTAINER_NAME \
--network 1panel-network \
-p 50666:5000 \
$IMAGE_TAG; then
send_msg "ProxyAPI 容器启动失败"
exit 1
fi
# 部署成功通知
send_msg "ProxyAPI 容器 ${CONTAINER_NAME} 已部署,使用镜像标签 ${IMAGE_TAG}"
# 清理旧镜像
docker images | grep $IMAGE_NAME_BASE | grep -v $DATE | awk '{print $3}' | xargs -r docker rmi