likes
comments
collection
share

如何通过 Actions 为 Github 首页制作酷炫的效果?

作者站长头像
站长
· 阅读数 15

一、前言

这次给大家介绍一下通过 GitHub Actions 才能实现的炫酷效果,那么我们开始吧!

二、何为 Github Aictions?

GitHub ActionsGithub 于 2018 年推出的持续集成服务,可以理解为一段脚本,这段脚本包含了你想要进行的操作。

Github Actions 的配置文件是一个 YAML 格式的文件,它存放于代码仓库的 .github/workflows 目录下,当 Github 发现该目录下存在文件时,便会自动执行文件中的代码。

此外,Github 还提供了一个 官方市场,这个市场集合了大部分常用的功能模块以供用户使用,你也可以编写自己的代码上传到这个市场。

如何通过 Actions 为 Github 首页制作酷炫的效果?

用户找到了想要执行的功能时,复制相关代码到自己的配置文件即可。

如何通过 Actions 为 Github 首页制作酷炫的效果?

配置文件主要包含了四个部分:

  • workflow: 持续集成一次运行的过程。
  • job:workflow 中的任务,一个 workflow 可以包含多个 job。
  • step:job 中的步骤,一个 job 可以包含多个 step。
  • action:每个 step 都由一个或多个 action 组成。

更多的介绍可以看看阮一峰的教程,这里面的介绍很详细,笔者就不班门弄斧了 。

由于我们的 Actions 都需要改动我们的仓库,因此需要更改相关设置,给予 workflow 读写权限。

如何通过 Actions 为 Github 首页制作酷炫的效果?

三、贪吃蛇动画

如何通过 Actions 为 Github 首页制作酷炫的效果?

首先在 .github/workflows 目录下新建一个配置文件,其内容如下:

name: generate animation

on:
  # run automatically every 24 hours
  schedule:
    - cron: "0 */24 * * *" 
  
  # allows to manually run the job at any time
  workflow_dispatch:
  
  # run on every push on the master branch
  push:
    branches:
    - main
    
  

jobs:
  generate:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    
    steps:
      # generates a snake game from a github user (<github_user_name>) contributions graph, output a svg animation at <svg_out_path>
      - name: generate github-contribution-grid-snake.svg
        uses: Platane/snk/svg-only@v3
        with:
          github_user_name: ${{ github.repository_owner }}
          outputs: |
            dist/github-contribution-grid-snake.svg
            dist/github-contribution-grid-snake-dark.svg?palette=github-dark
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          
          
      # push the content of <build_dir> to a branch
      # the content will be available at https://raw.githubusercontent.com/<github_user>/<repository>/<target_branch>/<file> , or as github page
      - name: push github-contribution-grid-snake.svg to the output branch
        uses: crazy-max/ghaction-github-pages@v3.1.0
        with:
          target_branch: output
          build_dir: dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

推送到仓库之后,Github 便会执行这个文件,并在仓库的 output 分支中生成 github-contribution-grid-snake-dark.svggithub-contribution-grid-snake.svg 这两个文件 。

最后,我们只需要在 README.md 文件中添加以下代码, 并将笔者的 Github ID 换成你的即可。

![](https://raw.githubusercontent.com/Cwd295645351/Cwd295645351/output/github-contribution-grid-snake.svg)

四、代码活动统计

如何通过 Actions 为 Github 首页制作酷炫的效果?

代码活动统计需要借助 WakaTime 工具来实现,它展示了你用编辑器时用的语言的使用时长,接下来介绍一下怎么使用该功能。

第一步,用 Github 账号登陆官网,并从设置中获取到密钥。

如何通过 Actions 为 Github 首页制作酷炫的效果?

第二步,回到我们的仓库,在设置中创建个 Actions secretsname 字段填 WAKATIME_API_KEYSecret 字段填刚才复制的密钥。

如何通过 Actions 为 Github 首页制作酷炫的效果?

如何通过 Actions 为 Github 首页制作酷炫的效果?

第三步,我们需要在编辑器添加对应的 WakaTime 插件,笔者用的是 VSCode,在拓展中搜索该插件并安装。

如何通过 Actions 为 Github 首页制作酷炫的效果?

安装完毕后,编辑器顶部会提示输入 WakaTime Api Key,此时将我们在 WakaTime 网站复制的密钥填进去即可。

如何通过 Actions 为 Github 首页制作酷炫的效果?

第四步,新建一个配置文件,内容如下,官网有更加详细的配置供参考。

name: Waka Readme

on:
  workflow_dispatch:
  schedule:
    # Runs at 12am UTC
    - cron: "0 0 * * *"

jobs:
  update-readme:
    name: WakaReadme DevMetrics
    runs-on: ubuntu-latest
    steps:
      - uses: athul/waka-readme@master
        with:
          WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
          SHOW_TITLE: true
          BLOCKS: ░▒▓█
          TIME_RANGE: last_year
          SHOW_TIME: true
          SHOW_MASKED_TIME: true
          LANG_COUNT: 10

最后,需要在 README.md 文件中加入以下代码:

<!--START_SECTION:waka-->
<!--END_SECTION:waka-->

五、Contributions 三维图

如何通过 Actions 为 Github 首页制作酷炫的效果?

首先看图,这只是其中一种展示效果,配置文件如下:

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
    # Triggers the workflow on push or pull request events but only for the "main" branch
    push:
        branches: ["main"]

    # Allows you to run this workflow manually from the Actions tab
    workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
    # This workflow contains a single job called "build"
    build:
        # The type of runner that the job will run on
        runs-on: ubuntu-latest

        name: generate-github-profile-3d-contrib

        # Steps represent a sequence of tasks that will be executed as part of the job
        steps:
            # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
            - uses: actions/checkout@v3
            - uses: yoshi389111/github-profile-3d-contrib@0.7.1
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
                  USERNAME: ${{ github.repository_owner }}

            # Runs a single command using the runners shell
            - name: Commit & Push
              run: |
                  git config user.name github-actions
                  git config user.email github-actions@github.com
                  git add -A .
                  git commit -m "generated"
                  git push

文件执行后,会在仓库下创建 profile-3d-contrib 目录,并在该目录下生成了各种风格的图片。

如何通过 Actions 为 Github 首页制作酷炫的效果?

找到想要呈现的风格后,在 README.md 文件中引入即可

![](./profile-3d-contrib/profile-gitblock.svg)