少女
385 字
2 分钟
使用Github Actions自动切换网站主题
2024-01-21

现在网站用了六套主题,周一用Diaspora,周二和周日用ShokaX,周三和周六用Particlex,周四用Stellaris,周五用Fluid,里站用Brewski,可喜可贺

使用Github Actions实现每天自动切换网站的主题

比如周一、三、五、七使用theme-A,二、四、六使用theme-B

当然也可以添加theme-Ctheme-D

  1. 用户名.github.io仓库中有两个文件夹theme-A/theme-B/,用于存放两套主题的静态文件

  2. 在GitHub的 Settings -> Developer Settings -> Personal access tokens -> Tokens (classic) (链接)生成一个token,填写Note,勾选repoworkflow然后生成

    token

  3. 在仓库的 Settings -> Secrets and variables -> Actions 添加刚才的token

    secret

  4. 在GitHub仓库中新建.github/workflows,新建一个.yaml文件,文件名随意;可以先点Run workflow把静态文件部署到gh-pages分支

name: Update Blog

on:
  workflow_dispatch:
  schedule:
    - cron: "0 2 * * *"

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Determine Deployment Directory
        id: determine_directory
        run: |
          day_of_week=$(date +'%u')
          if [ $((day_of_week % 2)) -eq 0 ]; then
          echo "::set-output name=directory::theme-B/"
            else
          echo "::set-output name=directory::theme-A/"
          fi
          
      - name: Get Current Time
        id: current_time
        run: echo "::set-output name=time::$(date +'%Y-%m-%d %H:%M:%S')"

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3.9.3 # 使用部署到 GitHub pages 的 action
        with:
          publish_dir: ${{ steps.determine_directory.outputs.directory }} 
          github_token: ${{ secrets.DEPLOY_SECRET }} # secret 名
          user_name: ${{ secrets.MY_USER_NAME }}
          user_email: ${{ secrets.MY_USER_EMAIL }}
          commit_message: "Automated Deployment at ${{ steps.current_time.outputs.time }}" # 在 commit message 中添加当前时间
  1. 在仓库的 Settings -> Pages 设置Build and deploymentSourceDeploy from a branch,选择gh-pages分支,此时GitHub Pages将开始部署

总之就挺神经病的

写的方法我自己都看不懂

感谢ChatGPT提供的GitHub Actions的源码

使用Github Actions自动切换网站主题
https://onaniishoujo.github.io/posts/使用github-actions自动切换网站主题/
作者
オナニー少女
发布于
2024-01-21
许可协议
CC BY-NC-SA 4.0