创建动态 README.md 文件

2025-05-28

创建动态 README.md 文件

图片描述

这是我的 Github Profile。这里具体说的是,天气每 6 小时自动更新一次。
(I know that embedding weather in the readme does nothing; it's just for demonstration purposes in the 'Creating Dynamic README.md File')

在本文中,我将向您展示我是如何做到的!
让我们开始吧!

1.什么是 Github Actions

GitHub Actions 是一个持续集成和持续交付 (CI/CD) 平台,可让您自动化构建、测试和部署流程。它类似于 Gitlab CI。在这个项目中,我使用 Github Action 自动更新 README.md 文件中的天气信息。

2. Github Action Workflow 触发器

工作流触发器是导致工作流运行的事件。这些事件可以是:

  • 工作流程存储库中发生的事件,例如:
    • 拉取请求
    • 预定时间
    • 手动的

在这个项目中,我使用“预定时间”来触发工作流“更新天气”。

3. Go 模板

Go 模板是一种基于预定义模板结构生成文本、HTML 或任何其他输出的强大方法。它类似于 Laravel 等其他框架中的模板以及 Pug 等模板引擎。

(数据 + 模板)= 输出

4. 合并它们以使 Readme 保持最新

  • 实现一个收集天气数据(Data)的服务。
  • 编写一个使用上述数据作为输入的模板。
  • 运行 Go Template 来组合模板和数据来生成输出。
  • 提交并推送输出更改。
  • 编写一个 Github 操作来间隔地运行上述步骤。

只需使用我的 Github Action

源代码:huantt/weather-forecast

我把上面的四个步骤打包成一个 Github Action。你可以按照以下简单步骤轻松使用它:

步骤 1:在您的存储库中,创建一个名为 的文件README.md.template

第 2 步:在文件中写入您想要的任何内容README.md.template

步骤 3:在您的 中嵌入以下实体之一README.md.template

  • 今日天气表: ```shell

{{ 模板“小时表”$todayWeather.HourlyWeathers }}


- **Daily Weather Table:**
```shell


{{ template "daily-table" .Weathers }}


Enter fullscreen mode Exit fullscreen mode
  • 更新于: ```shell

{{ 格式时间 .更新时间 }}


If you are familiar with Go templates, you have access to the `root` variable, which includes the following fields:

- `Weathers`: An array of daily weather data. You can view the weather struct definition in [model/weather.go](model/weather.go).
- `UpdatedAt`: This field contains the timestamp in the format of `time.Date`.

**Step 4**: Register Github Action
- Create a file `.github/workflows/update-weather.yml` in your repository.

```yml


name: "Cronjob"
on:
  schedule:
    - cron: '15 * * * *'

jobs:
  update-weather:
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Generate README
        uses: huantt/weather-forecast@v1.0.5
        with:
          city: HaNoi
          days: 7
          weather-api-key: ${{ secrets.WEATHER_API_KEY }}
          template-file: 'README.md.template'
          out-file: 'README.md'
      - name: Commit
        run: |
          if git diff --exit-code; then
            echo "No changes to commit."
            exit 0
          else
            git config user.name github-actions
            git config user.email github-actions@github.com
            git add .
            git commit -m "update"
            git push origin main
          fi


Enter fullscreen mode Exit fullscreen mode
  • 更新此文件中的一些变量:
    • 城市:您想要预报天气的城市。
    • days:预测天数。
    • template-file:上述模板文件的路径。例如。template/README.md.template
    • out-file:您的 README.md 文件名。
    • 天气 API 密钥:
    • 在https://www.weatherapi.com注册一个免费的 API 密钥
    • WEATHER_API_KEY使用中的名称设置机密Your repo > settings > Secrets and variables > Actions > New repository secret

步骤 5:提交您的更改,然后 Github 操作将按照指定的 cron 间隔运行以更新 README.md 文件中的天气。

其他 Github 操作

参考

文章来源:https://dev.to/jacktt/creating-dynamic-readmemd-file-388o
PREV
赋予你的终端超能力:tmux 速查表!安装 tmux 会话窗格 Windows 命令模式 获取帮助
NEXT
说再见 Trycatch Hell