Watch MDCx Release #5338
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: Watch MDCx Release | |
on: | |
push: | |
branches: | |
- none | |
schedule: | |
# 每30分钟 | |
- cron: '*/30 * * * *' | |
workflow_dispatch: | |
inputs: | |
isDev: | |
description: Is Dev | |
type: boolean | |
env: | |
RELEASE_URL: 'https://api.github.com/repos/sqzw-x/mdcx/releases/latest' | |
ENABLE_WATCH: ${{ secrets.ENABLE_WATCH_MDCX_RELEASE }} | |
GITHUB_CURRENT_REPO: ${{ github.event.repository.name }} | |
GITHUB_API_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }} | |
GITHUB_OWNER: ${{ github.repository_owner }} | |
ENABLE_TG_NOTIFICATION: ${{ secrets.ENABLE_TG_NOTIFICATION }} | |
ENABLE_TG_VERBOSE_NOTIFICATION: ${{ secrets.ENABLE_TG_VERBOSE_NOTIFICATION }} | |
SCHEDULE_INTERVAL: 30 | |
jobs: | |
# 检查是否有新版本 | |
# TODO 清理N天前的runs | |
watch: | |
runs-on: ubuntu-latest | |
steps: | |
- | |
run: | | |
# TODO 在job上貌似不能读取env或者secrets | |
if [[ "${{ env.ENABLE_WATCH }}" != "true" && "${{ github.event.inputs.isDev }}" != "true" ]]; then | |
exit 1 | |
fi | |
- | |
name: Set timezone | |
uses: szenius/[email protected] | |
with: | |
timezoneLinux: "Asia/Shanghai" | |
- | |
name: Checkout | |
uses: actions/checkout@v3 | |
- | |
name: Install apt packages | |
run: sudo apt-get install -y jq | |
- | |
name: Check if there is a new MDCx release | |
id: check | |
env: | |
GITHUB_CURRENT_REPO: ${{ env.GITHUB_CURRENT_REPO }} | |
GITHUB_API_TOKEN: ${{ env.GITHUB_API_TOKEN }} | |
CURL_VERBOSE: '-s' | |
run: | | |
source scripts/base.sh | |
source scripts/github.sh | |
source scripts/release-utils.sh | |
VAR_VERSION="MDCX_LATEST_VERSION" | |
VAR_TIME="MDCX_LATEST_TIME" | |
REPO="sqzw-x/mdcx" | |
TAG_NAME="daily_release" | |
REPO="sqzw-x/mdcx" | |
TAG_NAME="daily_release" | |
info=$(get_release_info "$REPO" "$TAG_NAME") | |
if [[ $? -ne 0 ]]; then | |
echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!" | |
exit 1 | |
else | |
echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!" | |
fi | |
echo $info | jq | |
# 发布时间 | |
published_at=$(printf '%s' $info | jq -r ".published_at") | |
echo "📅 发布时间: $published_at" | |
# 版本号 | |
release_version=$(printf '%s' $info | jq -r ".release_version") | |
echo "🔢 版本号: $release_version" | |
newVersion="" | |
newTime="" | |
hasNewVersion=false | |
# 获取最新版本信息 | |
newVersion="$release_version" | |
newTime="$published_at" | |
# 获取已记录的版本信息 | |
latestVersion=$(getVariable $VAR_VERSION) | |
latestTime=$(getVariable $VAR_TIME) | |
echo "📦 已记录的最新版本:$latestVersion" | |
echo "📅 已记录的更新时间:$latestTime" | |
# 任意一个变量为空,都不视为新版本 | |
if [[ -z "$latestVersion" || -z "$latestTime" ]]; then | |
hasNewVersion=false | |
else | |
# 如果版本号或者更新时间不同,视为新版本 | |
if [[ "$newVersion" != "$latestVersion" || "$newTime" != "$latestTime" ]]; then | |
hasNewVersion=true | |
else | |
hasNewVersion=false | |
fi | |
fi | |
# 输出到GITHUB_OUTPUT | |
echo "newVersion=$newVersion" >> $GITHUB_OUTPUT | |
echo "newTime=$newTime" >> $GITHUB_OUTPUT | |
echo "hasNewVersion=$hasNewVersion" >> $GITHUB_OUTPUT | |
echo "latestVersion=$latestVersion" >> $GITHUB_OUTPUT | |
echo "latestTime=$latestTime" >> $GITHUB_OUTPUT | |
# 当前时间 | |
echo "currentTime=$(date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT | |
# 下次检查时间 | |
echo "nextCheckTime=$(date -d "+${{ env.SCHEDULE_INTERVAL }} minutes" '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT | |
if [[ "$hasNewVersion" == "true" ]]; then | |
echo "🎉 发现新版本!" | |
echo "📦 新版本号:$newVersion" | |
echo "📅 更新时间:$newTime" | |
updateVariable $VAR_TIME "$newTime" | |
updateVariable $VAR_VERSION "$newVersion" | |
exit 0 | |
else | |
echo "🔎 没有新版本,等待下一次检查..." | |
fi | |
- | |
name: TG Notification - For new version | |
uses: appleboy/telegram-action@master | |
if: ${{ steps.check.outputs.hasNewVersion == 'true' && env.ENABLE_TG_NOTIFICATION == 'true' }} | |
with: | |
to: ${{ secrets.TELE_CHAT_ID }} | |
token: ${{ secrets.TELE_BOT_TOKEN }} | |
message: | | |
🎉 MDCx新版本! | |
📦 新版本号:${{ steps.check.outputs.newVersion }} | |
📅 更新时间:${{ steps.check.outputs.newTime }} | |
🔗 发布链接:https://github.com/sqzw-x/mdcx/releases/tag/latest | |
- | |
name: TG Notification - For no new version | |
uses: appleboy/telegram-action@master | |
if: ${{ steps.check.outputs.hasNewVersion == 'false' && env.ENABLE_TG_VERBOSE_NOTIFICATION == 'true' }} | |
with: | |
to: ${{ secrets.TELE_CHAT_ID }} | |
token: ${{ secrets.TELE_BOT_TOKEN }} | |
message: | | |
🔔 MDCx新版本检查通知 - 没有新版本 | |
📦 最新版本:${{ steps.check.outputs.latestVersion }} | |
📅 发布时间:${{ steps.check.outputs.latestTime }} | |
⏰ 检查时间:${{ steps.check.outputs.currentTime }} | |
⏰ 下次检查:${{ steps.check.outputs.nextCheckTime }} | |
- | |
name: Trigger "Image build-mdcx CI" | |
if: ${{ steps.check.outputs.hasNewVersion == 'true' }} | |
run: | | |
# Personal Access Token | |
TOKEN="${{ env.GITHUB_API_TOKEN }}" | |
# GitHub Repo名称 | |
REPO="${{ env.GITHUB_OWNER }}/${{ env.GITHUB_CURRENT_REPO }}" | |
# Workflow Name | |
WORKFLOW_NAME="Image build-mdcx CI" | |
# 根据名称获取workflow id | |
WORKFLOW_ID=$(curl -s -H "Authorization: Bearer ${TOKEN}" \ | |
"https://api.github.com/repos/${REPO}/actions/workflows" \ | |
| jq -r ".workflows | .[] | select(.name==\"${WORKFLOW_NAME}\") | .id") | |
if [[ -z "$WORKFLOW_ID" ]]; then | |
echo "❌ 获取workflow id失败!" | |
exit 1 | |
else | |
echo "📦 workflow id:$WORKFLOW_ID" | |
fi | |
echo "🚀 触发构建..." | |
curl -L \ | |
-X POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${TOKEN}"\ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
https://api.github.com/repos/${REPO}/actions/workflows/${WORKFLOW_ID}/dispatches \ | |
-d '{"ref":"main","inputs":{"stage":"prod","baseImage":"latest"}}' | |
if [[ $? -ne 0 ]]; then | |
echo "❌ 触发构建失败!" | |
exit 1 | |
fi | |