Fetch JTWC Products #83
This file contains hidden or 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: Fetch JTWC Products | |
| # 触发条件:每10分钟自动运行,且允许手动触发 | |
| on: | |
| schedule: | |
| - cron: '*/10 * * * *' # 每10分钟运行一次 | |
| workflow_dispatch: # 允许手动触发 | |
| jobs: | |
| fetch-and-commit: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 检出代码仓库 | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # 确保获取所有分支历史 | |
| # 切换到pgtw分支,如果不存在则创建 | |
| - name: Switch to pgtw branch or create it | |
| run: | | |
| # 检查远程是否存在pgtw分支 | |
| if git ls-remote --heads origin pgtw | grep -q pgtw; then | |
| echo "Remote branch pgtw exists" | |
| # 检查本地是否已有pgtw分支 | |
| if git rev-parse --verify pgtw; then | |
| echo "Local branch pgtw exists, checking it out" | |
| git checkout pgtw | |
| else | |
| echo "Local branch pgtw does not exist, creating it from remote" | |
| git checkout -b pgtw origin/pgtw | |
| fi | |
| # 拉取远程最新更改 | |
| git pull origin pgtw | |
| else | |
| echo "Branch pgtw does not exist remotely, creating new local branch" | |
| git checkout --orphan pgtw # 创建一个无历史的新分支 | |
| git rm -rf . # 清除工作区内容,从空白开始 | |
| fi | |
| # 从JTWC主页抓取所有产品txt链接并下载 | |
| - name: Fetch all JTWC product txt files | |
| run: | | |
| echo "=== Fetching JTWC main page ===" | |
| # 下载JTWC主页HTML | |
| PAGE_HTML=$(curl -s https://www.metoc.navy.mil/jtwc/jtwc.html) | |
| # 提取所有 https://www.metoc.navy.mil/jtwc/products/ 开头、.txt 结尾的链接 | |
| LINKS=$(echo "$PAGE_HTML" | grep -oP 'https://www\.metoc\.navy\.mil/jtwc/products/[^"'\''<>\s]*\.txt' | sort -u) | |
| if [ -z "$LINKS" ]; then | |
| echo "No .txt links found on the page." | |
| exit 0 | |
| fi | |
| echo "=== Found the following .txt links ===" | |
| echo "$LINKS" | |
| echo "" | |
| # 创建products目录存放下载的文件 | |
| mkdir -p products | |
| # 逐个下载 | |
| DOWNLOAD_COUNT=0 | |
| for url in $LINKS; do | |
| # 从URL中提取文件名 | |
| FILENAME=$(basename "$url") | |
| echo "Downloading: $url -> products/$FILENAME" | |
| curl -s -o "products/$FILENAME" "$url" && DOWNLOAD_COUNT=$((DOWNLOAD_COUNT + 1)) || echo " Failed to download: $url" | |
| done | |
| echo "" | |
| echo "=== Download complete: $DOWNLOAD_COUNT files ===" | |
| ls -la products/ | |
| # 配置Git用户信息 | |
| - name: Configure Git | |
| run: | | |
| git config --global user.name "GitHub Actions" | |
| git config --global user.email "actions@github.com" | |
| # 提交更改 | |
| - name: Commit and push changes | |
| run: | | |
| git add products/ | |
| # 检查是否有更改 | |
| if git diff --cached --quiet; then | |
| echo "No changes to commit" | |
| else | |
| FILE_COUNT=$(git diff --cached --name-only | wc -l | tr -d ' ') | |
| git commit -m "Update JTWC products ($FILE_COUNT files): $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
| git push origin pgtw | |
| fi |