搭建git服务器并配置blog自动部署

Table of Contents

unsplash

最方便的还是查看git的官方文档 中关于服务器上git配置的相关章节,比较详细

👉 服务器上的-Git-配置服务器

记录下我搭建过程中的思路以及一些需要注意的点

搭建思路

  • 在本地搭建了hugo配合typora写文章

  • 图片部分使用腾讯云的oss存储实现图床功能 ,(做了防盗链话费不了多钱)

  • 文章写好后推送到github的私有库中,配合action功能自动部署推送到公开库中展示

    📑 Github Page


  • 原先计划

    • 在腾讯云搭建git服务,写好文章同时推送到腾讯云

    • 在服务器中安装hugo,使用git中的钩子功能提交文章后在git服务器编译到指定的目录中

    这次重新搭建折腾了好久都卡在vps拉取的环节上了,所以放弃


  • 使用action的burnett01/rsync-deployments实现对编译好文件的上传

  • 搭建nginx,配置域名实现公网访问

    server {
            listen 80;
            server_name blog.mfcp.fun;
            location / {
                alias /var/www/blog_site/;
            }
    }
    

问题记录

  1. 创建完git用户后设置git-shell,用户就不能登录shell了,正常推送是没有问题的

    根据git文档中搭建好git服务后,根据文档中的说明,更改了git用户的默认shell,只能通过git协议推送代码,不能登录shell,但是我们要新建仓库必须要在shell中操作。

    两种解决办法 👇

    1. 第一是通过高权限账户使用**chsh**命令临时更改git用户的默认shell,创建完在改回去

      # 查看系统shell
      cat /etc/shells    # 第二种
      chsh -s /bin/sh git  #更改git用户的shell
      
    2. 第二种是通过高权限账户创建好文件,然后更改把文件夹的用户和用户组更改为git用户。

  2. 创建的目录和文件要注意权限问题,所属权限不是git用户的话后期推送会失败

  3. git的钩子功能post-receive需要可执行权限才能成功执行

  4. 由于github page跟腾讯云的baseurl不一致,同一个config文件不能使用,创建单独的配置文件编译

  5. 同步到腾讯云ubuntu上需要创建ssh密钥文件,把公钥写入到authorized_keys中,把私钥文件写入到github项目的Security中通过虚拟变量形式来访问


👇以下是成功的action文件shili

# .github/workflows/gh-pages.yml

name: GitHub Pages

on:
  push:
    branches:
      - master

jobs:
  build-deploy:
    runs-on: ubuntu-22.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --minify
    
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: github.ref == 'refs/heads/master'
        with:
          # github_token: ${{ secrets.GITHUB_TOKEN }} 该项适用于发布到源码相同repo的情况,不能用于发布到其他repo
          external_repository: mugeliu/blog	# 发布到哪个repo
          personal_token: ${{ secrets.HUGO_CL_TOKEN }}	# 发布到其他repo需要提供上面生成的personal access token
          publish_branch: master	# 发布到哪个branch
          publish_dir: ./public	# 注意这里指的是要发布哪个文件夹的内容,而不是指发布到目的仓库的什么位置,因为hugo默认生成静态网页到public文件夹,所以这里发布public文件夹里的内容
          commit_message: ${{ github.event.head_commit.message }}
           
# .github/workflows/gh-pages.yml

name: GitHub Pages

on:
  push:
    branches:
      - master

jobs:
  build-deploy:
    runs-on: ubuntu-22.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --minify --config vps-deploy.toml
           
      - name: rsync deployments
        uses: burnett01/rsync-deployments@6.0.0
        with:
          switches: -avzr --delete
          path: public/
          remote_path: ${{ secrets.BLOG_DEPLOY_PATH }}
          remote_host: ${{ secrets.BLOG_DEPLOY_HOST }}
          remote_user: ${{ secrets.BLOG_DEPLOY_USER }}
          remote_key: ${{ secrets.BLOG_DEPLOY_KEY }}