Github Actions 部署方法记录

umwelt2023-11-26recogithub

使用Git Actions部署

文档参考

参考如下

前言

在通过vuepress搭建博客平台后,发现1.0爬不起动了,随后升级2.0平台,采用了新的reco大佬的theme 原有流程是

  1. 写文章
  2. build后生成静态文件
  3. 通过bash脚本自动提交到pages分支
  4. GitHub部署完毕访问

原有步骤需要自己在本地手动打包push,虽然有脚本文件当时也不方便

npm run build

cd .vuepress/dist

git init
git add -A
git commit -m 'deploy'

git push -f git@github.com:spikekkk/nostalgia.git master:main

cd ../
rm -rf dist

懒惰才是人类进步的动力哈哈哈

便考虑有没有自动化脚本,参考到原有reco主题推荐博文自动化部署open in new window, 进行搜索找到了掘金论坛actions参考教程,同时又考虑到文章中有些是进行了加密的,放在一个public仓库得不偿失,想着blog源码放置在private仓库中保存最好

设置 personal access

仓库要部署Actions, 需要在仓库总设置个人 personal access

设置方法如下:

  1. 进入 Github 后,点击我们的头像,
  2. 然后依次进入 Settings -> Developer settings -> Personal access tokens,对应地址就是 Token 生成。
  3. 然后点击右上方的 Generate new token,接着输入 token 的名字,这个名字可以随意,不过还是推荐根据它的用途来命名。
  4. 然后选 Expiration,也就是这个 Token 的有效期,如果我们要长期用,建议选为 No expiration,意思就是无期限。
  5. 最后就是选权限,一般来讲这里选 repo 就够了,但是如果你不确定,那就全都选上也行。然后点击 Generate Token,会生成一个令牌,注意这里它只会出现一次,一旦刷新该网页就不见了,所以最好把它复制到你的备忘录备份一下,而且我们待会也是需要用到这个 Token 的。

设置Secrets

在存放博客源码仓库中,

  1. 点击 Settings -> Secrets,
  2. 点击 New repository secret,新建一个 Secret。
  3. 这里的名字要命名为 BLOG_TOKEN,然后 Value 就是我们上一步中所生成的 Token。
  4. 在配置的.yml文件中需要使用配置的token,名称需要保持一致
 ACCESS_TOKEN: ${{ secrets.BLOG_TOKEN }} 

编写Actions

进入项目的的 Actions 选项,然后新建一个 workflow

生成后的 main.yml 位于项目的 .github/workflows 目录下

  • 参考模板如下:
name: Deploy

# on 定义的是执行该 Action 的时机,这里的配置是指:
# 当 main 分支的 push 事件被触发的时候执行该任务
on:
  push:
    branches: [main]
# jobs 是定义你要运行的具体任务,
# 每个 Action 可以有多个 Job
# 每个 Job 可以有多个 Step
# 每个 Step 又可以有多个 Run
jobs:
  build:
    runs-on: ubuntu-latest # 运行在 ubuntu 系统环境下
    steps:
      - uses: actions/checkout@v2 # uses 命令允许我们使用别人定义好的 Action,这里是从仓库检出分支
      - uses: actions/setup-node@v2 # 这里是设置 node 版本
        with: # 这里相当于传递给上面指令的参数,告诉他使用 node@12.13.1 这个版本
          node-version: '12.13.1'
      - name: Install Dependencies # name 定义步骤的名称,这个可以在该 Action 运行起来之后,在 Github Actions Tab 下看到
        run: | # `run: |` 的方式,允许我们一次定义多个要执行的命令
          yarn install
          yarn run build
      - name: Push To Pages
        working-directory: ./dist # 指定下面指令运行的目录
        run: |
          git init
          git checkout -b main
          git add -A
        # 这里把下面所有 `<YOUR_NAME>/<YOUR_EMAIL>/<COMMIT_MSG>/<YOUR_TOKEN>` 替换成你自己的就可以了
          git -c user.name='<YOUR_NAME>' -c user.email='<YOUR_EMAIL>' commit -m '<COMMIT_MSG>'
        # 这里需要一个 共开仓库 的 secrets 配置,这里的 secrets 是一个环境变量,可以直接拿过来直接用
          git remote add origin https://${{secrets.<YOUR_TOKEN>}}@github.com/<YOUR_NAME>/blog.git
          git push origin main -f -q

发现评论区提供一个新的GitHub Actions 插件更加简洁 GitHub Pages actionopen in new window

Deploy to external repository external_repository By default, your files are published to the repository which is running this action. If you want to publish to another repository on GitHub, set the environment variable external_repository to username external-repository.

- name: Deploy
  uses: peaceiris/actions-gh-pages@v3
  with:
    deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
    external_repository: username/external-repository
    publish_branch: your-branch  # default: gh-pages
    publish_dir: ./public

You can use deploy_key or personal_token. When you use deploy_key, set your private key to the repository which includes this action and set your public key to your external repository.

Note that GITHUB_TOKEN has no permission to access to external repositories. Please create a personal access token and set it to personal_token like personal_token: ${{ secrets.PERSONAL_TOKEN }}.

Use case:

A GitHub Free Plan account cannot use the GitHub Pages in a private repository. To make your source contents private and deploy it with the GitHub Pages, you can deploy your site from a private repository to a public repository using this option.

peaceiris/homepage: A private repository running this action with external_repository: peaceiris/peaceiris.github.io peaceiris/peaceiris.github.io: A public repository using GitHub Pages

所以方法不是唯一的,那个方便就按照那个来吧 我参考的是直接往blogpags仓库覆盖即可

运行查看Actions是否报错

我遇到的最多报错就是文件获取路径不对 修改workflow文件路径便成功了

 working-directory: .vuepress/dist # 指定下面指令运行的目录

完结

GitHub Actions参考文档

阮一峰老师的 GitHub Actions 入门教程open in new window

Last Updated 11/26/2023, 3:28:23 PM