likes
comments
collection
share

github自动化部署在线预览环境及github,gitee代码同步

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

目前使用比较多的仓库应该就是gitee和github了,并且gitee和github都提供了在线预览的功能,并且可以自动部署,为广大仓库使用者提供了极大的遍历,现记录一下github自动部署以及代码同步,相当于是学习记录笔记了,如有不对之处欢迎指正

github自动部署在线预览环境

通过设置actions添加workflows实现自动化部署 原理就是在push,pull等钩子触发workflows里配置的事件 workflows里添加.yml文件,上传到github会自动识别并执行.github/workflows/*.yml 里配置的事件

贴上demo(具体步骤根据实际项目需要,这个demo是vue项目部署的脚本)

name: Deploy  //事件流名称

on:   //在什么钩子执行
  push: // push钩子执行
    branches:  //分支
      - develop  // 监听develop分支

jobs:   //一个工作流可以有多个job
  push-to-gh-pages:   // job名称
  runs-on: ubuntu-latest  //运行环境
    steps:                //一个job里可以执行多个step
      - name: Checkout    //step名称
        uses: actions/checkout@v2

      - name: use Node.js 16
        uses: actions/setup-node@v2.1.2
        with:
          node-version: '16.x'

      - name: Get yarn cache
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Set SSH Environment
        env:
          DOCS_DEPLOY_KEY: ${{ secrets.ACTIONS_DEPLOY_KEY }}
        run: |
          mkdir -p ~/.ssh/
          echo "$ACTIONS_DEPLOY_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com > ~/.ssh/known_hosts
          chmod 700 ~/.ssh && chmod 600 ~/.ssh/*
          git config --local user.email "1522248771@qq.com"
          git config --local user.name "z6w6j6"
      - name: Build
        run: |
          yarn install
          yarn run build

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{secrets.ACTIONS_DEPLOY_KEY}}
          publish_branch: gh-pages
          publish_dir: ./dist
          force_orphan: true

生成ssh公钥和私钥

1.打开cmd 输入生成秘钥命令ssh-keygen -t rsa -b 4096 -C "your_email@example.com",将 your_email@example.com 替换为自己的 Email 地址,一路回车。 2.执行完毕后秘钥和公钥都会生成在目录C:\Users\111\.ssh下 3.复制公钥,并在github添加,电脑就才可以正常上传拉取github代码

在github中添加公钥

打开github网站->setting->SSH and GPG keys 点击New SSH keys 添加上述生成的sshkey 或者直接到C:\Users\111\.ssh\id_rsa.pub复制里面的内容就是所需的key

在actions中添加私钥

私钥内容就是C:\Users\111\.ssh\id_rsa路径下的内容复制过来添加进去,上述demo里的ACTIONS_DEPLOY_KEY就是这里的私钥(这里特别感谢一下chatgpt,一直以为添加actions里添加的是setting里面的公钥) github自动化部署在线预览环境及github,gitee代码同步

git代码和gitee代码同步

同时把代码提交到gitee和github仓库

在根目录下的.git 文件夹里面找到config配置文件,加上对应的仓库地址 想要提交成功的前提是本机已经配置了gitee和github的公钥(添加公钥的方法上面有提到) github自动化部署在线预览环境及github,gitee代码同步 github自动化部署在线预览环境及github,gitee代码同步

jenkins自动化部署配置