Github实现push触发自动构建(包括提交代码到另一个仓库中和发布npm)

将指定内容自动更新到另一个代码仓库中

  1. 登录https://github.com,创建Personal access token:
    (1)github右上角,点击头像,进入Settings => Developer settings => Personal access tokens 下面的Tokens (classic)中,点击Generate new token,选择Generate new token (classic)
    (2)跳出的页面中输入github密码进行验证,
    (3)Note中填写此token的描述,过期时间自己选择,select scopes中选择repo(全选),点击Generate token按钮,即可生成一个token,此时会显示具体token内容,记得一定要复制!!!(只此显示一次,后面都不会再次显示)
    在这里插入图片描述

  2. 进入该代码仓库中新增secret,按照下图新增一个secret,对于需要用到的用户名、邮箱也可以在这里自定义添加,到这里配置基本结束
    在这里插入图片描述
    在这里插入图片描述

  3. 接下来点击Actions创建wokeflows脚本,目录在.guthub/workflows目录下选择合适的工作流脚本文件

  4. 脚本文件内容如下(根据需要调整内容,复制后请把//后面的注释删掉):

name: update fe-toolkit-doc

on:
  push:
    branches:
      - master  // 这里替换为自己要触发的分支

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '18.x'
      - name: Install Dependencies
        run: npm i
      - name: Build
        run: npm run build:doc   // 替换为自己的打包命令

      - name: Clone destination repository
        run: |
          git clone https://这里是github的username:${{ secrets.API_TOKEN_GITHUB }}@github.com/这里是github的username/要提交代码的另一个仓库名称.git destination_repo

      - name: Backup all files in the repository
        run: |
         mkdir -p dist/backup  // 创建备份文件
         cp -r destination_repo/* dist/backup/

      - name: Remove all files from the repository
        run: |
          cd destination_repo
          git rm -r *

      - name: Commit and push changes
        run: |
          cd destination_repo
          git config --local user.email ${{ secrets.USER_EMAIL }}
          git config --local user.name ${{ secrets.USER_NAME }}
          git commit -m "Remove all files"
          git push origin master

      - name: Push to another repository
        uses: cpina/github-action-push-to-another-repository@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
        with:
          source-directory: 'dist'   // 要提交的目标文件
          destination-github-username:  ${{ secrets.STORE_USER_NAME }}
          destination-repository-name: ${{ secrets.STORE_ANOTHER_NAME }}
          user-email: ${{ secrets.USER_EMAIL }}
          target-branch: master  // 目标分支

  1. 提交代码后到 Actions下工作流查看进度和日志
    在这里插入图片描述

代码提交后自动发布更新npm包

  1. 按照上面第一步操作创建一个Personal access token
  2. 按照上面第二步添加该Personal access token
  3. 登录https://www.npmjs.com/,按照下图去创建npm的access token(会跳出npm的验证页面,输入密码即可),最后会生成一个token,记得一定要复制!!!,此token只会出现一次
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  4. 然后我们登录到gihub上,打开自己的代码仓库,将此token放到仓库中的secrets中,此时就基本完成的所有的创建工作
    在这里插入图片描述
  5. 按照上面第三部操作创建一个工作流脚本文件
  6. 脚本内容如下(根据需要调整内容,复制后请把//后面的注释删掉):
name: npm publish

on:
  push:
    branches:
      - master  // 触发分支

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '18.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: |
          cd packages/utils  // 要发布的npm文件目录
          npm i

      - name: Set user info
        run: |
          git config user.name 自己的username
          git config user.email 自己的邮箱

      - name: Version bump and Publish to npm  // 每次发布都需要更新版本号
        run: |
          cd packages/utils   // 要发布的npm文件目录
          npm version patch -m "Version bumped to %s" && npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Commit and push changes  // 发布后需要将增加的版本号更新到仓库中
        run: |
          git config --local user.email ${{ secrets.USER_EMAIL }}
          git config --local user.name ${{ secrets.USER_NAME }}
          git add .
          git commit -m "Update version"
          git push https://这里是github的username:${{ secrets.API_TOKEN_GITHUB }}@github.com/这里是github的username/这里是github的仓库名称.git

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将GitHub代码发布npm,可以按照以下步骤进行操作: 1. 首先,在代码目录中执行以下命令将代码提交GitHub仓库: ``` git add . git commit -m "init commit" git push ``` 2. 将GitHub仓库地址复制到npm包的package.json文件中的"repository"字段中,例如: ``` "repository": "https://github.com/your_username/your_repository.git" ``` 3. 执行以下命令将完整和正确的代码发布npm: ``` npm login // 输入npm账号名 // 输入npm密码 // 输入npm邮箱 npm publish ``` 4. 验证发布是否成功。 此外,你还可以在GitHub目录下创建一个自动提交和更改npm版本的脚本,以便简化发布过程。例如,你可以创建一个名为autopublish.sh的脚本,内容如下: ``` #!/bin/bash commit_name="`date +%Y%m%d%H`_commit" echo "====git auto push start..." echo $commit_name git add . git commit -m $commit_name npm version patch git push echo "====git auto push end..." ``` 然后,在执行发布之前,运行该脚本即可自动提交代码并更改npm版本。 希望以上信息对你有所帮助! #### 引用[.reference_title] - *1* [如何将自己写的代码同步到github发布npm](https://blog.csdn.net/yorcentroll/article/details/105214724)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [利用GitHub actions实现自动发布npm package](https://blog.csdn.net/misswuyang/article/details/116453839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值