git Action

2022/7/23

# 常用配置

# 自动部署

  • 项目自动化部署到 github-page,同时解析到自己域名
  • 项目根路径创建文件夹:.github/workflows/
  • 创建流水线文件:deploy-action.yml
  • 配置文件.github/workflows/deploy-action.yml
  • 重点关注必须更改的地方





 
















 


 





 
 




name: deploy

on:
  push:
    branches: [master] # master 分支有 push 时触发
    paths-ignore:   # 下列文件的变更不触发部署
      - README.md
      - .github/**

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:

      # 检出并下载里面的代码到runner环境中,actions/checkout@v2是官方造的轮子,直接拿来用就行
      - name: Checkout
        uses: actions/checkout@v2

      # 打包构建
      - name: Build
        uses: actions/setup-node@master
        with:
          node-version: '14.17.2'
      - run: npm install
      - run: npm run build
      - run: echo 'coderhdy.com' > ./docs/.vuepress/dist/CNAME # 自定义域名,没有就不添加

      # 部署到 GitHub pages
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3 # 使用部署到 GitHub pages 的 action
        with:
          publish_dir: ./docs/.vuepress/dist # 部署打包后的 dist 目录
          github_token: ${{ secrets.DEPLOY }} # secret 名
          user_name: ${{ secrets.MY_USER_NAME }}
          user_email: ${{ secrets.MY_USER_EMAIL }}
          commit_message: 自动部署 # 部署时的 git 提交信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  • 创建github token
  • GitHub头像 -> Setting -> 最下面Developer settings -> Personal access tokens -> 选择 repo、workflow -> 创建
  • 拷贝token
  • 添加github secrets
  • 需要添加流水线的仓库 -> Settings -> Secrets -> Actions -> New repository secret
  • 粘贴token
  • 填写Secret名字
  • 生成
  • Secret名字填写到.github/workflows/deploy-action.yml文件中
  • 例:Secret名字是DEPLOY,修改第33行:
    github_token: ${{ secrets.DEPLOY }}
    
    1
  • git push
  • git仓库 -> Actions 查看流水线
  • 双击流水线名查看日志

# github同步更新gitee

  • 根目录添加.github/workflows/gitee-sync-actions.yml










 
 
 
 
 


name: SyncToGitee # 自定义名
'on':
  - push
jobs:
  repo-sync:
    runs-on: ubuntu-latest
    steps:
      - name: Mirror the Github organization repos to Gitee.
        uses: Yikun/hub-mirror-action@master # 使用第三方 action cli
        with:
          src: github/coderHDY # github账户
          dst: gitee/coderhdy # gitee账户
          dst_key: '${{ secrets.MY_PRIVATE_KEY }}'
          dst_token: '${{ secrets.GITEE_TOKEN }}'
          static_list: myblog # 仓库名
          force_update: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • github配置公钥
  1. 本机输入
    cat ~/.ssh/id_rsa
    
    1
  2. 复制全部:
    -----BEGIN OPENSSH PRIVATE KEY-----
    XXXXX
    -----END OPENSSH PRIVATE KEY-----
    
    1
    2
    3
  3. Github仓库 -> Setting -> Secrets -> Actions -> New repository secret -> 粘贴私钥 -> 添加 -> 复制Secret name
  4. .github/workflows/gitee-sync-actions.yml文件配置dst_key
    • 例,Secret nameMY_PRIVATE_KEY
    dst_key: '${{ secrets.MY_PRIVATE_KEY }}'
    
    1
  • gitee配置公钥(已有就跳过)
cat ~/.ssh/id_rsa.pub
1
  • gitee主页 -> 个人设置 -> SSH公钥 -> 添加
  • 原理:
  • github拿私钥,gitee拿公钥

由于是github推送东西给gitee,所以他们需要有成对的公私钥

  • gitee生成token:gitee首页 -> 私人设置 -> 私人令牌 -> 生成新令牌 -> 生成 -> 复制令牌内容
  • github保存token:Github仓库 -> Setting -> Secrets -> Actions -> New repository secret -> 粘贴令牌内容 -> 添加 -> 复制Secret name
  • .github/workflows/gitee-sync-actions.yml文件14行配置dst_token
  • 例,Secret nameGITEE_TOKEN
    dst_token: '${{ secrets.GITEE_TOKEN }}'
    
    1
  • git push
  • github仓库 -> action -> 日志
上次更新: 9/17/2024