Git-Github自动化部署方式

115 阅读1分钟
  1. Git 钩子 (部署相关hooks仅支持私有服务器使用) 了解git裸仓库并利用post-receive自动化部署 部署git服务器实现自动发布代码
  • 示例
# 在服务器端初始化git项目并配置hooks
git init --bare projectA.git
touch projectA.git/hooks/post-receive
chmod 755 projectA.git/hooks/post-receive
vi projectA.git/hooks/post-receive
# post-receive内容参考如下
DIR=/data/publish/projectA
git --work-tree=${DIR} clean -fd
git --work-tree=${DIR} checkout --force
cd ${DIR}
chmod 755 build.sh
./build.sh
  1. Github Actions (需有外网访问通道供Github调用) 在 linux 服务器|上部署的 GitHub 操作 使用 Github Action 将静态页面发布到指定的服务器 使用GithubActions自动部署应用到自己的服务器 Github调戏Action手记——自动构建并发布到另一仓库 github action自动部署构建入门 Appleboy scp action master | Autoscripts.net
  • 示例
name: Auto Publish Website # 自动部署的名称
on:
  push:
    branches:
      - main
    # tags: # 当我们提交代码为tag 是以'v'开头的时候才会触发自动部署到服务端 如 git push tag v0.1.0
    #   - 'v*'
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest # 运行环境,告诉它运行在什么环境
    steps: # 步骤

    # 第一步:下载源码(CI/CD拉取代码到自己的本地)
    - name: Checkout
      uses: actions/checkout@main

    # 使用 node:14
    - name: use Node.js 14
      uses: actions/setup-node@main
      with:
        node-version: 14

    # 第二步:打包构建
    - name: Build
      uses: actions/setup-node@main
    - run: npm install # 安装第三方包
    - run: npm run build # 打包

    # 第三步:部署到服务器
    - name: Deploy
      uses: appleboy/scp-action@master
      with:
        host: ${{ secrets.SERVER_HOST }}
        username: ${{ secrets.SERVER_USER }}
        port: "${{ secrets.SERVER_PORT }}"
        password: ${{ secrets.SERVER_PASSWORD }}
        #source: "dist/"
        source: ${{ secrets.Project_SOURCE }}
        # target: "/www/wwwroot/base/piano/"
        target: ${{ secrets.SERVER_TARGET }}